dbml_rs/ast/
schema.rs

1use alloc::vec::Vec;
2
3use super::*;
4
5/// Represents the entire structure of a parsed DBML file.
6#[derive(Debug, Clone, Default)]
7pub struct SchemaBlock<'a> {
8  /// The span range of the entire parsed DBML structure in the source text.
9  pub span_range: SpanRange,
10  /// The input source content.
11  pub input: &'a str,
12  /// A vector of top-level blocks comprising the parsed DBML structure.
13  pub blocks: Vec<TopLevelBlock>,
14}
15
16/// An enum representing various top-level blocks in a parsed DBML file.
17#[derive(Debug, Clone)]
18pub enum TopLevelBlock {
19  /// Represents a project block in the DBML file.
20  Project(ProjectBlock),
21  /// Represents a table block in the DBML file.
22  Table(TableBlock),
23  /// Represents a table group block in the DBML file.
24  TableGroup(TableGroupBlock),
25  /// Represents a note block in the DBML file.
26  Note(NoteBlock),
27  /// Represents a reference block in the DBML file.
28  Ref(RefBlock),
29  /// Represents an enum block in the DBML file.
30  Enum(EnumBlock),
31}
32
33impl<'a> SchemaBlock<'a> {
34  pub fn projects(&self) -> Vec<&ProjectBlock> {
35    self
36      .blocks
37      .iter()
38      .filter_map(|block| {
39        if let TopLevelBlock::Project(project) = block {
40          Some(project)
41        } else {
42          None
43        }
44      })
45      .collect()
46  }
47
48  pub fn tables(&self) -> Vec<&TableBlock> {
49    self
50      .blocks
51      .iter()
52      .filter_map(|block| {
53        if let TopLevelBlock::Table(table) = block {
54          Some(table)
55        } else {
56          None
57        }
58      })
59      .collect()
60  }
61
62  pub fn table_groups(&self) -> Vec<&TableGroupBlock> {
63    self
64      .blocks
65      .iter()
66      .filter_map(|block| {
67        if let TopLevelBlock::TableGroup(table_group) = block {
68          Some(table_group)
69        } else {
70          None
71        }
72      })
73      .collect()
74  }
75
76  pub fn refs(&self) -> Vec<&RefBlock> {
77    self
78      .blocks
79      .iter()
80      .filter_map(|block| {
81        if let TopLevelBlock::Ref(reference) = block {
82          Some(reference)
83        } else {
84          None
85        }
86      })
87      .collect()
88  }
89
90  pub fn enums(&self) -> Vec<&EnumBlock> {
91    self
92      .blocks
93      .iter()
94      .filter_map(|block| {
95        if let TopLevelBlock::Enum(enum_block) = block {
96          Some(enum_block)
97        } else {
98          None
99        }
100      })
101      .collect()
102  }
103}