diff --git a/oca/src/facade/build.rs b/oca/src/facade/build.rs index acff19b..23e1b6d 100644 --- a/oca/src/facade/build.rs +++ b/oca/src/facade/build.rs @@ -1,4 +1,3 @@ -use super::bundle::BundleElement; use super::Facade; use crate::data_storage::{DataStorage, Namespace}; use crate::facade::fetch::get_oca_bundle; @@ -25,6 +24,8 @@ use said::sad::SerializationFormats; pub enum Error { #[error("Validation error")] ValidationError(Vec), + #[error("Deprecated")] + Deprecated, } #[derive(thiserror::Error, Debug, serde::Serialize)] @@ -64,21 +65,15 @@ impl References for Box { } } -pub fn build_from_ocafile(ocafile: String) -> Result> { +pub fn build_from_ocafile(ocafile: String) -> Result> { 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::>() - }) - .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| { @@ -88,7 +83,7 @@ pub fn build_from_ocafile(ocafile: String) -> Result> }) .map_err(|errs| vec![Error::ValidationError(errs)])?; - Ok(BundleElement::Structural(oca_build.oca_bundle)) + Ok(oca_build.oca_bundle) } } } @@ -143,36 +138,24 @@ impl Facade { Ok(oca_build.oca_bundle.clone()) } - pub fn build_from_ocafile(&mut self, ocafile: String) -> Result> { + pub fn build_from_ocafile(&mut self, ocafile: String) -> Result> { 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::>() - }) - .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( diff --git a/oca/src/facade/fetch.rs b/oca/src/facade/fetch.rs index 387b3c9..f588588 100644 --- a/oca/src/facade/fetch.rs +++ b/oca/src/facade/fetch.rs @@ -429,7 +429,6 @@ fn retrive_all_references(bundle: OCABundle) -> Vec { mod test { use super::*; use crate::data_storage::InMemoryDataStorage; - use crate::facade::bundle::BundleElement; use crate::repositories::SQLiteConfig; #[test] @@ -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(()) } } diff --git a/tests/build_from_ocafile.rs b/tests/build_from_ocafile.rs index 5accd47..7c2b19a 100644 --- a/tests/build_from_ocafile.rs +++ b/tests/build_from_ocafile.rs @@ -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 @@ -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] @@ -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] @@ -112,15 +100,12 @@ 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 @@ -128,14 +113,11 @@ 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); @@ -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(_) + ) + ); + } } } diff --git a/tests/dev.rs b/tests/dev.rs deleted file mode 100644 index 7293c08..0000000 --- a/tests/dev.rs +++ /dev/null @@ -1,118 +0,0 @@ -#[cfg(test)] -mod test { - use oca_rs::{ - data_storage::{DataStorage, InMemoryDataStorage}, - facade::{build::Error, bundle::{Bundle, BundleElement}}, - repositories::SQLiteConfig, - Facade, - }; - - #[test] - fn dev() -> Result<(), Vec> { - let db = InMemoryDataStorage::new(); - let db_cache = InMemoryDataStorage::new(); - let cache_storage_config = SQLiteConfig::build().unwrap(); - let ocafile = r#" -ADD ATTRIBUTE d=Text i = Text passed=Boolean -ADD META en PROPS name = "Entrance credential" description = "Entrance credential" -ADD CHARACTER_ENCODING ATTRS d=utf-8 i=utf-8 passed=utf-8 -ADD CONFORMANCE ATTRS d=M i=M passed=M -ADD LABEL en ATTRS d="Schema digest" i="Credential Issuee" passed="Passed" -ADD INFORMATION en ATTRS d="Schema digest" i="Credential Issuee" passed="Enables or disables passing" -ADD UNIT ATTRS d=kg -"#.to_string(); - let mut facade = - Facade::new(Box::new(db), Box::new(db_cache), cache_storage_config); - - let result = facade.build_from_ocafile(ocafile)?; - - let mut bundle = Bundle::new(); - - bundle.add(result); - - let ocafile_rename = r#" --- precompiler=transformation --- source=refs:source_said --- target=refs:target_said -RENAME ATTRIBUTE d=first_name -"# - .to_string(); - let result_rename = facade.build_from_ocafile(ocafile_rename)?; - bundle.add(result_rename); - - let ocafile_rename2 = r#" --- precompiler=transformation --- source=refs:source_said --- target=refs:target_said -RENAME ATTRIBUTE i=last_name -"# - .to_string(); - let result_rename2 = facade.build_from_ocafile(ocafile_rename2)?; - bundle.add(result_rename2); - - bundle.fill_said(); - - println!("{}", bundle.encode().unwrap()); - - Ok(()) - } - - #[test] - fn dev_fake_to_mock() -> Result<(), Vec> { - let db = InMemoryDataStorage::new(); - let db_cache = InMemoryDataStorage::new(); - let cache_storage_config = SQLiteConfig::build().unwrap(); - let ocafile_source = r#" --- name=fake_standard_patient - -ADD ATTRIBUTE name=Text surname=Text height=Numeric weight=Numeric -ADD META en PROPS description="FAKE Standard Patient" name="FAKE Patient" -ADD CHARACTER_ENCODING ATTRS name="utf-8" surname="utf-8" height="utf-8" weight="utf-8" -"#.to_string(); - - let ocafile_target = r#" --- name=mock_standard_patient - -ADD ATTRIBUTE first_name=Text last_name=Text hgt=Numeric wgt=Numeric -ADD META en PROPS description="MOCK Standard Patient" name="MOCK Patient" -ADD CHARACTER_ENCODING ATTRS first_name="utf-8" last_name="utf-8" hgt="utf-8" wgt="utf-8" -"#.to_string(); - let mut facade = - Facade::new(Box::new(db), Box::new(db_cache), cache_storage_config); - - let mut bundle = Bundle::new(); - - let result_source = facade.build_from_ocafile(ocafile_source)?; - let mut source_said: Option<_> = None; - if let BundleElement::Structural(ref structural) = result_source { - source_said = structural.said.clone(); - } - bundle.add(result_source); - - let result_target = facade.build_from_ocafile(ocafile_target)?; - let mut target_said: Option<_> = None; - if let BundleElement::Structural(ref structural) = result_target { - target_said = structural.said.clone(); - } - - let ocafile_link = format!(r#" --- precompiler=transformation --- source=refs:{} --- target=refs:{} - -LINK ATTRIBUTE name -> first_name -LINK ATTRIBUTE surname -> last_name -LINK ATTRIBUTE height -> hgt -LINK ATTRIBUTE weight -> wgt -"#, source_said.unwrap(), target_said.unwrap()); - let result_link = facade.build_from_ocafile(ocafile_link)?; - println!("{:#?}", result_link); - bundle.add(result_link); - - bundle.fill_said(); - - println!("{}", bundle.encode().unwrap()); - - Ok(()) - } -}