Update: toml reader for multiple dir

This commit is contained in:
itiB
2020-10-29 21:49:55 +09:00
parent 0a222e0efa
commit d55dfe587f

View File

@@ -16,7 +16,7 @@ impl ParseToml {
ParseToml { rules: Vec::new() }
}
fn read_file(&self, path: PathBuf) -> Result<String, String> {
pub fn read_file(&self, path: PathBuf) -> Result<String, String> {
let mut file_content = String::new();
let mut fr = fs::File::open(path)
@@ -29,7 +29,7 @@ impl ParseToml {
Ok(file_content)
}
fn read_dir<P: AsRef<Path>>(&mut self, path: P) -> io::Result<String> {
pub fn read_dir<P: AsRef<Path>>(&mut self, path: P) -> io::Result<String> {
Ok(fs::read_dir(path)?
.filter_map(|entry| {
let entry = entry.ok()?;
@@ -39,6 +39,9 @@ impl ParseToml {
Err(e) => panic!("fail to read file: {}", e),
};
}
if entry.file_type().ok()?.is_dir() {
self.read_dir(entry.path());
}
Some("")
})
.collect())
@@ -66,4 +69,21 @@ mod tests {
}
}
}
#[test]
fn test_read_multiple_dir() {
let mut toml = toml::ParseToml::new();
&toml.read_dir("test_files".to_string());
for rule in toml.rules {
match rule {
Ok(_rule) => {
if let Some(severity) = _rule.rule.severity {
assert_eq!("high", severity);
}
}
Err(_) => (),
}
}
}
}