diff --git a/README.md b/README.md index 6226674..ed438e1 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,9 @@ for (const feature in features) { ``` ## Rust Definition Package - +This package is a Rust crate designed to read and parse CodeZero definition files (JSON) from a directory structure. It loads all features, including data-types, flow-types, and runtime-functions, providing them as idiomatic Rust structs. +### Package Resources +Crate: [code0-definition-reader](https://crates.io/crates/code0-definition-reader) on crates.io ### Install Package ```bash cargo add code0-definition-reader @@ -99,14 +101,23 @@ cargo add code0-definition-reader ### Usage ```rs -use code0_definition_reader::Definition; +use code0_definition_reader::Reader; + +let reader = Reader::configure( + String::new(), // Path required for configure + false, // should_break + Vec::new(), // accepted_features + None // accepted_versions +); -let features = Definition::new("./path/to/definitions"); +let features = reader.read_features("./path/to/definitions"); for feature in features { - let name = feature.name(); //name of the feature (e.g. rest) - let data_types = feature.data_types(); //dataTypes of this feature - let flow_types = feature.flow_types(); //flowTypes of this feature - let functions = feature.runtime_functions(); //runtimeFunctions of this feature + let name = &feature.name; // name of the feature (e.g., http) + let data_types = &feature.data_types; // dataTypes of this feature + let flow_types = &feature.flow_types; // flowTypes of this feature + let functions = &feature.functions; // runtimeFunctions of this feature + + println!("Loaded feature: {}", name); } ``` diff --git a/crates/package/src/lib.rs b/crates/package/src/lib.rs index c6546bd..8e8c2d1 100644 --- a/crates/package/src/lib.rs +++ b/crates/package/src/lib.rs @@ -12,7 +12,7 @@ use walkdir::WalkDir; pub struct Reader { should_break: bool, accepted_features: Vec, - accepted_versions: Option, + accepted_version: Option, path: String, } @@ -21,18 +21,18 @@ impl Reader { path: String, should_break: bool, accepted_features: Vec, - accepted_versions: Option, + accepted_version: Option, ) -> Self { Self { should_break, accepted_features, - accepted_versions, + accepted_version, path, } } - pub fn read_features(&self, path: &str) -> Result, ReaderError> { - let definitions = Path::new(path); + pub fn read_features(&self) -> Result, ReaderError> { + let definitions = Path::new(&self.path); match self.read_feature_content(definitions) { Ok(features) => { @@ -40,9 +40,9 @@ impl Reader { Ok(features) } Err(err) => { - log::error!("Failed to read features from {}", path); + log::error!("Failed to read features from {}", &self.path); Err(ReaderError::ReadFeatureError { - path: path.to_string(), + path: self.path.to_string(), source: Box::new(err), }) } @@ -95,8 +95,23 @@ impl Reader { let flow_types: Vec = self.collect_definitions(&flow_types_path)?; let functions_path = path.join("runtime_definition"); - let functions: Vec = - self.collect_definitions(&functions_path)?; + let functions = + match self.collect_definitions::(&functions_path) { + Ok(func) => func + .into_iter() + .filter(|v| v.version == self.accepted_version) + .collect(), + Err(err) => { + if self.should_break { + return Err(ReaderError::ReadFeatureError { + path: functions_path.to_string_lossy().to_string(), + source: Box::new(err), + }); + } else { + continue; + } + } + }; let feature = Feature { name: feature_name, @@ -140,11 +155,15 @@ impl Reader { match serde_json::from_str::(&content) { Ok(def) => definitions.push(def), Err(e) => { - log::error!("Failed to parse JSON in file {}: {}", path.display(), e); - return Err(ReaderError::JsonError { - path: path.to_path_buf(), - error: e, - }); + if self.should_break { + log::error!("Failed to parse JSON in file {}: {}", path.display(), e); + return Err(ReaderError::JsonError { + path: path.to_path_buf(), + error: e, + }); + } else { + log::warn!("Skipping invalid JSON file {}: {}", path.display(), e); + } } } }