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