Skip to main content

GeneratorConfig

Struct GeneratorConfig 

Source
pub struct GeneratorConfig {
    pub locale: String,
    pub fake_keys: FakeKeys,
    pub fake_generator: FakeGenerator,
    pub rng: StdRng,
    pub gen_value: Map<String, Value>,
}
Expand description

Configuration for JSON data generation in the JGD system.

GeneratorConfig provides the runtime context and state needed for generating fake JSON data according to JGD (JSON Generator Definition) specifications. It encapsulates the random number generator, locale settings, fake data generators, and maintains state during the generation process.

§Core Components

  • Random Number Generator: Deterministic or random generation using StdRng
  • Locale Support: Locale-specific fake data generation (names, addresses, etc.)
  • Fake Data: Pre-configured generators for realistic fake data
  • Generation State: Tracks generated values for cross-references and relationships

§Usage in JGD Generation

This configuration is passed to all [JsonGenerator] implementations to provide consistent generation context. The same config instance should be used throughout a single generation session to maintain consistency and enable value references.

§Examples

use jgd_rs::{GeneratorConfig, NumberSpec, JsonGenerator};

let mut config = GeneratorConfig::new("EN", Some(42));
let spec = NumberSpec::new_integer(1.0, 10.0);
let value = spec.generate(&mut config, None).unwrap();

§Thread Safety

GeneratorConfig is not thread-safe due to its mutable state. Each thread should have its own instance for concurrent generation.

Fields§

§locale: String

The locale code for locale-specific data generation.

This field determines the language and regional settings used for generating locale-specific fake data such as names, addresses, and phone numbers. Common values include “EN” for English, “ES” for Spanish, etc.

Currently marked as dead_code but preserved for future locale-specific features.

§fake_keys: FakeKeys

Keys for accessing fake data categories.

Provides access to different categories of fake data (names, addresses, etc.) that can be used during generation. This is used in conjunction with the fake generator to produce realistic test data.

§fake_generator: FakeGenerator

Generator for producing locale-specific fake data.

This component generates realistic fake data such as names, addresses, phone numbers, and other locale-specific information based on the configured locale setting.

§rng: StdRng

Random number generator for deterministic or random generation.

Uses StdRng to provide high-quality random numbers. Can be seeded for deterministic generation (useful for testing) or use a random seed for truly random output.

§gen_value: Map<String, Value>

Map storing generated values for cross-references and relationships.

This map maintains the state of previously generated values during a generation session. It enables features like referencing previously generated values or maintaining relationships between different parts of the generated data structure.

Implementations§

Source§

impl GeneratorConfig

Source

pub fn new(locale: &str, seed: Option<u64>) -> Self

Creates a new GeneratorConfig with the specified locale and optional seed.

This constructor initializes all components needed for JGD data generation, including the random number generator, fake data generators, and state storage.

§Arguments
  • locale - The locale code for locale-specific data generation (e.g., “EN”, “ES”)
  • seed - Optional seed for deterministic random number generation. If None, a random seed will be used for non-deterministic generation.
§Returns

A new GeneratorConfig instance ready for data generation.

§Examples
use jgd_rs::GeneratorConfig;

// Deterministic generation with seed
let config = GeneratorConfig::new("EN", Some(42));

// Random generation
let config = GeneratorConfig::new("EN", None);
§Deterministic Generation

When a seed is provided, the generator will produce the same sequence of values across different runs, which is useful for:

  • Testing and debugging
  • Reproducible data sets
  • Consistent development environments
Source

pub fn get_value_from_path(&self, path: String) -> Option<&Value>

Retrieves a value from the generated data using a dot-notation path.

This method enables cross-references and relationships in generated data by allowing access to previously generated values. It supports traversing nested objects and randomly selecting from arrays using dot-notation paths.

§Arguments
  • path - A dot-notation path string (e.g., “user.address.city”)
§Returns

Some(&Value) if the path exists in the generated data, None otherwise.

§Path Resolution Rules
  • Objects: Navigate through object properties using dot notation
  • Arrays: Randomly select an object from the array and continue traversal
  • Primitive Values: Cannot be traversed further (returns None for additional path segments)
§Examples
use jgd_rs::GeneratorConfig;
use serde_json::json;

let mut config = GeneratorConfig::new("EN", Some(42));

// Assume some data has been generated and stored
config.gen_value.insert("user".to_string(), json!({
    "name": "Alice",
    "address": {
        "city": "New York",
        "country": "USA"
    }
}));

// Access nested values
let city = config.get_value_from_path("user.address.city".to_string());
assert_eq!(city, Some(&json!("New York")));
§Use Cases
  • Foreign Key References: Reference IDs from previously generated entities
  • Consistent Data: Ensure related fields have consistent values
  • Complex Relationships: Build interconnected data structures
  • Template Substitution: Replace placeholders with generated values
§Performance Notes
  • Array traversal involves random selection, making it non-deterministic for the same path
  • Deep paths may have performance implications for large data structures
  • Path parsing is done on each call; consider caching for frequently accessed paths

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