Skip to main content

ron_schema/schema/
mod.rs

1/*************************
2 * Author: Bradley Hunter
3 */
4
5/// Schema parser — converts `.ronschema` source text into a [`Schema`] AST.
6pub mod parser;
7
8use std::collections::{HashMap, HashSet};
9
10use crate::Spanned;
11
12/// A named enum with a closed set of unit variants.
13#[derive(Debug, Clone, PartialEq)]
14pub struct EnumDef {
15    /// The enum name (e.g., `"CardType"`).
16    pub name: String,
17    /// The set of valid variant names.
18    pub variants: HashSet<String>,
19}
20
21/// A type descriptor representing the expected type of a field value.
22#[derive(Debug, Clone, PartialEq)]
23pub enum SchemaType {
24    /// A quoted string.
25    String,
26    /// A whole number (i64).
27    Integer,
28    /// A floating-point number (f64).
29    Float,
30    /// A boolean (`true` or `false`).
31    Bool,
32    /// An optional value — matches `Some(value)` or `None`.
33    Option(Box<SchemaType>),
34    /// A homogeneous list — matches `[value, value, ...]`.
35    List(Box<SchemaType>),
36    /// A reference to a named enum definition.
37    EnumRef(String),
38    /// A reference to a named type alias.
39    AliasRef(String),
40    /// A map with typed keys and values — matches `{ key: value, ... }`.
41    Map(Box<SchemaType>, Box<SchemaType>),
42    /// A positional tuple — matches `(value1, value2, ...)`.
43    Tuple(Vec<SchemaType>),
44    /// An inline nested struct — matches `(field: value, ...)`.
45    Struct(StructDef),
46}
47
48/// A single field definition within a struct.
49#[derive(Debug, Clone, PartialEq)]
50pub struct FieldDef {
51    /// The field name with source location.
52    pub name: Spanned<String>,
53    /// The expected type for this field's value, with source location.
54    pub type_: Spanned<SchemaType>,
55}
56
57/// A struct definition containing an ordered list of field definitions.
58#[derive(Debug, Clone, PartialEq)]
59pub struct StructDef {
60    /// Ordered list of fields. Uses `Vec` to preserve declaration order for error messages.
61    pub fields: Vec<FieldDef>,
62}
63
64/// The top-level schema produced by parsing a `.ronschema` file.
65#[derive(Debug, Clone, PartialEq)]
66pub struct Schema {
67    /// The root struct definition.
68    pub root: StructDef,
69    /// Named enum definitions, keyed by name for O(1) lookup during validation.
70    pub enums: HashMap<String, EnumDef>,
71    /// Type aliases, keyed by name. Stored as-is (not expanded) for better error messages.
72    pub aliases: HashMap<String, Spanned<SchemaType>>,
73}