pub struct OptionalSpec {
pub of: Box<Field>,
pub prob: f64,
}Expand description
Specification for conditional field generation based on probability.
OptionalSpec wraps any field type and generates its value conditionally based on
a probability threshold. This enables modeling of optional or sparse data patterns
commonly found in real-world JSON datasets.
§Fields
of: The wrapped field specification that will be generated when the probability condition is metprob: Probability value (0.0 to 1.0) determining how often the field is generated
§Probability Behavior
- 0.0: Field is never generated (always
null) - 0.5: Field is generated 50% of the time (default)
- 1.0: Field is always generated (never
null) - Other values: Proportional probability of generation
§JGD Schema Examples
§Basic Optional String
{
"bio": {
"optional": {
"of": "${lorem.paragraph}",
"prob": 0.7
}
}
}§Optional Nested Object
{
"profile": {
"optional": {
"prob": 0.8,
"of": {
"fields": {
"avatar": "${internet.avatar}",
"social": {
"fields": {
"twitter": "${internet.userName}",
"github": "${internet.userName}"
}
}
}
}
}
}
}§Optional Array
{
"tags": {
"optional": {
"prob": 0.6,
"of": {
"array": {
"count": [1, 5],
"of": "${lorem.word}"
}
}
}
}
}§Default Probability
When prob is not specified in the JSON schema, it defaults to 0.5 (50% probability).
This provides balanced optional field generation for realistic data patterns.
§Deserialization
The struct uses Serde’s #[serde(default)] attribute with a custom default function
to provide the 0.5 probability when not explicitly specified in the input JSON.
Fields§
§of: Box<Field>The field specification to generate when the probability condition is met.
This boxed field can be any valid Field type, including primitives, complex objects,
arrays, or even nested optional specifications for multi-level conditional generation.
prob: f64Probability threshold for field generation (0.0 to 1.0).
- Values closer to 0.0 make the field less likely to be generated
- Values closer to 1.0 make the field more likely to be generated
- Defaults to 0.5 when not specified in the JSON schema
§Valid Range
While the type allows any f64 value, probabilities should typically be
between 0.0 and 1.0 for meaningful probability behavior.
Trait Implementations§
Source§impl Clone for OptionalSpec
impl Clone for OptionalSpec
Source§fn clone(&self) -> OptionalSpec
fn clone(&self) -> OptionalSpec
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OptionalSpec
impl Debug for OptionalSpec
Source§impl<'de> Deserialize<'de> for OptionalSpec
impl<'de> Deserialize<'de> for OptionalSpec
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl JsonGenerator for OptionalSpec
impl JsonGenerator for OptionalSpec
Source§fn generate(
&self,
config: &mut GeneratorConfig,
local_config: Option<&mut LocalConfig>,
) -> Result<Value, JgdGeneratorError>
fn generate( &self, config: &mut GeneratorConfig, local_config: Option<&mut LocalConfig>, ) -> Result<Value, JgdGeneratorError>
Generates a JSON value conditionally based on the configured probability.
This method implements the core probability-based generation logic for optional fields.
It uses the random number generator from the configuration to determine whether
to generate the wrapped field or return null.
§Parameters
config: Mutable reference to generator configuration containing the RNG state
§Returns
Value: Either the generated field value orValue::Nullbased on probability
§Algorithm
- Generate a random floating-point number between 0.0 and 1.0
- Compare it against the configured probability threshold
- If random value < probability: generate the wrapped field
- Otherwise: return
Value::Null
§Probability Distribution
The method uses uniform random distribution, ensuring that over many generations, the actual presence rate will converge to the specified probability value.
§Examples
use jgd_rs::{OptionalSpec, Field, GeneratorConfig};
// 80% chance of generating a string
let optional_field = OptionalSpec {
of: Box::new(Field::Str("Hello World".to_string())),
prob: 0.8,
};
let mut config = GeneratorConfig::new("EN", Some(42));
let result = optional_field.generate(&mut config);
// Result: ~80% chance of Value::String("Hello World"), ~20% chance of Value::Null§Deterministic Behavior
When using a seeded random number generator, the probability outcomes become deterministic and reproducible, which is useful for testing and consistent data generation across runs.
Auto Trait Implementations§
impl Freeze for OptionalSpec
impl RefUnwindSafe for OptionalSpec
impl Send for OptionalSpec
impl Sync for OptionalSpec
impl Unpin for OptionalSpec
impl UnsafeUnpin for OptionalSpec
impl UnwindSafe for OptionalSpec
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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