1use std::collections::HashMap;
2
3#[derive(Parser)]
4#[grammar = "grammar.pest"]
5pub struct AstParser;
6
7#[derive(Debug, Default, Clone)]
8pub struct Ast {
9 pub imports: Vec<String>,
10 pub injects: Vec<AstCode>,
11 pub replacements: Vec<AstReplace>,
12 pub externs: Vec<AstExtern>,
13 pub structs: Vec<AstStruct>,
14 pub enums: Vec<AstEnum>,
15 pub implementations: Vec<AstImplementation>,
16}
17
18#[derive(Debug, Default, Clone)]
25pub struct AstReplace {
26 pub pattern: String,
27 pub template: AstCode,
28}
29
30#[derive(Debug, Default, Clone)]
31pub struct AstExtern {
32 pub types: Vec<String>,
33 pub implementations: Vec<(String, AstCode)>,
34}
35
36#[derive(Debug, Clone, Hash, PartialEq, Eq)]
37pub enum AstType {
38 None,
39 Extern(String),
40 Local(String),
41}
42
43impl Default for AstType {
44 fn default() -> Self {
45 Self::None
46 }
47}
48
49impl ToString for AstType {
50 fn to_string(&self) -> String {
51 match self {
52 AstType::None => "".to_owned(),
53 AstType::Extern(name) => name.to_owned(),
54 AstType::Local(name) => name.to_owned(),
55 }
56 }
57}
58
59#[derive(Debug, Default, Clone)]
60pub struct AstStruct {
61 pub tags: Vec<(String, HashMap<String, String>)>,
62 pub name: String,
63 pub fields: Vec<(String, AstType)>,
64}
65
66#[derive(Debug, Default, Clone)]
67pub struct AstEnum {
68 pub tags: Vec<(String, HashMap<String, String>)>,
69 pub name: String,
70 pub fields: Vec<String>,
71}
72
73#[derive(Debug, Default, Clone)]
74pub struct AstImplementation {
75 pub target: AstImplementationTarget,
76 pub name: String,
77 pub where_rules: Vec<AstWhereRule>,
78 pub code: AstCode,
79}
80
81#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
82pub enum AstImplementationTarget {
83 All,
84 Struct,
85 Enum,
86}
87
88impl AstImplementationTarget {
89 pub fn is_valid(&self, other: AstImplementationTarget) -> bool {
90 match (self, other) {
91 (Self::All, _) | (Self::Struct, Self::Struct) | (Self::Enum, Self::Enum) => true,
92 _ => false,
93 }
94 }
95}
96
97impl Default for AstImplementationTarget {
98 fn default() -> Self {
99 Self::All
100 }
101}
102
103#[derive(Debug, Clone)]
104pub enum AstWhereRule {
105 None,
106 Exists(String),
107 Is(AstWhereRuleIs),
108 Impl(AstWhereRuleImpl),
109}
110
111impl Default for AstWhereRule {
112 fn default() -> Self {
113 Self::None
114 }
115}
116
117#[derive(Debug, Default, Clone)]
118pub struct AstWhereRuleIs {
119 pub variable: String,
120 pub value: String,
121}
122
123#[derive(Debug, Default, Clone)]
124pub struct AstWhereRuleImpl {
125 pub container: AstIn,
126 pub implements: Vec<String>,
127}
128
129#[derive(Debug, Default, Clone)]
130pub struct AstCode(pub Vec<AstCodeChunk>);
131
132#[derive(Debug, Clone)]
133pub enum AstCodeChunk {
134 None,
135 Content(String),
136 Variable(String),
137 For(AstCodeFor),
138}
139
140impl Default for AstCodeChunk {
141 fn default() -> Self {
142 Self::None
143 }
144}
145
146#[derive(Debug, Default, Clone)]
147pub struct AstCodeMatch {
148 pub variables: Vec<String>,
149 pub container: AstIn,
150 pub where_rules: Vec<AstWhereRule>,
151 pub code: AstCode,
152}
153
154#[derive(Debug, Default, Clone)]
155pub struct AstCodeFor {
156 pub variables: Vec<String>,
157 pub container: AstIn,
158 pub where_rules: Vec<AstWhereRule>,
159 pub code: AstCode,
160}
161
162#[derive(Debug, Clone)]
163pub enum AstIn {
164 None,
165 Fields,
166 Variable(String),
167}
168
169impl Default for AstIn {
170 fn default() -> Self {
171 Self::None
172 }
173}