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: StringThe 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: FakeKeysKeys 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: FakeGeneratorGenerator 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: StdRngRandom 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
impl GeneratorConfig
Sourcepub fn new(locale: &str, seed: Option<u64>) -> Self
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. IfNone, 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
Sourcepub fn get_value_from_path(&self, path: String) -> Option<&Value>
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
Nonefor 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§
impl Freeze for GeneratorConfig
impl !RefUnwindSafe for GeneratorConfig
impl !Send for GeneratorConfig
impl !Sync for GeneratorConfig
impl Unpin for GeneratorConfig
impl UnsafeUnpin for GeneratorConfig
impl !UnwindSafe for GeneratorConfig
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