From d55dfe587ff43c2d2f748b893630c8d5797cf6f9 Mon Sep 17 00:00:00 2001 From: itiB Date: Thu, 29 Oct 2020 21:49:55 +0900 Subject: [PATCH] Update: toml reader for multiple dir --- src/toml.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/toml.rs b/src/toml.rs index 9e1c366a..08482f88 100644 --- a/src/toml.rs +++ b/src/toml.rs @@ -16,7 +16,7 @@ impl ParseToml { ParseToml { rules: Vec::new() } } - fn read_file(&self, path: PathBuf) -> Result { + pub fn read_file(&self, path: PathBuf) -> Result { 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>(&mut self, path: P) -> io::Result { + pub fn read_dir>(&mut self, path: P) -> io::Result { 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(_) => (), + } + } + } }