Expand description
Pluggable replacement strategies.
This module provides the Strategy trait and five built-in
implementations that can be composed with the mapping engine via
StrategyGenerator, an adapter that implements
ReplacementGenerator.
§Design Note
This is the extensibility layer for library consumers who need custom
replacement logic. The CLI binary uses crate::generator::HmacGenerator
and crate::generator::RandomGenerator directly with category-aware
formatters for performance and simplicity. Both paths share the same
ReplacementGenerator interface. See ARCHITECTURE.md section 2 for
details on the dual-path design.
§Architecture
┌──────────────────────┐
│ MappingStore │ ← owns Arc<dyn ReplacementGenerator>
└──────────┬───────────┘
│ calls generate(category, original)
▼
┌──────────────────────┐
│ StrategyGenerator │ ← adapter: produces entropy, delegates to Strategy
│ (ReplacementGenerator)│
└──────────┬───────────┘
│ calls replace(original, &entropy)
▼
┌──────────────────────┐
│ dyn Strategy │ ← pure function of (original, entropy) → String
│ │
│ RandomString │
│ RandomUuid │
│ FakeIp │
│ PreserveLength │
│ HmacHash │
└──────────────────────┘§Deterministic Mode
Strategies are pure functions of (original, entropy). Determinism is
controlled by the entropy source inside StrategyGenerator:
- Deterministic (
EntropyMode::Deterministic): entropy is derived via HMAC-SHA256 keyed with a fixed seed — same seed + same input → same replacement across runs. - Random (
EntropyMode::Random): entropy comes from OS CSPRNG — each call produces a fresh value (but theMappingStorestill caches the first result per unique input for per-run consistency).
The HmacHash strategy is an exception: it carries its own HMAC key
and is deterministic by construction regardless of the entropy mode.
§Extensibility
To add a new replacement strategy:
- Create a struct implementing
Strategy. - Return a unique name from
Strategy::name. - Implement
Strategy::replaceas a pure function of(original, entropy). - Wrap it in a
StrategyGeneratorto use withMappingStore.
Third-party crates can implement Strategy without modifying this crate,
since the trait is public and object-safe.
Structs§
- FakeIp
- Generates a fake IPv4 address in the
10.0.0.0/8reserved range. - Hmac
Hash - HMAC-SHA256 hash strategy — deterministic by construction.
- Preserve
Length - Generates a replacement with the same byte length as the original.
- Random
String - Generates an alphanumeric string from entropy bytes.
- Random
Uuid - Generates a UUID v4–formatted string from entropy bytes.
- Strategy
Generator - Adapter that bridges a
Strategyinto theReplacementGeneratorinterface consumed byMappingStore.
Enums§
- Entropy
Mode - How entropy is produced for strategies.
Traits§
- Strategy
- A pluggable replacement strategy.