mold_cli/types/
schema.rs

1use thiserror::Error;
2
3/// Represents all possible JSON/Schema types
4#[derive(Debug, Clone, PartialEq)]
5pub enum SchemaType {
6    // Primitives
7    String,
8    Number,  // Float (f64)
9    Integer, // Whole numbers
10    Boolean,
11    Null,
12
13    // Complex types
14    Array(Box<SchemaType>),
15    Object(ObjectType),
16
17    // Special cases
18    Optional(Box<SchemaType>), // For nullable fields
19    Union(Vec<SchemaType>),    // Mixed arrays: [1, "two"] → number | string
20    Any,                       // Fallback for empty arrays, unknown
21}
22
23/// Represents an object with named fields
24#[derive(Debug, Clone, PartialEq)]
25pub struct ObjectType {
26    pub fields: Vec<Field>,
27}
28
29impl ObjectType {
30    pub fn new(fields: Vec<Field>) -> Self {
31        Self { fields }
32    }
33
34    pub fn empty() -> Self {
35        Self { fields: vec![] }
36    }
37}
38
39/// A single field within an object
40#[derive(Debug, Clone, PartialEq)]
41pub struct Field {
42    pub name: String,
43    pub field_type: SchemaType,
44    pub optional: bool,
45}
46
47impl Field {
48    pub fn new(name: impl Into<String>, field_type: SchemaType) -> Self {
49        Self {
50            name: name.into(),
51            field_type,
52            optional: false,
53        }
54    }
55
56    pub fn optional(mut self) -> Self {
57        self.optional = true;
58        self
59    }
60}
61
62/// Extracted nested object (for non-flat mode)
63#[derive(Debug, Clone)]
64pub struct NestedType {
65    pub name: String,
66    pub object: ObjectType,
67}
68
69impl NestedType {
70    pub fn new(name: impl Into<String>, object: ObjectType) -> Self {
71        Self {
72            name: name.into(),
73            object,
74        }
75    }
76}
77
78/// Top-level schema container
79#[derive(Debug, Clone)]
80pub struct Schema {
81    pub name: String,
82    pub root_type: SchemaType,
83    pub nested_types: Vec<NestedType>,
84}
85
86impl Schema {
87    pub fn new(name: impl Into<String>, root_type: SchemaType) -> Self {
88        Self {
89            name: name.into(),
90            root_type,
91            nested_types: vec![],
92        }
93    }
94
95    pub fn with_nested_types(mut self, nested_types: Vec<NestedType>) -> Self {
96        self.nested_types = nested_types;
97        self
98    }
99}
100
101/// Custom error types for mold
102#[derive(Debug, Error)]
103pub enum MoldError {
104    #[error("Failed to read file: {0}")]
105    FileRead(#[from] std::io::Error),
106
107    #[error("Invalid JSON: {0}")]
108    JsonParse(#[from] serde_json::Error),
109
110    #[error("Root must be an object, got {0}")]
111    InvalidRoot(String),
112
113    #[error("Failed to write output: {0}")]
114    WriteError(String),
115
116    #[error("No output format specified. Use --ts, --zod, --prisma, or --all")]
117    NoOutputFormat,
118}