pub struct ReplacerCollection {
pub value: String,
pub collection: Vec<Replacer>,
pub full_replace: bool,
}Expand description
A collection of placeholders found in a JGD template string.
ReplacerCollection analyzes a template string to find all placeholder patterns
and provides functionality to replace them with generated fake data. It handles
both full replacement (when the entire string is a single placeholder) and
partial replacement (when placeholders are embedded within other text).
§Usage in JGD Templates
JGD templates can contain multiple placeholders that need to be replaced with generated data. This struct manages the entire replacement process, maintaining the order and position of placeholders for correct string manipulation.
§Replacement Modes
-
Full Replacement: When the entire string is a single placeholder (e.g.,
"${name.firstName}")- Returns the generated value directly (may be any JSON type)
- More efficient as it avoids string concatenation
-
Partial Replacement: When placeholders are mixed with literal text (e.g.,
"Hello ${name.firstName}!")- Always returns a string with placeholders substituted
- Processes replacements in reverse order to maintain correct positions
§Examples
use jgd_rs::{ReplacerCollection, GeneratorConfig};
let mut config = GeneratorConfig::new("EN", Some(42));
// Full replacement
let collection = ReplacerCollection::new("${name.firstName}".to_string());
let result = collection.replace(&mut config);
// Partial replacement
let collection = ReplacerCollection::new("Hello ${name.firstName}!".to_string());
let result = collection.replace(&mut config);Fields§
§value: StringThe original template string containing placeholders.
This is preserved for reference and used as the base for replacement
operations. Contains the literal text with ${...} placeholders.
collection: Vec<Replacer>Vector of all placeholders found in the template string.
Each Replacer contains metadata about a placeholder’s position and
content. The collection maintains the order they appear in the string.
full_replace: boolWhether the entire string should be replaced with a single generated value.
Set to true when:
- The string contains exactly one placeholder
- The placeholder spans the entire string (no additional text)
When true, replacement can return any JSON type directly.
When false, replacement always returns a string with substitutions.
Implementations§
Source§impl ReplacerCollection
impl ReplacerCollection
Sourcepub fn new(value: String) -> Self
pub fn new(value: String) -> Self
Creates a new ReplacerCollection by analyzing a template string.
This constructor scans the input string for placeholder patterns using
regex matching and creates Replacer instances for each found placeholder.
It also determines whether full replacement is possible.
§Arguments
value- The template string to analyze for placeholders
§Returns
A new ReplacerCollection with all placeholders identified and metadata populated.
§Full Replacement Detection
Full replacement is enabled when:
- Exactly one placeholder is found
- The placeholder length equals the total string length
- No literal text exists outside the placeholder
§Examples
use jgd_rs::ReplacerCollection;
// Full replacement case
let collection = ReplacerCollection::new("${name.firstName}".to_string());
assert!(collection.full_replace);
// Partial replacement case
let collection = ReplacerCollection::new("Hello ${name.firstName}!".to_string());
assert!(!collection.full_replace);
// No replacement case
let collection = ReplacerCollection::new("Hello world!".to_string());
assert!(collection.is_empty());Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the collection contains any placeholders.
§Returns
true if no placeholders were found in the template string, false otherwise.
§Examples
use jgd_rs::ReplacerCollection;
let empty = ReplacerCollection::new("Hello world!".to_string());
assert!(empty.is_empty());
let not_empty = ReplacerCollection::new("Hello ${name.firstName}!".to_string());
assert!(!not_empty.is_empty());Sourcepub fn replace(
&self,
config: &mut GeneratorConfig,
local_config: Option<&mut LocalConfig>,
) -> Result<Value, JgdGeneratorError>
pub fn replace( &self, config: &mut GeneratorConfig, local_config: Option<&mut LocalConfig>, ) -> Result<Value, JgdGeneratorError>
Performs placeholder replacement using the provided generator configuration.
This method replaces all placeholders in the template string with generated fake data. The replacement behavior depends on whether full or partial replacement is needed.
§Arguments
config- Mutable reference to the generator configuration containing fake data generators and keys
§Returns
Some(Value) containing the result of replacement, or None if replacement fails.
§Replacement Logic
§Full Replacement
When full_replace is true:
- Uses the complete pattern (including arguments) for generation
- Returns the generated value directly (any JSON type)
- Falls back to original string if key is not found
§Partial Replacement
When full_replace is false:
- Processes replacers in reverse order to maintain string positions
- Uses only the key portion (without arguments) for generation
- Converts all generated values to strings for substitution
- Skips invalid keys, leaving their placeholders unchanged
- Always returns a
Value::String
§Key Validation
Only placeholders with keys present in config.fake_keys are replaced.
Invalid keys are left as-is in the output string.
§Examples
use jgd_rs::{ReplacerCollection, GeneratorConfig};
let mut config = GeneratorConfig::new("EN", Some(42));
// Full replacement - returns generated value directly
let collection = ReplacerCollection::new("${name.firstName}".to_string());
let result = collection.replace(&mut config);
// result might be Value::String("John")
// Partial replacement - returns string with substitutions
let collection = ReplacerCollection::new("Hello ${name.firstName}!".to_string());
let result = collection.replace(&mut config);
// result might be Value::String("Hello John!")§Performance Notes
- Full replacement is more efficient as it avoids string manipulation
- Partial replacement processes in reverse order to avoid position shifts
- String conversion is performed for all non-string generated values in partial mode
Auto Trait Implementations§
impl Freeze for ReplacerCollection
impl RefUnwindSafe for ReplacerCollection
impl Send for ReplacerCollection
impl Sync for ReplacerCollection
impl Unpin for ReplacerCollection
impl UnsafeUnpin for ReplacerCollection
impl UnwindSafe for ReplacerCollection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more