aam-core 2.8.1

Core types and parsing engine for the AAM configuration format
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use crate::aam::AAM;
use std::collections::{HashMap, HashSet};

#[derive(Debug, Clone, PartialEq)]
pub enum AamType {
    String,
    I32,
    F64,
    Bool,
    Color,
    List(Box<AamType>),
    Object(AamSchema),
    Custom(String),
    Unknown,
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct AamSchema {
    pub fields: HashMap<String, SchemaField>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct SchemaField {
    pub name: String,
    pub ty: AamType,
    pub optional: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Scalar(String),
    Object(HashMap<String, Value>),
    List(Vec<Value>),
}

fn clean_quotes(val: &str) -> &str {
    let trimmed = val.trim();
    if (trimmed.starts_with('"') && trimmed.ends_with('"'))
        || (trimmed.starts_with('\'') && trimmed.ends_with('\''))
    {
        if trimmed.len() >= 2 {
            &trimmed[1..trimmed.len() - 1]
        } else {
            trimmed
        }
    } else {
        trimmed
    }
}

fn to_pascal_case(s: &str) -> String {
    let mut result = String::new();
    let mut capitalize_next = true;
    for c in s.chars() {
        if c == '_' || c == '-' {
            capitalize_next = true;
        } else if capitalize_next {
            result.push(c.to_ascii_uppercase());
            capitalize_next = false;
        } else {
            result.push(c);
        }
    }
    result
}

fn split_by_comma(input: &str) -> Vec<String> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut brace_count = 0;
    let mut bracket_count = 0;
    let mut in_quote = None;
    let mut chars = input.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\\' {
            current.push(c);
            if let Some(next_c) = chars.next() {
                current.push(next_c);
            }
            continue;
        }

        if let Some(q) = in_quote {
            if c == q {
                in_quote = None;
            }
            current.push(c);
        } else {
            match c {
                '\'' | '"' => {
                    in_quote = Some(c);
                    current.push(c);
                }
                '{' => {
                    brace_count += 1;
                    current.push(c);
                }
                '}' => {
                    brace_count -= 1;
                    current.push(c);
                }
                '[' => {
                    bracket_count += 1;
                    current.push(c);
                }
                ']' => {
                    bracket_count -= 1;
                    current.push(c);
                }
                ',' => {
                    if brace_count == 0 && bracket_count == 0 {
                        parts.push(current.trim().to_string());
                        current.clear();
                    } else {
                        current.push(c);
                    }
                }
                _ => {
                    current.push(c);
                }
            }
        }
    }
    if !current.trim().is_empty() {
        parts.push(current.trim().to_string());
    }
    parts
}

fn parse_value(input: &str) -> Value {
    let input = input.trim();
    if input.starts_with('{') && input.ends_with('}') {
        let content = &input[1..input.len() - 1];
        let mut kvs = HashMap::new();
        let parts = split_by_comma(content);
        for part in parts {
            if let Some(pos) = part.find('=') {
                let key = part[..pos].trim().to_string();
                let val_str = part[pos + 1..].trim();
                if !key.is_empty() {
                    insert_hierarchical(&mut kvs, &key, parse_value(val_str));
                }
            }
        }
        Value::Object(kvs)
    } else if input.starts_with('[') && input.ends_with(']') {
        let content = &input[1..input.len() - 1];
        let parts = split_by_comma(content);
        let mut list_vals = Vec::new();
        for part in parts {
            list_vals.push(parse_value(part.trim()));
        }
        Value::List(list_vals)
    } else {
        Value::Scalar(input.to_string())
    }
}

fn insert_hierarchical(fields: &mut HashMap<String, Value>, key_path: &str, value: Value) {
    let segments: Vec<&str> = key_path.split('.').collect();
    if segments.is_empty() {
        return;
    }
    insert_hierarchical_rec(fields, &segments, value);
}

fn insert_hierarchical_rec(fields: &mut HashMap<String, Value>, segments: &[&str], value: Value) {
    if segments.len() == 1 {
        fields.insert(segments[0].to_string(), value);
        return;
    }

    let current = segments[0].to_string();
    let entry = fields
        .entry(current)
        .or_insert_with(|| Value::Object(HashMap::new()));

    if let Value::Object(sub_map) = entry {
        insert_hierarchical_rec(sub_map, &segments[1..], value);
    } else {
        let mut sub_map = HashMap::new();
        insert_hierarchical_rec(&mut sub_map, &segments[1..], value);
        *entry = Value::Object(sub_map);
    }
}

