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