datafake_rs/
types.rs

1//! Core type definitions for datafake-rs configuration and context.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::collections::HashMap;
6
7/// The main configuration structure for fake data generation.
8///
9/// This struct represents the complete configuration parsed from JSON,
10/// containing optional metadata, pre-defined variables, and the schema
11/// that defines the structure of generated data.
12///
13/// # Example
14///
15/// ```json
16/// {
17///     "metadata": { "name": "User Generator", "version": "1.0" },
18///     "variables": { "userId": {"fake": ["uuid"]} },
19///     "schema": { "id": {"var": "userId"}, "name": {"fake": ["name"]} }
20/// }
21/// ```
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct DataFakeConfig {
24    /// Optional metadata about this configuration (name, version, description).
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub metadata: Option<Metadata>,
27
28    /// Pre-defined variables that are evaluated once and can be referenced in the schema.
29    /// Use `{"var": "variableName"}` in the schema to reference these values.
30    #[serde(default)]
31    pub variables: HashMap<String, Value>,
32
33    /// The schema defining the structure and fake data types for the output.
34    pub schema: Value,
35}
36
37/// Optional metadata about the configuration.
38///
39/// This can include a name, version, description, and any additional
40/// custom fields which are captured in the `extra` field.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct Metadata {
43    /// Human-readable name for this configuration.
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub name: Option<String>,
46
47    /// Version string for this configuration.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub version: Option<String>,
50
51    /// Description of what this configuration generates.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub description: Option<String>,
54
55    /// Additional custom metadata fields.
56    #[serde(flatten)]
57    pub extra: HashMap<String, Value>,
58}
59
60/// Context for data generation, holding resolved variable values.
61///
62/// The context is created for each generation run and contains
63/// the resolved values of all variables defined in the configuration.
64/// These can be referenced using `{"var": "variableName"}` in the schema.
65#[derive(Debug, Clone)]
66pub struct GenerationContext {
67    /// Map of variable names to their resolved JSON values.
68    pub variables: HashMap<String, Value>,
69}
70
71impl GenerationContext {
72    /// Creates a new empty generation context.
73    pub fn new() -> Self {
74        Self {
75            variables: HashMap::new(),
76        }
77    }
78
79    /// Creates a new context with pre-populated variables.
80    pub fn with_variables(variables: HashMap<String, Value>) -> Self {
81        Self { variables }
82    }
83
84    /// Retrieves a variable value by name.
85    pub fn get_variable(&self, name: &str) -> Option<&Value> {
86        self.variables.get(name)
87    }
88
89    /// Sets or updates a variable value.
90    pub fn set_variable(&mut self, name: String, value: Value) {
91        self.variables.insert(name, value);
92    }
93}
94
95impl Default for GenerationContext {
96    fn default() -> Self {
97        Self::new()
98    }
99}