From a9efce5e506e1cd5623809142c725dbc6c0d7a1b Mon Sep 17 00:00:00 2001 From: Kuba Groblewski Date: Tue, 14 May 2024 00:06:25 +0200 Subject: [PATCH 1/2] feat: multi file wip --- src/lib.rs | 3 +-- src/stats.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4b17d73..b16afef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,8 +27,7 @@ impl Config { } pub fn run(config: Config) -> Result<(), Box> { - // let stats = Stats::from_dir(&config.path)?; - let stats = Stats::from_file(&config.path)?; + let stats = Stats::new().from_path(&config.path)?; stats.print(); Ok(()) } diff --git a/src/stats.rs b/src/stats.rs index dd03ed6..425a90e 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -27,9 +27,8 @@ pub struct UDDFData { } impl Stats { - pub fn from_file(path: &str) -> Result> { - println!("\nFile: {}", path); - let mut stats = Stats { + pub fn new() -> Self { + Self { dives_no: 0, total_time: 0, depth_max: 0.0, @@ -39,17 +38,61 @@ impl Stats { gf_99_max: 0., gf_end_max: 0., time_below: vec![], - }; - let UDDFData { dives_data, gas_mixes } = stats.extract_data_from_file(path)?; + } + } - for dive_data in dives_data { - let dive = stats.calc_dive_stats(&dive_data, &gas_mixes)?; - stats.update_with_dive_data(dive); + pub fn from_path(&self, path: &str) -> Result> { + let mut stats = Self::new(); + let path_meta = fs::metadata(path)?; + if path_meta.is_file() { + stats.from_file(path)?; + } else if path_meta.is_dir() { + stats.from_dir(path)?; + } else { + return Err("Unable to resolve file or directory".into()) } Ok(stats) } + fn from_file(&mut self, path: &str) -> Result<(), Box> { + println!("\nFile: {}", path); + let UDDFData { dives_data, gas_mixes } = self.extract_data_from_file(path)?; + for dive_data in dives_data { + let dive = self.calc_dive_stats(&dive_data, &gas_mixes)?; + self.update_with_dive_data(dive); + } + Ok(()) + } + + fn from_dir(&mut self, path: &str) -> Result, Box> { + println!("\nDirectory: {}", path); + + let paths = Self::traverse_for_uddf(path)?; + dbg!(&paths); + + Ok(paths) + } + + fn traverse_for_uddf(path: &str) -> Result, Box> { + let mut uddf_file_paths:Vec = vec![]; + let entries = fs::read_dir(path)?; + for entry in entries { + let entry = entry?; + let path = entry.path(); + if path.is_dir() { + let mut traversal_res = Stats::traverse_for_uddf(path.to_str().unwrap())?; + uddf_file_paths.append(&mut traversal_res); + } + let extension = path.extension().unwrap_or_default(); + if extension.to_ascii_lowercase() == "uddf" { + uddf_file_paths.push(path); + } + } + + Ok(uddf_file_paths) + } + fn extract_data_from_file(&self, path: &str) -> Result> { println!("Extracting dives from UDDF"); let file = parser::parse_file(path)?; From c4e9d152b216dfb78b16b188555f714b87d4c19d Mon Sep 17 00:00:00 2001 From: Kuba Groblewski Date: Tue, 14 May 2024 21:25:02 +0200 Subject: [PATCH 2/2] feat: stats from directory nested --- src/stats.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/stats.rs b/src/stats.rs index 425a90e..9dec73e 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -56,7 +56,7 @@ impl Stats { } fn from_file(&mut self, path: &str) -> Result<(), Box> { - println!("\nFile: {}", path); + // println!("\nFile: {}", path); let UDDFData { dives_data, gas_mixes } = self.extract_data_from_file(path)?; for dive_data in dives_data { let dive = self.calc_dive_stats(&dive_data, &gas_mixes)?; @@ -66,10 +66,12 @@ impl Stats { } fn from_dir(&mut self, path: &str) -> Result, Box> { - println!("\nDirectory: {}", path); + // println!("\nDirectory: {}", path); let paths = Self::traverse_for_uddf(path)?; - dbg!(&paths); + for path in &paths { + self.from_file(&path.to_str().unwrap()); + } Ok(paths) } @@ -94,7 +96,7 @@ impl Stats { } fn extract_data_from_file(&self, path: &str) -> Result> { - println!("Extracting dives from UDDF"); + // println!("Extracting dives from UDDF"); let file = parser::parse_file(path)?; let gas_definitions = file.gas_definitions;