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 /// An inline nested struct — matches `(field: value, ...)`.
43 Struct(StructDef),
44}
45
46/// A single field definition within a struct.
47#[derive(Debug, Clone, PartialEq)]
48pub struct FieldDef {
49 /// The field name with source location.
50 pub name: Spanned<String>,
51 /// The expected type for this field's value, with source location.
52 pub type_: Spanned<SchemaType>,
53}
54
55/// A struct definition containing an ordered list of field definitions.
56#[derive(Debug, Clone, PartialEq)]
57pub struct StructDef {
58 /// Ordered list of fields. Uses `Vec` to preserve declaration order for error messages.
59 pub fields: Vec<FieldDef>,
60}
61
62/// The top-level schema produced by parsing a `.ronschema` file.
63#[derive(Debug, Clone, PartialEq)]
64pub struct Schema {
65 /// The root struct definition.
66 pub root: StructDef,
67 /// Named enum definitions, keyed by name for O(1) lookup during validation.
68 pub enums: HashMap<String, EnumDef>,
69 /// Type aliases, keyed by name. Stored as-is (not expanded) for better error messages.
70 pub aliases: HashMap<String, Spanned<SchemaType>>,
71}