Skip to content

Commit

Permalink
fix geojson flag handling and introduce path object (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndGem committed Jun 6, 2024
1 parent d0be94c commit ee10d2f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "osm_extract_polygon"
version = "0.5.5"
version = "0.5.6"
authors = ["Andreas <[email protected]>"]
edition = "2018"

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/output/file_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ enum OverwriteOrSkip {
}

impl FileCreator {
pub fn create_file(&mut self, filename: &str) -> std::io::Result<File> {
if Path::new(filename).exists() {
match self.overwrite_handling(filename)? {
pub fn create_file(&mut self, filename: &Path) -> std::io::Result<File> {
if filename.exists() {
match self.overwrite_handling(filename.as_os_str().to_str().unwrap())? {
OverwriteOrSkip::Skip => {
return Err(Error::new(ErrorKind::AlreadyExists, "skipped"));
}
Expand Down
34 changes: 19 additions & 15 deletions src/output/output_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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
}
}
Expand Down

0 comments on commit ee10d2f

Please sign in to comment.