Skip to content

Commit 4260664

Browse files
committed
feat: implement ToJSON trait for OCABundle to simplify JSON serialization
1 parent 4f6bf4d commit 4260664

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,31 @@ pub use oca_bundle_semantics::{
5757
controller::load_oca as load,
5858
state::{
5959
attribute::{Attribute, AttributeType},
60-
oca::{OCABox as StructuralBox, OCABundle as StructuralBundle},
60+
oca::{OCABox, OCABundle},
6161
validator::{SemanticValidationStatus, Validator as OCAValidator},
6262
},
6363
};
6464
pub use oca_rs::facade::build::build_from_ocafile;
65-
use oca_rs::facade::bundle::Bundle;
65+
use oca_rs::{
66+
facade::bundle::Bundle, EncodeBundle, HashFunctionCode,
67+
SerializationFormats,
68+
};
6669
use std::collections::HashMap;
6770
pub use transformation_file::state::Transformation;
6871

72+
pub trait ToJSON {
73+
fn get_json_bundle(&self) -> String;
74+
}
75+
76+
impl ToJSON for OCABundle {
77+
fn get_json_bundle(&self) -> String {
78+
let code = HashFunctionCode::Blake3_256;
79+
let format = SerializationFormats::JSON;
80+
81+
String::from_utf8(self.encode(&code, &format).unwrap()).unwrap()
82+
}
83+
}
84+
6985
pub trait WithInfo {
7086
fn info(&self) -> BundleInfo;
7187
}
@@ -88,7 +104,7 @@ impl BundleInfo {
88104
let mut attributes = HashMap::new();
89105
let mut meta = HashMap::new();
90106
if let Some(structural_bundle) = bundle.structural {
91-
let structural_box = StructuralBox::from(structural_bundle.clone());
107+
let structural_box = OCABox::from(structural_bundle.clone());
92108
if let Some(m) = structural_box.meta {
93109
m.iter().for_each(|(k, v)| {
94110
meta.insert(k.to_639_3().to_string(), v.to_owned());

tests/captured_data_validation.rs

Lines changed: 3 additions & 2 deletions
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,
4+
load, validate_semantics, SemanticValidationStatus, ToJSON,
55
};
66
use std::fs;
77
use std::path::Path;
@@ -14,10 +14,11 @@ fn building_from_ocafile() -> Result<(), Box<dyn std::error::Error>> {
1414

1515
let oca_bundle = build_from_ocafile(ocafile_str).unwrap();
1616
assert_eq!(
17-
oca_bundle.said.unwrap().to_string(),
17+
oca_bundle.said.clone().unwrap().to_string(),
1818
"EKHBds6myKVIsQuT7Zr23M8Xk_gwq-2SaDRUprvqOXxa"
1919
);
2020

21+
println!("{}", oca_bundle.get_json_bundle());
2122

2223
Ok(())
2324
}

0 commit comments

Comments
 (0)