chore: code quality pass — type safety, macros, dead code removal#4
Merged
chore: code quality pass — type safety, macros, dead code removal#4
Conversation
These inline implementations were superseded by the sub-crates (zerolease-store-rusqlite, zerolease-store-postgres) in the workspace conversion. They were never declared as modules and didn't compile.
CipherAlgorithm and SecretKind used serde_json::to_string() for DB
storage, which wrapped enum variants in JSON quotes ("\"Pat\""). This
already caused a production bug with AWS tag values.
Replace with explicit as_str() → &'static str and parse_db() → Result
methods using lowercase plain strings (aes256gcm, pat, apikey, etc.).
Includes backward-compatible parsing of legacy JSON-quoted values.
Eliminates the trim_matches('"') hack in the AWS SM tag code.
Replace bare String fields with SecretName, DomainScope, and AgentId from zerolease::types. This makes it impossible to accidentally swap an agent_id with a secret_name at compile time. Make zerolease a non-optional dependency of zerolease-provider (with default-features = false) since the types are lightweight.
Change AuditEvent::indexed_fields() to return (Option<&str>, Option<&LeaseId>) instead of (Option<String>, Option<String>). Eliminates heap allocations on every audit event. Callers that need owned strings convert at the point of use.
Add parse_params! and json_response! macros to reduce the 8 nearly- identical request dispatch branches. Take Request by value in dispatch() so params can be moved instead of cloned. Reduces ~200 lines of boilerplate to ~80 lines of clear intent.
Replace is_not_found() string-parsing helper with typed pattern matching on SdkError::ServiceError variants using the SDK's is_* methods (is_resource_not_found_exception, is_resource_exists_exception, is_invalid_request_exception). More robust against SDK changes.
Add col! macro for extracting typed column values with uniform error handling. Extract parse_id() and parse_timestamp() helpers. Reduces row_to_stored_secret and row_to_metadata from verbose per-field extraction to concise struct initialization. Applied to both rusqlite and postgres store backends. Also extracted row_to_metadata for postgres list() to match the pattern.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Comprehensive quality pass across the codebase following the workspace conversion and AWS SM backend work. Net -1,375 lines (removed 1,688, added 313).
P0: Dead code removal (-1,309 lines)
Deleted three orphaned inline backend files superseded by sub-crates in the workspace conversion.
P1: Type safety — plain string enum serialization
Replaced
serde_json::to_string()/from_str()for DB storage ofCipherAlgorithmandSecretKindwith explicitas_str()→&'static strandparse_db()methods. This pattern already caused a production bug (AWS tag value quoting). Includes backward-compatible parsing of legacy JSON-quoted values.P2: Type safety — newtypes in CredentialRequest
Replaced bare
Stringfields withSecretName,DomainScope,AgentIdinCredentialRequestandCredentialGuard. Prevents compile-time field swaps.P3: Eliminate allocations — borrowed
indexed_fields()Changed
AuditEvent::indexed_fields()to return(Option<&str>, Option<&LeaseId>)instead of(Option<String>, Option<String>). Eliminates heap allocations on every audit event.P4: Dispatch macros — eliminate clone and boilerplate (-72 lines)
Added
parse_params!andjson_response!macros. Changeddispatch()to takeRequestby value so params move instead of clone. Reduces 8 near-identical dispatch branches.P5: Replace
eprintln!withtracing::warn!Standardize on the tracing facade in zerolease-provider.
P6: Typed AWS SDK errors (-25 lines)
Replaced string-matching
is_not_found()with typedSdkError::ServiceErrorpattern matching usingis_resource_not_found_exception(),is_resource_exists_exception(), etc.P7: Row mapping macros (-31 lines)
Added
col!macro andparse_id()/parse_timestamp()helpers in both rusqlite and postgres stores. Reduces verbose per-field extraction to concise struct initialization.Test plan
cargo clippy --workspace --all-targets— cleancargo clippy --manifest-path crates/zerolease-store-postgres/Cargo.toml --all-targets— cleancargo test --workspace— all pass, zero failures