Skip to content

Commit db54511

Browse files
committed
feat: update BundleInfo to OCABundleInfo to get rid of Bundle and cache info
1 parent a9784b8 commit db54511

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

src/lib.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
//! - Validate data against OCA Bundle.
1010
//! - Traverse through OCA Bundle attributes.
1111
pub mod data_validator;
12-
pub use oca_ast_semantics::ast::{AttributeType, NestedAttrType, OverlayType, RefValue, recursive_attributes::NestedAttrTypeFrame};
12+
pub use oca_ast_semantics::ast::{
13+
recursive_attributes::NestedAttrTypeFrame, AttributeType, NestedAttrType,
14+
OverlayType, RefValue,
15+
};
1316

1417
/// Performs semantic validation of an `OCABundle` and returns a status
1518
/// indicating whether the validation succeeded or failed, along with any associated errors.
@@ -57,17 +60,17 @@ pub use oca_bundle_semantics::{
5760
controller::load_oca as load,
5861
state::{
5962
attribute::Attribute,
60-
oca::{OCABox, OCABundle, overlay},
63+
oca::{overlay, OCABox, OCABundle},
6164
validator::{SemanticValidationStatus, Validator as OCAValidator},
6265
},
6366
};
64-
pub use oca_rs::facade::{Facade, build::{build_from_ocafile, parse_oca_bundle_to_ocafile}};
65-
use oca_rs::{
66-
facade::bundle::Bundle, EncodeBundle, HashFunctionCode,
67-
SerializationFormats,
67+
pub use oca_rs::facade::{
68+
build::{build_from_ocafile, parse_oca_bundle_to_ocafile},
69+
Facade,
6870
};
71+
use oca_rs::{EncodeBundle, HashFunctionCode, SerializationFormats};
6972
use std::collections::HashMap;
70-
pub use transformation_file::state::Transformation;
73+
use std::sync::OnceLock;
7174

7275
pub trait ToJSON {
7376
fn get_json_bundle(&self) -> String;
@@ -83,45 +86,42 @@ impl ToJSON for OCABundle {
8386
}
8487

8588
pub trait WithInfo {
86-
fn info(&self) -> BundleInfo;
89+
fn info(&self) -> &OCABundleInfo;
8790
}
8891

89-
impl WithInfo for Bundle {
90-
fn info(&self) -> BundleInfo {
91-
BundleInfo::new(self.clone())
92+
impl WithInfo for OCABundle {
93+
fn info(&self) -> &OCABundleInfo {
94+
static CACHE: OnceLock<OCABundleInfo> = OnceLock::new();
95+
CACHE.get_or_init(|| OCABundleInfo::new(self))
9296
}
9397
}
9498

95-
pub struct BundleInfo {
96-
pub attributes: HashMap<String, Attribute>,
99+
pub struct OCABundleInfo {
100+
attributes: HashMap<String, Attribute>,
97101
pub meta: HashMap<String, HashMap<String, String>>,
98-
pub links: Vec<Transformation>,
99-
pub framing: Vec<String>,
102+
pub links: Vec<overlay::Link>,
103+
pub framing: Vec<overlay::AttributeFraming>,
100104
}
101105

102-
impl BundleInfo {
103-
pub fn new(bundle: Bundle) -> Self {
104-
let mut attributes = HashMap::new();
106+
impl OCABundleInfo {
107+
pub fn new(bundle: &OCABundle) -> Self {
105108
let mut meta = HashMap::new();
106-
if let Some(structural_bundle) = bundle.structural {
107-
let structural_box = OCABox::from(structural_bundle.clone());
108-
if let Some(m) = structural_box.meta {
109-
m.iter().for_each(|(k, v)| {
110-
meta.insert(k.to_639_3().to_string(), v.to_owned());
111-
})
112-
}
113-
attributes = structural_box.attributes;
109+
let oca_box = OCABox::from(bundle.clone());
110+
if let Some(m) = oca_box.meta {
111+
m.iter().for_each(|(k, v)| {
112+
meta.insert(k.to_639_3().to_string(), v.to_owned());
113+
})
114114
}
115115

116116
Self {
117-
attributes,
117+
attributes: oca_box.attributes,
118118
meta,
119-
links: bundle.transformations,
119+
links: vec![],
120120
framing: vec![],
121121
}
122122
}
123123

124-
pub fn attributes(&self) -> impl Iterator<Item = &Attribute>{
124+
pub fn attributes(&self) -> impl Iterator<Item = &Attribute> {
125125
self.attributes.values()
126126
}
127127

tests/captured_data_validation.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use oca_sdk_rs::{
22
build_from_ocafile,
33
data_validator::{validate_data, DataValidationStatus},
4-
load, validate_semantics, SemanticValidationStatus, ToJSON,
4+
load, validate_semantics, SemanticValidationStatus, ToJSON, WithInfo,
55
};
66
use std::fs;
77
use std::path::Path;
@@ -18,6 +18,9 @@ fn building_from_ocafile() -> Result<(), Box<dyn std::error::Error>> {
1818
"EKHBds6myKVIsQuT7Zr23M8Xk_gwq-2SaDRUprvqOXxa"
1919
);
2020

21+
oca_bundle.info().attributes().for_each(|attr| {
22+
println!("{:?}", attr);
23+
});
2124
println!("{}", oca_bundle.get_json_bundle());
2225

2326
Ok(())

0 commit comments

Comments
 (0)