Skip to main content

ReplacerCollection

Struct ReplacerCollection 

Source
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: String

The 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: bool

Whether 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

Source

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());
Source

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());
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: Rng + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V