From ee10d2fa5add12c51524e8698dcf39ac0aae1f63 Mon Sep 17 00:00:00 2001 From: Andreas Date: Thu, 6 Jun 2024 22:05:50 +0200 Subject: [PATCH] fix geojson flag handling and introduce path object (#46) --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- src/main.rs | 2 +- src/output/file_creator.rs | 6 +++--- src/output/output_handler.rs | 34 +++++++++++++++++++--------------- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec83bef..c6d9551 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,9 +40,9 @@ dependencies = [ [[package]] name = "anstyle-query" -version = "1.0.3" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ "windows-sys", ] @@ -319,7 +319,7 @@ dependencies = [ [[package]] name = "osm_extract_polygon" -version = "0.5.5" +version = "0.5.6" dependencies = [ "clap", "geo-types", diff --git a/Cargo.toml b/Cargo.toml index 9c390b2..0f32ec3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "osm_extract_polygon" -version = "0.5.5" +version = "0.5.6" authors = ["Andreas "] edition = "2018" diff --git a/src/main.rs b/src/main.rs index b033794..defb2df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,7 +119,7 @@ fn main() { OverwriteConfiguration::Ask }; - let geojson_output = matches.contains_id(GEOJSON_ARG); + let geojson_output = matches.get_flag(GEOJSON_ARG); let output_handler_config = OutputHandlerConfiguration { overwrite_configuration, diff --git a/src/output/file_creator.rs b/src/output/file_creator.rs index 8ff017a..9865c97 100644 --- a/src/output/file_creator.rs +++ b/src/output/file_creator.rs @@ -16,9 +16,9 @@ enum OverwriteOrSkip { } impl FileCreator { - pub fn create_file(&mut self, filename: &str) -> std::io::Result { - if Path::new(filename).exists() { - match self.overwrite_handling(filename)? { + pub fn create_file(&mut self, filename: &Path) -> std::io::Result { + if filename.exists() { + match self.overwrite_handling(filename.as_os_str().to_str().unwrap())? { OverwriteOrSkip::Skip => { return Err(Error::new(ErrorKind::AlreadyExists, "skipped")); } diff --git a/src/output/output_handler.rs b/src/output/output_handler.rs index d554052..f7f2464 100644 --- a/src/output/output_handler.rs +++ b/src/output/output_handler.rs @@ -7,6 +7,7 @@ use crate::output::OverwriteConfiguration; use std::collections::HashSet; use std::fs::{create_dir_all, File}; use std::io::Result; +use std::path::{Path, PathBuf}; use std::time::Instant; pub trait FileWriter { @@ -54,11 +55,20 @@ impl OutputHandler { println!("writing output files..."); for (name, polygon) in filename_polys { - let filename_wo_ext = format!("{}/{}", base_folder, name); - if self.write_poly && self.write_file(&filename_wo_ext, "poly", polygon, &poly_writer) { + let filename_wo_ext: PathBuf = [base_folder.to_string(), name].iter().collect(); + if self.write_poly + && self.write_file(filename_wo_ext.with_extension("poly").as_path(), polygon, &poly_writer) + { file_count += 1; } - if self.write_geojson && self.write_file(&filename_wo_ext, "geojson", polygon, &geojson_writer) { + + if self.write_geojson + && self.write_file( + filename_wo_ext.with_extension("geojson").as_path(), + polygon, + &geojson_writer, + ) + { file_count += 1; } } @@ -67,27 +77,21 @@ impl OutputHandler { Ok(file_count) } - pub fn write_file( - &mut self, - filename_wo_ext: &str, - ext: &str, - polygon: &Polygon, - file_writer: &impl FileWriter, - ) -> bool { - let filename = format!("{}.{}", filename_wo_ext, ext); - + pub fn write_file(&mut self, filename_wo_ext: &Path, polygon: &Polygon, file_writer: &impl FileWriter) -> bool { let result = self .file_creator - .create_file(&filename) + .create_file(filename_wo_ext) .and_then(|mut file| file_writer.write_to_file(&mut file, polygon)); + let filename_str = filename_wo_ext.as_os_str().to_str().unwrap(); + match result { Err(e) => { - println!("{}: {}", filename, e); + println!("{}: {}", filename_str, e); false } Ok(_) => { - println!("{}: successfully written ", filename); + println!("{}: successfully written ", filename_str); true } }