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