Skip to content

Commit 5c41ab3

Browse files
authored
Merge pull request #148 from code0-tech/147-add-reader-struct-options
[Rust-Reader] Struct Options
2 parents 741f36f + 60cd20d commit 5c41ab3

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ for (const feature in features) {
9090
```
9191

9292
## Rust Definition Package
93-
93+
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.
94+
### Package Resources
95+
Crate: [code0-definition-reader](https://crates.io/crates/code0-definition-reader) on crates.io
9496
### Install Package
9597
```bash
9698
cargo add code0-definition-reader
@@ -99,14 +101,23 @@ cargo add code0-definition-reader
99101
### Usage
100102

101103
```rs
102-
use code0_definition_reader::Definition;
104+
use code0_definition_reader::Reader;
105+
106+
let reader = Reader::configure(
107+
String::new(), // Path required for configure
108+
false, // should_break
109+
Vec::new(), // accepted_features
110+
None // accepted_versions
111+
);
103112

104-
let features = Definition::new("./path/to/definitions");
113+
let features = reader.read_features("./path/to/definitions");
105114

106115
for feature in features {
107-
let name = feature.name(); //name of the feature (e.g. rest)
108-
let data_types = feature.data_types(); //dataTypes of this feature
109-
let flow_types = feature.flow_types(); //flowTypes of this feature
110-
let functions = feature.runtime_functions(); //runtimeFunctions of this feature
116+
let name = &feature.name; // name of the feature (e.g., http)
117+
let data_types = &feature.data_types; // dataTypes of this feature
118+
let flow_types = &feature.flow_types; // flowTypes of this feature
119+
let functions = &feature.functions; // runtimeFunctions of this feature
120+
121+
println!("Loaded feature: {}", name);
111122
}
112123
```

crates/package/src/lib.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use walkdir::WalkDir;
1212
pub struct Reader {
1313
should_break: bool,
1414
accepted_features: Vec<String>,
15-
accepted_versions: Option<Version>,
15+
accepted_version: Option<Version>,
1616
path: String,
1717
}
1818

@@ -21,28 +21,28 @@ impl Reader {
2121
path: String,
2222
should_break: bool,
2323
accepted_features: Vec<String>,
24-
accepted_versions: Option<Version>,
24+
accepted_version: Option<Version>,
2525
) -> Self {
2626
Self {
2727
should_break,
2828
accepted_features,
29-
accepted_versions,
29+
accepted_version,
3030
path,
3131
}
3232
}
3333

34-
pub fn read_features(&self, path: &str) -> Result<Vec<Feature>, ReaderError> {
35-
let definitions = Path::new(path);
34+
pub fn read_features(&self) -> Result<Vec<Feature>, ReaderError> {
35+
let definitions = Path::new(&self.path);
3636

3737
match self.read_feature_content(definitions) {
3838
Ok(features) => {
3939
log::info!("Loaded Successfully {} features", features.len());
4040
Ok(features)
4141
}
4242
Err(err) => {
43-
log::error!("Failed to read features from {}", path);
43+
log::error!("Failed to read features from {}", &self.path);
4444
Err(ReaderError::ReadFeatureError {
45-
path: path.to_string(),
45+
path: self.path.to_string(),
4646
source: Box::new(err),
4747
})
4848
}
@@ -95,8 +95,23 @@ impl Reader {
9595
let flow_types: Vec<FlowType> = self.collect_definitions(&flow_types_path)?;
9696

9797
let functions_path = path.join("runtime_definition");
98-
let functions: Vec<RuntimeFunctionDefinition> =
99-
self.collect_definitions(&functions_path)?;
98+
let functions =
99+
match self.collect_definitions::<RuntimeFunctionDefinition>(&functions_path) {
100+
Ok(func) => func
101+
.into_iter()
102+
.filter(|v| v.version == self.accepted_version)
103+
.collect(),
104+
Err(err) => {
105+
if self.should_break {
106+
return Err(ReaderError::ReadFeatureError {
107+
path: functions_path.to_string_lossy().to_string(),
108+
source: Box::new(err),
109+
});
110+
} else {
111+
continue;
112+
}
113+
}
114+
};
100115

101116
let feature = Feature {
102117
name: feature_name,
@@ -140,11 +155,15 @@ impl Reader {
140155
match serde_json::from_str::<T>(&content) {
141156
Ok(def) => definitions.push(def),
142157
Err(e) => {
143-
log::error!("Failed to parse JSON in file {}: {}", path.display(), e);
144-
return Err(ReaderError::JsonError {
145-
path: path.to_path_buf(),
146-
error: e,
147-
});
158+
if self.should_break {
159+
log::error!("Failed to parse JSON in file {}: {}", path.display(), e);
160+
return Err(ReaderError::JsonError {
161+
path: path.to_path_buf(),
162+
error: e,
163+
});
164+
} else {
165+
log::warn!("Skipping invalid JSON file {}: {}", path.display(), e);
166+
}
148167
}
149168
}
150169
}

0 commit comments

Comments
 (0)