pub enum Field {
Array {
array: ArraySpec,
},
Entity(Entity),
Number {
number: NumberSpec,
},
Optional {
optional: OptionalSpec,
},
Ref {
ref: String,
},
Str(String),
Bool(bool),
I64(i64),
F64(f64),
Null,
}Expand description
A field specification that can generate any JSON value type.
Fields are the fundamental building blocks in JGD schemas. Each field variant
corresponds to a different type of value generation strategy. The enum uses
#[serde(untagged)] to support flexible JSON deserialization where the structure
determines the variant.
§Variants
§Complex Types
Array: Generates arrays with configurable element types and countsEntity: Generates nested objects with multiple fieldsOptional: Conditionally generates values based on probability
§Dynamic Types
Ref: References values from other generated entitiesStr: Template strings with placeholder substitution support
§Primitive Types
Number: Generates numbers within specified rangesBool: Static boolean valuesI64: Static 64-bit integer valuesF64: Static 64-bit floating-point valuesNull: JSON null values
§JGD Schema Examples
{
"name": "John Doe", // Field::Str
"age": { "number": { "min": 18, "max": 65 } }, // Field::Number
"active": true, // Field::Bool
"score": 95.5, // Field::F64
"id": 12345, // Field::I64
"metadata": null, // Field::Null
"email": "${internet.email}", // Field::Str with template
"user_id": { "ref": "users.id" }, // Field::Ref
"tags": { // Field::Array
"array": {
"count": 3,
"of": "${lorem.word}"
}
},
"profile": { // Field::Optional
"optional": {
"prob": 0.8,
"of": { "bio": "${lorem.sentence}" }
}
}
}§Deserialization
The #[serde(untagged)] attribute allows automatic variant detection:
- Objects with
"array"key →Field::Array - Objects with
"number"key →Field::Number - Objects with
"optional"key →Field::Optional - Objects with
"ref"key →Field::Ref - Plain strings →
Field::Str - Plain numbers →
Field::I64orField::F64 - Plain booleans →
Field::Bool null→Field::Null
Variants§
Array
Array field that generates JSON arrays.
Wraps an ArraySpec that defines the element type and count for array generation.
Arrays can contain any field type as elements and support dynamic sizing.
Entity(Entity)
Entity field that generates nested JSON objects.
Embeds a complete Entity specification for generating complex nested structures.
Entities can contain multiple fields and support uniqueness constraints.
Number
Number field that generates numeric values within ranges.
Wraps a NumberSpec that defines the range and type (integer/float) for number generation.
Supports both discrete integer ranges and continuous floating-point ranges.
Fields
number: NumberSpecOptional
Optional field that conditionally generates values.
Wraps an OptionalSpec that defines probability-based value generation.
Can generate the specified field or null based on the configured probability.
Fields
optional: OptionalSpecRef
Reference field that links to other generated entities.
Contains a dot-notation path string for accessing values from previously generated entities. Enables cross-referencing and relational data generation.
Str(String)
String field with template support.
Can be a literal string or contain ${...} placeholders for dynamic content generation.
Supports faker function calls and cross-references to other entities.
Bool(bool)
Static boolean field.
Generates a fixed boolean value without any dynamic behavior.
I64(i64)
Static 64-bit integer field.
Generates a fixed integer value without any dynamic behavior.
F64(f64)
Static 64-bit floating-point field.
Generates a fixed floating-point value without any dynamic behavior.
Null
Null field.
Always generates a JSON null value.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Field
impl<'de> Deserialize<'de> for Field
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 Field
impl JsonGenerator for Field
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 based on the field type.
This method dispatches to the appropriate generation logic for each field variant. It handles all supported field types and ensures proper JSON value generation according to the JGD specification.
§Parameters
config: Mutable reference to generator configuration for accessing state and utilities
§Returns
serde_json::Value: The generated JSON value appropriate for the field type
§Generation Behavior
- Array: Delegates to
ArraySpec::generate()for array creation - Entity: Delegates to
Entity::generate()for object creation - Number: Delegates to
NumberSpec::generate()for numeric value generation - Optional: Delegates to
OptionalSpec::generate()for probability-based generation - Ref: Resolves cross-references using
generate_for_ref() - Str: Processes template strings with placeholder replacement
- Bool/I64/F64/Null: Direct conversion to corresponding JSON values
§Template Processing
String fields undergo template processing to replace ${...} placeholders:
- Faker calls:
"${name.firstName}"→"John" - Cross-references:
"${users.id}"→"12345" - Function calls:
"${lorem.words(3)}"→"lorem ipsum dolor"
§Examples
let field = Field::Str("Hello ${name.firstName}!".to_string());
let result = field.generate(&mut config);
// Result: Value::String("Hello John!")
let number_field = Field::Number {
number: NumberSpec::new_integer(1.0, 100.0)
};
let result = number_field.generate(&mut config);
// Result: Value::Number(42)Auto Trait Implementations§
impl Freeze for Field
impl RefUnwindSafe for Field
impl Send for Field
impl Sync for Field
impl Unpin for Field
impl UnsafeUnpin for Field
impl UnwindSafe for Field
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