oca-bundle 2.0.0

Rust implementation of Overlays Capture Architecture bundle
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use crate::state::oca_bundle::OCABundleModel;
use crate::state::oca_bundle::overlay::OverlayModel;
use log::info;
use oca_ast::ast;
use oca_ast::ast::NestedValue;
use overlay_file::OverlayDef;

/// OCABuild represents a build process of an OCA bundle from OCA AST.
/// It contains the final OCA bundle and a list of steps that were applied to create it.
#[derive(Debug)]
pub struct OCABuild {
    pub oca_bundle: OCABundleModel,
    pub steps: Vec<OCABuildStep>,
}

#[derive(Debug)]
pub struct OCABuildStep {
    pub parent_said: Option<said::SelfAddressingIdentifier>,
    pub command: ast::Command,
    pub result: OCABundleModel,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct FromASTError {
    pub line_number: usize,
    pub raw_line: String,
    pub message: String,
}

#[derive(thiserror::Error, Debug, Clone, serde::Serialize)]
#[serde(untagged)]
pub enum Error {
    #[error("Error at line {line_number} ({raw_line}): {message}")]
    FromASTError {
        #[serde(rename = "ln")]
        line_number: usize,
        #[serde(rename = "c")]
        raw_line: String,
        #[serde(rename = "e")]
        message: String,
    },
}

/// Create a new OCA build from OCA AST
pub fn from_ast(
    from_bundle: Option<OCABundleModel>,
    oca_ast: &ast::OCAAst,
) -> Result<OCABuild, Vec<Error>> {
    let mut errors = vec![];
    let mut steps = vec![];
    let mut parent_said: Option<said::SelfAddressingIdentifier> = match &from_bundle {
        Some(oca_bundle) => oca_bundle.digest.clone(),
        None => None,
    };
    let has_from_bundle = from_bundle.is_some();

    let mut oca_bundle = from_bundle.unwrap_or_default();

    let default_command_meta = ast::CommandMeta {
        line_number: 0,
        raw_line: "unknown".to_string(),
    };
    for (i, command) in oca_ast.commands.iter().enumerate() {
        let command_index = if has_from_bundle { i + 1 } else { i };

        // todo pass the references
        let command_meta = oca_ast
            .commands_meta
            .get(&command_index)
            .unwrap_or(&default_command_meta);

        match apply_command(&mut oca_bundle, command.clone()) {
            Ok(oca_bundle) => {
                /* if oca_bundle.said == parent_said {
                    errors.push(Error::FromASTError {
                        line_number: command_meta.line_number,
                        raw_line: command_meta.raw_line.clone(),
                        message: "Applying command failed".to_string(),
                    });
                } else { */
                steps.push(OCABuildStep {
                    parent_said: parent_said.clone(),
                    command: command.clone(),
                    result: oca_bundle.clone(),
                });
                parent_said.clone_from(&oca_bundle.digest);
            }
            Err(mut err) => {
                errors.extend(err.iter_mut().map(|e| Error::FromASTError {
                    line_number: command_meta.line_number,
                    raw_line: command_meta.raw_line.clone(),
                    message: e.clone(),
                }));
            }
        }
    }
    if errors.is_empty() {
        Ok(OCABuild { oca_bundle, steps })
    } else {
        Err(errors)
    }
}

pub fn apply_command(
    base: &mut OCABundleModel,
    op: ast::Command,
) -> Result<&OCABundleModel, Vec<String>> {
    let mut errors = vec![];

    match (op.kind, op.object_kind) {
        (ast::CommandType::From, _) => {
            errors.push(
                "Unsupported FROM command, it should be resolved before applying commands"
                    .to_string(),
            );
        }
        (ast::CommandType::Add, ast::ObjectKind::CaptureBase(content)) => {
            if let Some(ref attributes) = content.attributes {
                base.capture_base.attributes.extend(attributes.clone());
            }
        }
        (ast::CommandType::Add, ast::ObjectKind::Overlay(content)) => {
            let mut overlay = OverlayModel::new(content.clone());
            overlay.overlay_def = Some(content.overlay_def);
            if let Err(err) = merge_or_add_overlay(base, overlay) {
                errors.push(err);
            }
        }
        (ast::CommandType::Add, ast::ObjectKind::OCABundle(_)) => todo!(),
        (ast::CommandType::Remove, ast::ObjectKind::CaptureBase(content)) => {
            if let Some(ref attributes) = content.attributes {
                for (attr_name, _) in attributes {
                    base.remove_attribute(attr_name);
                }
            }
        }
        (ast::CommandType::Remove, ast::ObjectKind::OCABundle(_)) => todo!(),
        (ast::CommandType::Remove, ast::ObjectKind::Overlay(_)) => todo!(),
        (ast::CommandType::Modify, ast::ObjectKind::CaptureBase(_)) => todo!(),
        (ast::CommandType::Modify, ast::ObjectKind::OCABundle(_)) => todo!(),
        (ast::CommandType::Modify, ast::ObjectKind::Overlay(_)) => todo!(),
    }
    // Calculate and fill digest for bundle, capture base and overlays
    match base.compute_and_fill_digest() {
        Ok(_) => info!("Digests filled successfully"),
        Err(e) => return Err(vec![format!("Error filling digests: {}", e)]),
    }
    base.fill_attributes();
    if errors.is_empty() {
        Ok(base)
    } else {
        Err(errors)
    }
}

fn merge_or_add_overlay(base: &mut OCABundleModel, incoming: OverlayModel) -> Result<(), String> {
    let overlay_def = incoming
        .overlay_def
        .as_ref()
        .ok_or_else(|| "Overlay definition missing".to_string())?;
    if overlay_def.unique_keys.is_empty() {
        base.overlays.push(incoming);
        return Ok(());
    }

    let incoming_properties = incoming.properties.as_ref().ok_or_else(|| {
        format!(
            "Overlay {} is missing properties for unique keys",
            overlay_def.get_full_name()
        )
    })?;

    let signature = unique_keys_signature(overlay_def, incoming_properties)?;
    let incoming_name = overlay_def.get_full_name();

    for existing in &mut base.overlays {
        let existing_def = match &existing.overlay_def {
            Some(def) => def,
            None => continue,
        };
        if existing_def.get_full_name() != incoming_name {
            continue;
        }
        let existing_props = match existing.properties.as_mut() {
            Some(props) => props,
            None => {
                return Err(format!(
                    "Overlay {} is missing properties for unique keys",
                    existing_def.get_full_name()
                ));
            }
        };
        let existing_signature = unique_keys_signature(existing_def, existing_props)?;
        if existing_signature != signature {
            continue;
        }

        // Merge properties for same overlay + unique signature.
        merge_properties(existing_props, incoming_properties)?;
        return Ok(());
    }

    base.overlays.push(incoming);
    Ok(())
}

fn unique_keys_signature(
    overlay_def: &OverlayDef,
    properties: &indexmap::IndexMap<String, NestedValue>,
) -> Result<String, String> {
    let mut parts = Vec::new();
    let mut missing = Vec::new();
    for key in &overlay_def.unique_keys {
        match properties.get(key) {
            Some(value) => {
                let value_str = serde_json::to_string(value).unwrap_or_else(|_| value.to_string());
                parts.push(format!("{}={}", key, value_str));
            }
            None => missing.push(key.clone()),
        }
    }
    if !missing.is_empty() {
        return Err(format!(
            "Overlay {} is missing unique keys: {}",
            overlay_def.get_full_name(),
            missing.join(", ")
        ));
    }
    Ok(parts.join("|"))
}

fn merge_properties(
    target: &mut indexmap::IndexMap<String, NestedValue>,
    incoming: &indexmap::IndexMap<String, NestedValue>,
) -> Result<(), String> {
    for (key, value) in incoming {
        match target.get_mut(key) {
            Some(existing) => merge_nested_value(existing, value, key)?,
            None => {
                target.insert(key.clone(), value.clone());
            }
        }
    }
    Ok(())
}

fn merge_nested_value(
    target: &mut NestedValue,
    incoming: &NestedValue,
    path: &str,
) -> Result<(), String> {
    match target {
        NestedValue::Object(target_obj) => match incoming {
            NestedValue::Object(incoming_obj) => {
                for (key, value) in incoming_obj.iter() {
                    if let Some(existing) = target_obj.get_mut(key) {
                        let nested_path = format!("{path}.{key}");
                        merge_nested_value(existing, value, &nested_path)?;
                    } else {
                        target_obj.insert(key.clone(), value.clone());
                    }
                }
                Ok(())
            }
            _ => {
                if target == incoming {
                    Ok(())
                } else {
                    Err(format!(
                        "Overlay attribute override for {path}: existing value differs"
                    ))
                }
            }
        },
        _ => {
            if target == incoming {
                Ok(())
            } else {
                Err(format!(
                    "Overlay attribute override for {path}: existing value differs"
                ))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::io::Error;

    use crate::state::oca_bundle::OCABundle;

    use super::*;
    use indexmap::IndexMap;
    use oca_ast::ast::{AttributeType, CaptureContent};
    use oca_file::ocafile::parse_from_string;
    use overlay_file::overlay_registry::{OverlayLocalRegistry, OverlayRegistry};

    #[test]
    fn test_add_step() -> Result<(), Box<dyn std::error::Error>> {
        let _ = env_logger::builder().is_test(true).try_init();
        let mut commands = vec![];

        let registry = OverlayLocalRegistry::from_dir("../overlay-file/core_overlays/")?;

        let mut attributes = IndexMap::new();
        attributes.insert(
            "d".to_string(),
            ast::NestedAttrType::Value(AttributeType::Text),
        );
        attributes.insert(
            "i".to_string(),
            ast::NestedAttrType::Value(AttributeType::Text),
        );
        attributes.insert(
            "passed".to_string(),
            ast::NestedAttrType::Value(AttributeType::Boolean),
        );
        commands.push(ast::Command {
            kind: ast::CommandType::Add,
            object_kind: ast::ObjectKind::CaptureBase(CaptureContent {
                attributes: Some(attributes),
            }),
        });

        let mut properties = IndexMap::new();
        properties.insert(
            "language".to_string(),
            ast::NestedValue::Value("en".to_string()),
        );
        properties.insert(
            "name".to_string(),
            ast::NestedValue::Value("Entrance credential".to_string()),
        );
        properties.insert(
            "description".to_string(),
            ast::NestedValue::Value("Entrance credential".to_string()),
        );
        let meta_ov_def = registry.get_overlay("Meta/2.0.0").unwrap();
        commands.push(ast::Command {
            kind: ast::CommandType::Add,
            object_kind: ast::ObjectKind::Overlay(ast::OverlayContent {
                properties: Some(properties),
                overlay_def: meta_ov_def.clone(),
            }),
        });

        let mut properties = IndexMap::new();
        properties.insert(
            "d".to_string(),
            ast::NestedValue::Value("Schema digest".to_string()),
        );
        properties.insert(
            "i".to_string(),
            ast::NestedValue::Value("Credential Issuee".to_string()),
        );
        properties.insert(
            "passed".to_string(),
            ast::NestedValue::Value("Passed".to_string()),
        );
        let mut attr_labels = IndexMap::new();
        attr_labels.insert(
            "language".to_string(),
            ast::NestedValue::Value("en".to_string()),
        );
        attr_labels.insert(
            "attribute_labels".to_string(),
            ast::NestedValue::Object(properties.clone()),
        );
        let label_ov_def = registry.get_overlay("Label/2.0.0").unwrap();
        commands.push(ast::Command {
            kind: ast::CommandType::Add,
            object_kind: ast::ObjectKind::Overlay(ast::OverlayContent {
                properties: Some(attr_labels),
                overlay_def: label_ov_def.clone(),
            }),
        });

        let mut attributes = IndexMap::new();
        attributes.insert(
            "d".to_string(),
            ast::NestedValue::Value("Schema digest".to_string()),
        );
        attributes.insert(
            "i".to_string(),
            ast::NestedValue::Value("Credential Issuee".to_string()),
        );
        attributes.insert(
            "passed".to_string(),
            ast::NestedValue::Value("Enables or disables passing".to_string()),
        );
        let mut properties = IndexMap::new();
        properties.insert(
            "language".to_string(),
            ast::NestedValue::Value("en".to_string()),
        );

        let mut properties = IndexMap::new();
        properties.insert(
            "d".to_string(),
            ast::NestedValue::Value("utf-8".to_string()),
        );
        properties.insert(
            "i".to_string(),
            ast::NestedValue::Value("utf-8".to_string()),
        );
        properties.insert(
            "passed".to_string(),
            ast::NestedValue::Value("utf-8".to_string()),
        );
        let character_encoding_ov_def = registry.get_overlay("Character_Encoding/2.0.0").unwrap();
        commands.push(ast::Command {
            kind: ast::CommandType::Add,
            object_kind: ast::ObjectKind::Overlay(ast::OverlayContent {
                properties: Some(properties),
                overlay_def: character_encoding_ov_def.clone(),
            }),
        });

        let mut properties = IndexMap::new();
        properties.insert("d".to_string(), ast::NestedValue::Value("M".to_string()));
        properties.insert("i".to_string(), ast::NestedValue::Value("M".to_string()));
        properties.insert(
            "passed".to_string(),
            ast::NestedValue::Value("M".to_string()),
        );
        let conformance_ov_def = registry.get_overlay("Conformance/2.0.0").unwrap();
        commands.push(ast::Command {
            kind: ast::CommandType::Add,
            object_kind: ast::ObjectKind::Overlay(ast::OverlayContent {
                properties: Some(properties),
                overlay_def: conformance_ov_def.clone(),
            }),
        });

        // todo test if references with name are working
        let mut base = OCABundleModel::default();

        for command in commands {
            match apply_command(&mut base, command.clone()) {
                Ok(oca) => {
                    println!("Bundle: {}", serde_json::to_string_pretty(oca)?);
                }
                Err(errors) => {
                    println!("Error applying command: {:?}", errors);
                    return Err(Box::new(Error::other(format!("{:?}", errors))));
                }
            }
        }
        assert_eq!(
            base.attributes.unwrap().len(),
            3,
            "Expected 5 attributes in the base after applying commands"
        );
        // TODO check if overlays are created correctly
        // let mut oca = apply_command(base, command);
        // base = Some(oca);
        Ok(())
    }

    #[test]
    fn merge_label_overlays_conflict_should_error_and_keep_single_overlay() {
        let registry = OverlayLocalRegistry::from_dir("../overlay-file/core_overlays/").unwrap();
        let ocafile = r#"
ADD ATTRIBUTE first_name=Text last_name=Text

ADD OVERLAY LABEL
  language="en"
  attribute_labels
    first_name="First Name"
    last_name="Last Name"

ADD OVERLAY LABEL
  language="en"
  attribute_labels
    first_name="Name"
    last_name="Name"
"#;

        let oca_ast = parse_from_string(ocafile.to_string(), &registry).unwrap();
        let mut base = OCABundleModel::default();
        let mut errors = Vec::new();
        for command in oca_ast.commands {
            if let Err(mut err) = apply_command(&mut base, command) {
                errors.append(&mut err);
            }
        }

        assert!(!errors.is_empty());
        assert_eq!(base.overlays.len(), 1);
    }

    #[test]
    fn merge_label_overlays_disjoint_should_merge() {
        let registry = OverlayLocalRegistry::from_dir("../overlay-file/core_overlays/").unwrap();
        let ocafile = r#"
ADD ATTRIBUTE first_name=Text last_name=Text description=Text info=Text

ADD OVERLAY LABEL
  language="en"
  attribute_labels
    first_name="First Name"
    last_name="Last Name"

ADD OVERLAY LABEL
  language="en"
  attribute_labels
    description="Name"
    info="Name"
"#;

        let oca_ast = parse_from_string(ocafile.to_string(), &registry).unwrap();
        let mut base = OCABundleModel::default();
        let mut errors = Vec::new();
        for command in oca_ast.commands {
            if let Err(mut err) = apply_command(&mut base, command) {
                errors.append(&mut err);
            }
        }

        assert!(errors.is_empty());
        assert_eq!(base.overlays.len(), 1);

        let properties = base.overlays[0].properties.as_ref().unwrap();
        let labels = properties.get("attribute_labels").unwrap();
        match labels {
            NestedValue::Object(map) => {
                assert!(map.contains_key("first_name"));
                assert!(map.contains_key("last_name"));
                assert!(map.contains_key("description"));
                assert!(map.contains_key("info"));
            }
            _ => panic!("attribute_labels should be an object"),
        }
    }

    #[test]
    fn build_from_ast() {
        let registry = OverlayLocalRegistry::from_dir("../overlay-file/core_overlays").unwrap();

        let unparsed_file = r#"
-- version=2.0.0
-- name=プラスウルトラ
ADD ATTRIBUTE remove=Text
ADD ATTRIBUTE name=Text age=Numeric car=[refs:EJeWVGxkqxWrdGi0efOzwg1YQK8FrA-ZmtegiVEtAVcu]
REMOVE ATTRIBUTE remove
ADD ATTRIBUTE incidentals_spare_parts=[[refs:EJeWVGxkqxWrdGi0efOzwg1YQK8FrA-ZmtegiVEtAVcu]]
ADD ATTRIBUTE d=Text i=Text passed=Boolean
ADD Overlay META
  language="en"
  description="Entrance credential"
  name="Entrance credential"
ADD Overlay CHARACTER_ENCODING
  attribute_character_encodings
    d="utf-8"
    i="utf-8"
    passed="utf-8"
ADD Overlay CONFORMANCE
  attribute_conformances=["d", "i", "passed"]
ADD Overlay LABEL
  language="en"
  attribute_labels
    d="Schema digest"
    i="Credential Issuee"
    passed="Passed"
ADD Overlay FORMAT
  attribute_formats
    d="image/jpeg"
ADD Overlay UNIT
  metric_system="SI"
  attribute_units
    i="m^2"
    d="°"
ADD ATTRIBUTE list=[Text] el=Text
ADD Overlay CARDINALITY
  attribute_cardinalities
    list="1-2"
ADD Overlay ENTRY_CODE
  attribute_entry_codes
    list=refs:EJeWVGxkqxWrdGi0efOzwg1YQK8FrA-ZmtegiVEtAVcu
    el=["o1", "o2", "o3"]
ADD Overlay ENTRY
  language="en"
  attribute_entries
    list=refs:EJeWVGxkqxWrdGi0efOzwg1YQK8FrA-ZmtegiVEtAVcu
    el
     o1="o1_label"
     o2="o2_label"
     o3="o3_label"
"#;
        let oca_ast = parse_from_string(unparsed_file.to_string(), &registry).unwrap();

        let oca_bundle = OCABundleModel::default();

        let mut oca_bundle_model = from_ast(Some(oca_bundle), &oca_ast).unwrap().oca_bundle;
        let _ = oca_bundle_model.compute_and_fill_digest();
        assert_eq!(oca_bundle_model.overlays.len(), 9);
        assert_eq!(oca_bundle_model.capture_base.attributes.len(), 9);
        assert!(oca_bundle_model.digest.is_some());
        let oca_bundle = OCABundle::from(oca_bundle_model);
        let overlay_name = oca_bundle.overlays.first().unwrap().model.name.clone();
        assert_eq!(overlay_name, "meta");
        // let json = serde_json::to_string_pretty(&oca_bundle).unwrap();
        // println!(" >>> Bundle: {}", json);
    }
}