dbml_language_server/
ast.rs

1// src/ast.rs
2#![allow(dead_code)]
3
4use std::ops::Range;
5
6pub type Span = Range<usize>;
7
8#[derive(Debug, Clone)]
9pub struct Document {
10    pub items: Vec<DocumentItem>,
11    pub span: Span,
12}
13
14#[derive(Debug, Clone)]
15pub enum DocumentItem {
16    Table(Table),
17    Ref(Ref),
18    Enum(Enum),
19    Project(Project),
20}
21
22#[derive(Debug, Clone)]
23pub struct Ident {
24    pub name: String,
25    pub span: Span,
26}
27
28#[derive(Debug, Clone)]
29pub struct Table {
30    pub name: Ident,
31    pub schema: Option<Ident>,
32    pub alias: Option<Ident>,
33    pub items: Vec<TableItem>,
34    pub settings: Vec<TableSetting>,
35    pub span: Span,
36}
37
38#[derive(Debug, Clone)]
39pub enum TableItem {
40    Column(Column),
41    Indexes(IndexesBlock),
42    Note(StringLiteral),
43}
44
45#[derive(Debug, Clone)]
46pub struct Column {
47    pub name: Ident,
48    pub col_type: String,
49    pub col_type_span: Span,
50    pub settings: Vec<ColumnSetting>,
51    pub span: Span,
52}
53
54#[derive(Debug, Clone)]
55pub enum ColumnSetting {
56    PrimaryKey,
57    NotNull,
58    Null,
59    Unique,
60    Increment,
61    Default(DefaultValue),
62    Note(StringLiteral),
63    Ref(InlineRef),
64}
65
66#[derive(Debug, Clone)]
67pub enum DefaultValue {
68    String(String),
69    Number(String),
70    Boolean(bool),
71    Expression(String),
72}
73
74#[derive(Debug, Clone)]
75pub struct InlineRef {
76    pub target_table: Ident,
77    pub target_column: Ident,
78    pub relationship: RelationshipType,
79    pub span: Span,
80}
81
82#[derive(Debug, Clone)]
83pub enum RelationshipType {
84    OneToOne,   // -
85    OneToMany,  // <
86    ManyToOne,  // >
87    ManyToMany, // <>
88}
89
90#[derive(Debug, Clone, PartialEq)]
91pub enum ReferentialAction {
92    Cascade,
93    Restrict,
94    NoAction,
95    SetNull,
96    SetDefault,
97}
98
99#[derive(Debug, Clone)]
100pub struct Ref {
101    pub name: Option<Ident>,
102    pub from_table: Ident,
103    pub from_columns: Vec<Ident>,
104    pub to_table: Ident,
105    pub to_columns: Vec<Ident>,
106    pub relationship: RelationshipType,
107    pub on_delete: Option<ReferentialAction>,
108    pub on_update: Option<ReferentialAction>,
109    pub span: Span,
110}
111
112#[derive(Debug, Clone)]
113pub struct IndexesBlock {
114    pub indexes: Vec<Index>,
115    pub span: Span,
116}
117
118#[derive(Debug, Clone)]
119pub struct Index {
120    pub columns: Vec<IndexColumn>,
121    pub settings: Vec<IndexSetting>,
122    pub span: Span,
123}
124
125#[derive(Debug, Clone)]
126pub enum IndexColumn {
127    Simple(Ident),
128    Expression(String),
129}
130
131#[derive(Debug, Clone)]
132pub enum IndexSetting {
133    PrimaryKey,
134    Unique,
135    Name(String),
136    Type(String),
137}
138
139#[derive(Debug, Clone)]
140pub struct Enum {
141    pub name: Ident,
142    pub members: Vec<EnumMember>,
143    pub span: Span,
144}
145
146#[derive(Debug, Clone)]
147pub struct EnumMember {
148    pub name: Ident,
149    pub note: Option<StringLiteral>,
150    pub span: Span,
151}
152
153#[derive(Debug, Clone)]
154pub struct Project {
155    pub name: Option<Ident>,
156    pub settings: Vec<ProjectSetting>,
157    pub span: Span,
158}
159
160#[derive(Debug, Clone)]
161pub enum ProjectSetting {
162    DatabaseType(String),
163    Note(StringLiteral),
164}
165
166#[derive(Debug, Clone)]
167pub struct TableSetting {
168    pub key: String,
169    pub value: String,
170}
171
172#[derive(Debug, Clone)]
173pub struct StringLiteral {
174    pub value: String,
175    pub span: Span,
176}