Skip to content

Commit

Permalink
feat: replace BundleElement with OCABundle in build_from_ocafile func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
olichwiruk committed Dec 18, 2024
1 parent 67df3be commit fcff7d0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 220 deletions.
37 changes: 10 additions & 27 deletions oca/src/facade/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::bundle::BundleElement;
use super::Facade;
use crate::data_storage::{DataStorage, Namespace};
use crate::facade::fetch::get_oca_bundle;
Expand All @@ -25,6 +24,8 @@ use said::sad::SerializationFormats;
pub enum Error {
#[error("Validation error")]
ValidationError(Vec<ValidationError>),
#[error("Deprecated")]
Deprecated,
}

#[derive(thiserror::Error, Debug, serde::Serialize)]
Expand Down Expand Up @@ -64,21 +65,15 @@ impl References for Box<dyn DataStorage> {
}
}

pub fn build_from_ocafile(ocafile: String) -> Result<BundleElement, Vec<Error>> {
pub fn build_from_ocafile(ocafile: String) -> Result<OCABundle, Vec<Error>> {
let ast = oca_file::ocafile::parse_from_string(ocafile.clone()).map_err(|e| {
vec![Error::ValidationError(vec![ValidationError::OCAFileParse(
e,
)])]
})?;
match ast {
oca_file::ocafile::OCAAst::TransformationAst(t_ast) => {
let transformation = transformation_file::build::from_ast(&t_ast).map_err(|e| {
e.iter()
.map(|e| ValidationError::TransformationBuild(e.clone()))
.collect::<Vec<_>>()
})
.map_err(|errs| vec![Error::ValidationError(errs)])?;
Ok(BundleElement::Transformation(transformation))
oca_file::ocafile::OCAAst::TransformationAst(_) => {
Err(vec![Error::Deprecated])
},
oca_file::ocafile::OCAAst::SemanticsAst(ast) => {
let oca_build = oca_bundle_semantics::build::from_ast(None, &ast).map_err(|e| {
Expand All @@ -88,7 +83,7 @@ pub fn build_from_ocafile(ocafile: String) -> Result<BundleElement, Vec<Error>>
})
.map_err(|errs| vec![Error::ValidationError(errs)])?;

Ok(BundleElement::Structural(oca_build.oca_bundle))
Ok(oca_build.oca_bundle)
}
}
}
Expand Down Expand Up @@ -143,36 +138,24 @@ impl Facade {
Ok(oca_build.oca_bundle.clone())
}

pub fn build_from_ocafile(&mut self, ocafile: String) -> Result<BundleElement, Vec<Error>> {
pub fn build_from_ocafile(&mut self, ocafile: String) -> Result<OCABundle, Vec<Error>> {
let ast = oca_file::ocafile::parse_from_string(ocafile.clone()).map_err(|e| {
vec![Error::ValidationError(vec![ValidationError::OCAFileParse(
e,
)])]
})?;
match ast {
oca_file::ocafile::OCAAst::TransformationAst(t_ast) => {
let transformation = transformation_file::build::from_ast(&t_ast).map_err(|e| {
e.iter()
.map(|e| ValidationError::TransformationBuild(e.clone()))
.collect::<Vec<_>>()
})
.map_err(|errs| vec![Error::ValidationError(errs)])?;
Ok(BundleElement::Transformation(transformation))
oca_file::ocafile::OCAAst::TransformationAst(_) => {
Err(vec![Error::Deprecated])
},
oca_file::ocafile::OCAAst::SemanticsAst(_ast) => {
let oca_build = self
.validate_ocafile(ocafile)
.map_err(|errs| vec![Error::ValidationError(errs)])?;

Ok(BundleElement::Structural(self.build(&oca_build)?))
self.build(&oca_build)
}
}
/*
let oca_build = self
.validate_ocafile(ocafile)
.map_err(|errs| vec![Error::ValidationError(errs)])?;
self.build(&oca_build) */
}

fn parse_and_check_base(
Expand Down
27 changes: 9 additions & 18 deletions oca/src/facade/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ fn retrive_all_references(bundle: OCABundle) -> Vec<SelfAddressingIdentifier> {
mod test {
use super::*;
use crate::data_storage::InMemoryDataStorage;
use crate::facade::bundle::BundleElement;
use crate::repositories::SQLiteConfig;

#[test]
Expand All @@ -454,26 +453,18 @@ ADD ENTRY en ATTRS list="refs:ENrf7niTCnz7HD-Ci88rlxHlxkpQ2NIZNNv08fQnXANI" el={
"#.to_string();
let oca_bundle = facade.build_from_ocafile(ocafile_input);
let oca_bundle = oca_bundle.unwrap();
if let BundleElement::Structural(structural) = oca_bundle {
let ocafile = facade.parse_oca_bundle_to_ocafile(&structural)?;
let new_structural = facade.build_from_ocafile(ocafile);
match new_structural {
Ok(BundleElement::Structural(new_structural)) => {
assert_eq!(structural.said, new_structural.said);
}
Ok(_) => {
panic!("Expected BundleElement::Structural")
}
Err(e) => {
println!("{:#?}", e);
panic!("Faild to load oca bundle");
}
let ocafile = facade.parse_oca_bundle_to_ocafile(&oca_bundle)?;
let new_bundle = facade.build_from_ocafile(ocafile);
match new_bundle {
Ok(new_bundle) => {
assert_eq!(oca_bundle.said, new_bundle.said);
}
Err(e) => {
println!("{:#?}", e);
panic!("Faild to load oca bundle");
}
} else {
panic!("Expected BundleElement::Structural")
}


Ok(())
}
}
101 changes: 44 additions & 57 deletions tests/build_from_ocafile.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#[cfg(test)]
pub mod dev;

#[cfg(test)]
mod test {
use oca_rs::{
data_storage::{DataStorage, InMemoryDataStorage},
facade::{build::Error, build::ValidationError, bundle::BundleElement},
facade::{build::Error, build::ValidationError},
repositories::SQLiteConfig,
Facade,
HashFunctionCode, SerializationFormats, EncodeBundle
Expand All @@ -28,27 +25,22 @@ ADD INFORMATION en ATTRS d="Schema digest" i="Credential Issuee" passed="Enables

let result = facade.build_from_ocafile(ocafile)?;

assert!(matches!(result, BundleElement::Structural(_)));
if let BundleElement::Structural(result) = result {
assert_eq!(
result.said.clone().unwrap().to_string(),
"EKHBds6myKVIsQuT7Zr23M8Xk_gwq-2SaDRUprvqOXxa"
);
assert_eq!(
result.said.clone().unwrap().to_string(),
"EKHBds6myKVIsQuT7Zr23M8Xk_gwq-2SaDRUprvqOXxa"
);

let code = HashFunctionCode::Blake3_256;
let format = SerializationFormats::JSON;
let oca_bundle_encoded = result.encode(&code, &format).unwrap();
let oca_bundle_version = String::from_utf8(
oca_bundle_encoded[6..23].to_vec()
).unwrap();
assert_eq!(oca_bundle_version, "OCAS11JSON000646_");

let search_result = facade.search_oca_bundle(None, "Ent".to_string(), 10, 1);
assert_eq!(search_result.metadata.total, 1);
Ok(())
} else {
panic!("Expected BundleElement::Structural")
}
let code = HashFunctionCode::Blake3_256;
let format = SerializationFormats::JSON;
let oca_bundle_encoded = result.encode(&code, &format).unwrap();
let oca_bundle_version = String::from_utf8(
oca_bundle_encoded[6..23].to_vec()
).unwrap();
assert_eq!(oca_bundle_version, "OCAS11JSON000646_");

let search_result = facade.search_oca_bundle(None, "Ent".to_string(), 10, 1);
assert_eq!(search_result.metadata.total, 1);
Ok(())
}

#[test]
Expand All @@ -74,15 +66,11 @@ ADD ATTRIBUTE x=Text
.to_string();
let result = facade.build_from_ocafile(ocafile)?;

if let BundleElement::Structural(result) = result {
assert_eq!(
result.said.unwrap().to_string(),
"EAMguWL--P5gad3xZoT2fd-qjoBDVkK82pb7KET1lrS1"
);
Ok(())
} else {
panic!("Expected BundleElement::Structural")
}
assert_eq!(
result.said.unwrap().to_string(),
"EAMguWL--P5gad3xZoT2fd-qjoBDVkK82pb7KET1lrS1"
);
Ok(())
}

#[test]
Expand Down Expand Up @@ -112,30 +100,24 @@ ADD ATTRIBUTE B=refn:first
ADD ATTRIBUTE C=Array[refn:second]
"#
.to_string();
let result = facade.build_from_ocafile(ocafile).unwrap();
let result = facade.build_from_ocafile(ocafile)?;

assert!(matches!(result, BundleElement::Structural(_)));
if let BundleElement::Structural(structural) = result {
assert_eq!(
structural.said.unwrap().to_string(),
"EGv65yGtFZG5CSRaS4q46dC3UWsW3vycbMFOqPFPvhWi"
);
}
assert_eq!(
result.said.unwrap().to_string(),
"EGv65yGtFZG5CSRaS4q46dC3UWsW3vycbMFOqPFPvhWi"
);

let from_ocafile = r#"
FROM EE15xNvWNy89ZBFhMBukb2kovfO2Y73y1Si2oFFkWFpy
ADD ATTRIBUTE x=Text
"#
.to_string();

let result = facade.build_from_ocafile(from_ocafile).unwrap();
assert!(matches!(result, BundleElement::Structural(_)));
if let BundleElement::Structural(structural) = result {
assert_eq!(
structural.said.unwrap().to_string(),
"EE-Ru8mxNWhql7Q2ibY2-uuK9cIKxR2S9rc-eRkEeBwO"
);
}
let result = facade.build_from_ocafile(from_ocafile)?;
assert_eq!(
result.said.unwrap().to_string(),
"EE-Ru8mxNWhql7Q2ibY2-uuK9cIKxR2S9rc-eRkEeBwO"
);
let refs = facade.fetch_all_refs().unwrap();

assert_eq!(refs.len(), 2);
Expand Down Expand Up @@ -164,13 +146,18 @@ ADD ATTRIBUTE C=Array[refn:third]
assert!(result.is_err());
let errors = result.unwrap_err();
let error = errors.first().unwrap();
let Error::ValidationError(validation_errors) = error;
let validation_error = validation_errors.first().unwrap();
assert!(
matches!(
validation_error,
ValidationError::UnknownRefn(_)
)
);
assert!(matches!(
error,
Error::ValidationError(_)
));
if let Error::ValidationError(validation_errors) = error {
let validation_error = validation_errors.first().unwrap();
assert!(
matches!(
validation_error,
ValidationError::UnknownRefn(_)
)
);
}
}
}
118 changes: 0 additions & 118 deletions tests/dev.rs

This file was deleted.

0 comments on commit fcff7d0

Please sign in to comment.