vos_ast/ast/
mod.rs

1use std::{
2    cmp::Ordering,
3    collections::BTreeMap,
4    fmt::{Debug, Formatter},
5    ops::Range,
6};
7
8use bigdecimal::BigDecimal;
9use indexmap::IndexMap;
10
11mod constraint;
12mod define;
13mod display;
14mod table;
15mod value;
16
17#[derive(Clone)]
18pub struct VosAST {
19    pub statements: Vec<VosStatement>,
20}
21
22#[derive(Clone)]
23pub enum VosStatement {
24    Table(Box<TableStatement>),
25    Object(Box<ObjectStatement>),
26    Union(Box<UnionStatement>),
27}
28
29#[derive(Clone, Debug, Default)]
30pub struct TableStatement {
31    pub kind: TableKind,
32    pub name: Identifier,
33    pub fields: IndexMap<String, FieldStatement>,
34    pub constraints: BTreeMap<String, ConstraintStatement>,
35}
36
37#[derive(Clone, Debug, Default)]
38pub struct ObjectStatement {
39    pub name: Identifier,
40    pub value: ValueStatement,
41}
42
43#[derive(Clone, Debug, Default)]
44pub struct UnionStatement {
45    pub name: Identifier,
46    pub value: ValueStatement,
47}
48
49#[derive(Debug, Clone)]
50pub enum TableKind {
51    /// Compact structures, lower size and better performance, any changes will break compatibility
52    ///
53    /// - ❌ change order
54    /// - ❌ add fields
55    /// - ❌ change types
56    /// - ❌ delete fields
57    Structure,
58    /// Structure with vtable, any changes will break compatibility
59    ///
60    /// - ✔️ change order
61    /// - ✔️ add fields
62    /// - ❌ change types
63    /// - ❌ delete fields
64    Table,
65}
66
67#[derive(Clone, Default)]
68pub struct FieldStatement {
69    pub name: Identifier,
70    pub typing: FieldTyping,
71    pub value: ValueStatement,
72}
73
74#[derive(Clone, Default)]
75pub struct FieldTyping {
76    pub namespace: Namespace,
77    pub generics: GenericStatement,
78}
79
80#[derive(Clone, PartialEq)]
81pub enum GenericStatement {
82    Nothing,
83    NumberBound { symbol: Ordering, number: BigDecimal, inclusive: bool },
84    NumberRange { min: BigDecimal, min_inclusive: bool, max: BigDecimal, max_inclusive: bool },
85    Arguments { arguments: Vec<ValueStatement> },
86}
87
88#[derive(Debug, Clone)]
89pub struct ConstraintStatement {
90    pub name: Identifier,
91    pub value: ValueStatement,
92}
93
94#[derive(Clone, Default)]
95pub struct ValueStatement {
96    pub kind: ValueKind,
97    pub range: Range<u32>,
98}
99
100#[derive(Clone, PartialEq)]
101pub enum ValueKind {
102    Default,
103    Boolean(bool),
104    String(String),
105    Number(BigDecimal),
106    Symbol(Namespace),
107    List(Vec<ValueStatement>),
108    Dict(IndexMap<String, ValueStatement>),
109}
110
111#[derive(Clone, Default, PartialEq)]
112pub struct Namespace {
113    pub scope: Vec<Identifier>,
114}
115
116#[derive(Clone, Default)]
117pub struct Identifier {
118    pub id: String,
119    pub range: Range<u32>,
120}