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