-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
174 lines (152 loc) · 5.78 KB
/
Copy pathlib.rs
File metadata and controls
174 lines (152 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
mod r#enum;
mod r#struct;
use crate::r#enum::reader_error::ReaderError;
use crate::r#struct::feature::Feature;
use serde::de::DeserializeOwned;
use std::fs;
use std::path::Path;
use tucana::shared::{DefinitionDataType, FlowType, RuntimeFunctionDefinition, Version};
use walkdir::WalkDir;
pub struct Reader {
should_break: bool,
accepted_features: Vec<String>,
accepted_version: Option<Version>,
path: String,
}
impl Reader {
pub fn configure(
path: String,
should_break: bool,
accepted_features: Vec<String>,
accepted_version: Option<Version>,
) -> Self {
Self {
should_break,
accepted_features,
accepted_version,
path,
}
}
pub fn read_features(&self) -> Result<Vec<Feature>, ReaderError> {
let definitions = Path::new(&self.path);
match self.read_feature_content(definitions) {
Ok(features) => {
log::info!("Loaded Successfully {} features", features.len());
Ok(features)
}
Err(err) => {
log::error!("Failed to read features from {}", &self.path);
Err(ReaderError::ReadFeatureError {
path: self.path.to_string(),
source: Box::new(err),
})
}
}
}
fn read_feature_content(&self, dir: &Path) -> Result<Vec<Feature>, ReaderError> {
let mut features: Vec<Feature> = Vec::new();
let readdir = match fs::read_dir(dir) {
Ok(readdir) => readdir,
Err(err) => {
log::error!("Failed to read directory {}: {}", dir.display(), err);
return Err(ReaderError::ReadDirectoryError {
path: dir.to_path_buf(),
error: err,
});
}
};
for entry_result in readdir {
let entry = match entry_result {
Ok(entry) => entry,
Err(err) => {
log::error!("Failed to read directory entry: {}", err);
return Err(ReaderError::DirectoryEntryError(err));
}
};
let path = entry.path();
if path.is_dir() {
let feature_name = path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
if !self.accepted_features.is_empty()
&& !self.accepted_features.contains(&feature_name)
{
log::info!("Skipping feature: {}", feature_name);
continue;
}
let data_types_path = path.join("data_type");
let data_types: Vec<DefinitionDataType> =
self.collect_definitions(&data_types_path)?;
let flow_types_path = path.join("flow_type");
let flow_types: Vec<FlowType> = self.collect_definitions(&flow_types_path)?;
let functions_path = path.join("runtime_definition");
let functions =
match self.collect_definitions::<RuntimeFunctionDefinition>(&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,
data_types,
flow_types,
functions,
};
features.push(feature);
}
}
Ok(features)
}
fn collect_definitions<T>(&self, dir: &Path) -> Result<Vec<T>, ReaderError>
where
T: DeserializeOwned,
{
let mut definitions = Vec::new();
if !dir.exists() {
return Ok(definitions);
}
for entry in WalkDir::new(dir).into_iter().filter_map(Result::ok) {
let path = entry.path();
if path.is_file() && path.extension().is_some_and(|ext| ext == "json") {
let content = match fs::read_to_string(path) {
Ok(content) => content,
Err(err) => {
log::error!("Failed to read file {}: {}", path.display(), err);
return Err(ReaderError::ReadFileError {
path: path.to_path_buf(),
error: err,
});
}
};
match serde_json::from_str::<T>(&content) {
Ok(def) => definitions.push(def),
Err(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);
}
}
}
}
}
Ok(definitions)
}
}