fn infer_type_for_scalar(val: &str) -> AamType {
    let trimmed = val.trim();
    if trimmed.is_empty() {
        return AamType::Unknown;
    }
    if (trimmed.starts_with('"') && trimmed.ends_with('"'))
        || (trimmed.starts_with('\'') && trimmed.ends_with('\''))
    {
        return AamType::String;
    }
    if trimmed.starts_with('#') {
        return AamType::Color;
    }
    if trimmed == "true" || trimmed == "false" || trimmed == "yes" || trimmed == "no" {
        return AamType::Bool;
    }
    if trimmed.parse::<i32>().is_ok() {
        return AamType::I32;
    }
    if trimmed.parse::<f64>().is_ok() {
        return AamType::F64;
    }
    AamType::String
}

fn infer_type_of_value(val: &Value) -> AamType {
    match val {
        Value::Scalar(s) => infer_type_for_scalar(s),
        Value::List(items) => {
            if items.is_empty() {
                AamType::List(Box::new(AamType::Unknown))
            } else {
                let element_type = infer_type_of_value(&items[0]);
                AamType::List(Box::new(element_type))
            }
        }
        Value::Object(obj) => {
            let mut fields = HashMap::new();
            for (key, sub_val) in obj {
                let ty = infer_type_of_value(sub_val);
                fields.insert(
                    key.clone(),
                    SchemaField {
                        name: key.clone(),
                        ty,
                        optional: false,
                    },
                );
            }
            AamType::Object(AamSchema { fields })
        }
    }
}

fn reconstruct_schema_from_values(instances_fields: &[&HashMap<String, Value>]) -> AamSchema {
    let mut schema = AamSchema::default();
    let mut all_keys = HashSet::new();

    for fields in instances_fields {
        for key in fields.keys() {
            all_keys.insert(key.clone());
        }
    }

    for key in all_keys {
        let mut present_values = Vec::new();
        for fields in instances_fields {
            if let Some(val) = fields.get(&key) {
                present_values.push(val);
            }
        }

        let is_optional = present_values.len() < instances_fields.len();
        let is_object = present_values.iter().any(|v| matches!(v, Value::Object(_)));
        let is_list = present_values.iter().any(|v| matches!(v, Value::List(_)));

        let ty = if is_object {
            let mut sub_maps = Vec::new();
            for val in &present_values {
                if let Value::Object(map) = val {
                    sub_maps.push(map);
                }
            }
            let sub_schema = reconstruct_schema_from_values(&sub_maps);
            AamType::Object(sub_schema)
        } else if is_list {
            let mut element_types: Vec<AamType> = Vec::new();
            for val in &present_values {
                if let Value::List(items) = val {
                    if !items.is_empty() {
                        let elem_ty = infer_type_of_value(&items[0]);
                        if !element_types.contains(&elem_ty) {
                            element_types.push(elem_ty);
                        }
                    }
                }
            }
            let final_elem_type = if element_types.len() == 1 {
                element_types.into_iter().next().unwrap()
            } else {
                AamType::String
            };
            AamType::List(Box::new(final_elem_type))
        } else {
            let mut scalar_types: Vec<AamType> = Vec::new();
            for val in &present_values {
                if let Value::Scalar(s) = val {
                    let inferred = infer_type_for_scalar(s);
                    if !scalar_types.contains(&inferred) {
                        scalar_types.push(inferred);
                    }
                }
            }

            if scalar_types.len() == 1 {
                scalar_types.into_iter().next().unwrap()
            } else if scalar_types.contains(&AamType::F64) && scalar_types.contains(&AamType::I32) {
                AamType::F64
            } else {
                AamType::String
            }
        };

        schema.fields.insert(
            key.clone(),
            SchemaField {
                name: key,
                ty,
                optional: is_optional,
            },
        );
    }

    schema
}

pub fn reconstruct_from_aam_instances(instances: &[AAM]) -> AamSchema {
    let mut structured_instances = Vec::new();

    for inst in instances {
        let mut structured = HashMap::new();
        for (k, v) in inst.iter() {
            insert_hierarchical(&mut structured, k, parse_value(v));
        }
        structured_instances.push(structured);
    }

    let refs: Vec<&HashMap<String, Value>> = structured_instances.iter().collect();
    reconstruct_schema_from_values(&refs)
}

