1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// SPDX-License-Identifier: Apache-2.0
use crate::model::config_model::{ConfigModel, ConfigValue};
use crate::model::source_location::SourceLocation;
use crate::parser::cola_actions::{
CodeBlock, Cola, Entity, FieldList, FieldValue, MarkdownItem, NestedBlock,
};
use std::path::PathBuf;
/// Builds a ConfigModel from a parsed Cola AST
pub struct ModelBuilder;
impl ModelBuilder {
/// Convert a Cola AST to a ConfigModel
pub fn build_config_model(cola: &Cola) -> Result<ConfigModel, String> {
let mut model = ConfigModel::new();
let root_id = model.root_id();
if let Some(markdown_items) = cola {
for markdown_item in markdown_items {
// Ignore non-cola code blocks, headings, paragraphs.
if let MarkdownItem::CodeBlock(CodeBlock::ColaCodeBlock(cola_block)) = markdown_item
&& let Some(entities) = &cola_block.cola_syntax
{
for entity in entities {
Self::process_entity(&mut model, root_id, "", entity)?;
}
}
}
}
Ok(model)
}
/// Process an entity and add it to the ConfigModel
fn process_entity(
model: &mut ConfigModel,
_parent_id: usize,
parent_path: &str,
entity: &Entity,
) -> Result<(), String> {
match entity {
Entity::SingularEntity(singular) => {
// Create entity path - get the identifier string
// We need to handle this differently since we can't directly access ValLoc fields
let identifier = &singular.identifier;
// Extract the string value
let entity_name = identifier.as_ref().trim();
let path = if parent_path.is_empty() {
entity_name.to_string()
} else {
format!("{}/{}", parent_path, entity_name)
};
// Extract source location from the rustemo ValLoc object
let location = singular.location.as_ref().map(|loc| {
// Convert rustemo Location to our SourceLocation
// Extract start position (line, column)
let (start_line, start_column) = match &loc.start {
rustemo::Position::LineBased(lc) => (lc.line, lc.column),
rustemo::Position::Position(_) => (1, 0), // Fallback for byte offset position
};
// Extract end position (line, column) if available
let (end_line, end_column) = if let Some(end) = &loc.end {
match end {
rustemo::Position::LineBased(lc) => (lc.line, lc.column),
rustemo::Position::Position(_) => (start_line, start_column), // Fallback
}
} else {
(start_line, start_column) // Default to start position if end is not available
};
SourceLocation {
file_path: PathBuf::new(), // We may not have a file path in the Location
start_line: start_line as u32,
start_column: start_column as u32,
end_line: end_line as u32,
end_column: end_column as u32,
}
});
// Create the entity at this path
let entity_id =
model.create_entity_at_path(parent_path, entity_name, None, location)?;
// Process entity contents
Self::process_entity_definition(
model,
entity_id,
&path,
&singular.entity_definition,
)?;
Ok(())
}
Entity::PluralEntity(plural) => {
// Create entity path - extract the identifiers
let id1 = &plural.identifier_1;
let id3 = &plural.identifier_3;
// Extract string values
let entity_name = id1.as_ref().trim();
let plural_name = id3.as_ref().trim();
let path = if parent_path.is_empty() {
entity_name.to_string()
} else {
format!("{}/{}", parent_path, entity_name)
};
// Extract source location from the rustemo ValLoc object
let location = plural.location.as_ref().map(|loc| {
// Convert rustemo Location to our SourceLocation
// Extract start position (line, column)
let (start_line, start_column) = match &loc.start {
rustemo::Position::LineBased(lc) => (lc.line, lc.column),
rustemo::Position::Position(_) => (1, 0), // Fallback for byte offset position
};
// Extract end position (line, column) if available
let (end_line, end_column) = if let Some(end) = &loc.end {
match end {
rustemo::Position::LineBased(lc) => (lc.line, lc.column),
rustemo::Position::Position(_) => (start_line, start_column), // Fallback
}
} else {
(start_line, start_column) // Default to start position if end is not available
};
SourceLocation {
file_path: PathBuf::new(), // We may not have a file path in the Location
start_line: start_line as u32,
start_column: start_column as u32,
end_line: end_line as u32,
end_column: end_column as u32,
}
});
// Create the entity at this path with plural name
let entity_id = model.create_entity_at_path(
parent_path,
entity_name,
Some(plural_name),
location,
)?;
// Process entity contents
Self::process_entity_definition(
model,
entity_id,
&path,
&plural.entity_definition,
)?;
Ok(())
}
}
}
/// Process the contents of an entity definition
fn process_entity_definition(
model: &mut ConfigModel,
entity_id: usize,
entity_path: &str,
entity_def: &Option<Vec<NestedBlock>>,
) -> Result<(), String> {
if let Some(nested_blocks) = entity_def {
for nested_block in nested_blocks {
match nested_block {
NestedBlock::FieldList(field_list) => {
Self::process_field_list(model, entity_id, field_list)?;
}
NestedBlock::Entity(entity) => {
Self::process_entity(model, entity_id, entity_path, entity)?;
}
}
}
}
Ok(())
}
/// Process a field list and add fields to the entity
fn process_field_list(
model: &mut ConfigModel,
entity_id: usize,
field_list: &FieldList,
) -> Result<(), String> {
match field_list {
FieldList::Field(field) => {
Self::add_field_to_entity(model, entity_id, field)?;
}
FieldList::C2(field_list_c2) => {
Self::process_field_list(model, entity_id, &field_list_c2.field_list)?;
Self::add_field_to_entity(model, entity_id, &field_list_c2.field)?;
}
}
Ok(())
}
/// Add a field to an entity in the model
fn add_field_to_entity(
model: &mut ConfigModel,
entity_id: usize,
field: &crate::cola_actions::Field,
) -> Result<(), String> {
// Extract field name from identifier
let id = &field.identifier;
let field_name = id.as_ref().trim().to_string();
// Extract source location from the field
let location = field.location.as_ref().map(|loc| {
// Convert rustemo Location to our SourceLocation
// Extract start position (line, column)
let (start_line, start_column) = match &loc.start {
rustemo::Position::LineBased(lc) => (lc.line, lc.column),
rustemo::Position::Position(_) => (1, 0), // Fallback for byte offset position
};
// Extract end position (line, column) if available
let (end_line, end_column) = if let Some(end) = &loc.end {
match end {
rustemo::Position::LineBased(lc) => (lc.line, lc.column),
rustemo::Position::Position(_) => (start_line, start_column), // Fallback
}
} else {
(start_line, start_column) // Default to start position if end is not available
};
SourceLocation {
file_path: PathBuf::new(), // We may not have a file path in the Location
start_line: start_line as u32,
start_column: start_column as u32,
end_line: end_line as u32,
end_column: end_column as u32,
}
});
// Pass field_value to be converted
let field_value = Self::convert_field_value(&field.field_value)?;
// Add field with source location to the entity
model.add_field_with_location(entity_id, &field_name, field_value, location)?;
Ok(())
}
/// Convert a FieldValue from the AST to a ConfigValue for the model
fn convert_field_value(field_value: &FieldValue) -> Result<ConfigValue, String> {
match field_value {
FieldValue::QuotedStringDouble(s) => {
// Extract string and remove surrounding quotes
let s_val = s.as_ref().trim();
let content = s_val[1..s_val.len() - 1].to_string();
Ok(ConfigValue::String(content))
}
FieldValue::QuotedStringSingle(s) => {
// Extract string and remove surrounding quotes
let s_val = s.as_ref().trim();
let content = s_val[1..s_val.len() - 1].to_string();
Ok(ConfigValue::String(content))
}
FieldValue::Number(n) => {
let n_str = n.as_ref().trim();
if n_str.contains('.') {
// Float value
match n_str.parse::<f64>() {
Ok(f) => Ok(ConfigValue::Float(f)),
Err(_) => Err(format!("Failed to parse float: {}", n_str)),
}
} else {
// Integer value
match n_str.parse::<i64>() {
Ok(i) => Ok(ConfigValue::Integer(i)),
Err(_) => Err(format!("Failed to parse integer: {}", n_str)),
}
}
}
FieldValue::BooleanTrue => Ok(ConfigValue::Boolean(true)),
FieldValue::BooleanFalse => Ok(ConfigValue::Boolean(false)),
}
}
}