Skip to content

Commit b743919

Browse files
committed
[fix] Add Reader Struct Options
1 parent c3095cd commit b743919

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

crates/package/src/lib.rs

Lines changed: 37 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 = match self.collect_definitions::<RuntimeFunctionDefinition>(&functions_path) {
99+
Ok(func) => {
100+
func.into_iter()
101+
.filter(|v| v.version == self.accepted_version)
102+
.collect()
103+
},
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,19 @@ 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!(
166+
"Skipping invalid JSON file {}: {}",
167+
path.display(),
168+
e
169+
);
170+
}
148171
}
149172
}
150173
}

0 commit comments

Comments
 (0)