pub fn extract_sub_schemas(
    _parent_name: &str,
    schema: &mut AamSchema,
    extracted: &mut HashMap<String, AamSchema>,
) {
    for (field_name, field) in schema.fields.iter_mut() {
        let field_ty = &mut field.ty;
        match field_ty {
            AamType::Object(sub_schema) => {
                let sub_schema_name = to_pascal_case(field_name);

                extract_sub_schemas(&sub_schema_name, sub_schema, extracted);

                extracted.insert(sub_schema_name.clone(), sub_schema.clone());

                *field_ty = AamType::Custom(sub_schema_name);
            }
            AamType::List(inner_ty) => {
                if let AamType::Object(sub_schema) = &mut **inner_ty {
                    let sub_schema_name = to_pascal_case(field_name);
                    extract_sub_schemas(&sub_schema_name, sub_schema, extracted);
                    extracted.insert(sub_schema_name.clone(), sub_schema.clone());
                    **inner_ty = AamType::Custom(sub_schema_name);
                }
            }
            _ => {}
        }
    }
}

pub fn format_type(ty: &AamType, indent: usize) -> String {
    match ty {
        AamType::String => "string".to_string(),
        AamType::I32 => "i32".to_string(),
        AamType::F64 => "f64".to_string(),
        AamType::Bool => "bool".to_string(),
        AamType::Color => "color".to_string(),
        AamType::Custom(name) => name.clone(),
        AamType::Unknown => "string".to_string(),
        AamType::List(inner) => format!("list<{}>", format_type(inner, indent)),
        AamType::Object(schema) => {
            let mut fields_str = Vec::new();
            let spaces = " ".repeat(indent + 4);
            let closing_spaces = " ".repeat(indent);

            let mut keys: Vec<&String> = schema.fields.keys().collect();
            keys.sort();

            for key in keys {
                let field = &schema.fields[key];
                let opt_sign = if field.optional { "*" } else { "" };
                let field_ty = format_type(&field.ty, indent + 4);
                fields_str.push(format!(
                    "{}{}{}: {}",
                    spaces, field.name, opt_sign, field_ty
                ));
            }

            format!("{{\n{}\n{}}}", fields_str.join("\n"), closing_spaces)
        }
    }
}

pub fn format_schema(name: &str, schema: &AamSchema) -> String {
    let mut fields_str = Vec::new();
    let mut keys: Vec<&String> = schema.fields.keys().collect();
    keys.sort();

    for key in keys {
        let field = &schema.fields[key];
        let opt_sign = if field.optional { "*" } else { "" };
        let field_ty = format_type(&field.ty, 4);
        fields_str.push(format!("    {}{}: {}", field.name, opt_sign, field_ty));
    }

    format!("@schema {} {{\n{}\n}}", name, fields_str.join("\n"))
}

pub fn format_all_schemas(main_name: &str, main_schema: &AamSchema) -> String {
    let mut extracted = HashMap::new();
    let mut main_schema_clone = main_schema.clone();

    extract_sub_schemas(main_name, &mut main_schema_clone, &mut extracted);

    let mut output_parts = Vec::new();

    let mut schema_names: Vec<&String> = extracted.keys().collect();
    schema_names.sort();

    for name in schema_names {
        let sub_schema = &extracted[name];
        output_parts.push(format_schema(name, sub_schema));
    }

    output_parts.push(format_schema(main_name, &main_schema_clone));

    output_parts.join("\n\n")
}

pub fn reconstruct_schema(schema_name: &str, file_sources: &[&str]) -> Result<String, String> {
    let mut instances = Vec::new();

    for src in file_sources {
        let aam = AAM::parse(src).map_err(|e| format!("Failed to parse configuration: {:?}", e))?;
        instances.push(aam);
    }

    let reconstructed = reconstruct_from_aam_instances(&instances);
    Ok(format_all_schemas(schema_name, &reconstructed))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::write;

    #[test]
    fn test_reconstruct_schema_helper() {
        let dir = tempfile::tempdir().unwrap();
        let base = dir.path().join("base.aam");
        let child1 = dir.path().join("child1.aam");
        let child2 = dir.path().join("child2.aam");

        write(
            &base,
            r#"@schema Player {
    name: string
}
"#,
        )
        .unwrap();

        write(
            &child1,
            r#"@derive base.aam::Player
name = "Luna"
port = 8080
info = { level = 10 }
"#,
        )
        .unwrap();

        write(
            &child2,
            r#"@derive base.aam::Player
name = "Sol"
info = { level = 15, class = "Warrior" }
"#,
        )
        .unwrap();

        let aam1 = AAM::load(&child1).unwrap();
        let aam2 = AAM::load(&child2).unwrap();

        let reconstructed = reconstruct_from_aam_instances(&[aam1, aam2]);
        let result = format_all_schemas("Player", &reconstructed);

        assert!(result.contains("@schema Player {"));
        assert!(result.contains("name: string"));
        assert!(result.contains("port*: i32"));
        assert!(result.contains("info: Info"));
        assert!(result.contains("@schema Info {"));
        assert!(result.contains("level: i32"));
        assert!(result.contains("class*: string"));
    }
}