diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 40 |
1 files changed, 12 insertions, 28 deletions
diff --git a/src/main.rs b/src/main.rs index 53a1301..7230dce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,14 @@ -use std::{ - fs, - io::{self, ErrorKind}, - path::{Path, PathBuf}, -}; +use std::{io, path::PathBuf}; use clap::Parser; use color_eyre::{ - eyre::{bail, eyre, Context, Result}, + eyre::{bail, eyre, Result}, Report, }; use hittekaart::{ gpx::{self, Compression}, renderer, + storage::{Folder, Storage}, }; use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle}; use is_terminal::IsTerminal; @@ -88,7 +85,8 @@ fn main() -> Result<()> { let tracks = tracks.into_iter().collect::<Result<Vec<_>>>()?; bar.finish(); - ensure_output_directory(&args.output_directory)?; + let mut storage = Folder::new(args.output_directory.clone()); + storage.prepare()?; let multibar = MultiProgress::new(); if !use_progress_bars { @@ -107,37 +105,23 @@ fn main() -> Result<()> { bar.finish(); multibar.remove(&bar); - let target = [&args.output_directory, &zoom.to_string().into()] - .iter() - .collect::<PathBuf>(); - fs::create_dir(&target)?; + storage.prepare_zoom(zoom)?; let bar = make_bar(counter.tile_count().try_into().unwrap()).with_style(progress_style.clone()); multibar.insert_from_back(1, bar.clone()); bar.set_prefix("Saving heat tiles"); - renderer::lazy_colorization(counter, &target, |x| bar.inc(x.try_into().unwrap()))?; + renderer::lazy_colorization(counter, |rendered_tile| { + storage.store(zoom, rendered_tile.x, rendered_tile.y, &rendered_tile.data)?; + bar.inc(1); + Ok(()) + })?; bar.finish(); multibar.remove(&bar); zoom_bar.inc(1); } + storage.finish()?; zoom_bar.finish(); Ok(()) } - -fn ensure_output_directory<P: AsRef<Path>>(path: P) -> Result<()> { - let path = path.as_ref(); - let metadata = fs::metadata(path); - match metadata { - Err(e) if e.kind() == ErrorKind::NotFound => { - let parent = path.parent().unwrap_or_else(|| Path::new("/")); - fs::create_dir(path) - .context(format!("Could not create output directory at {parent:?}"))? - } - Err(e) => Err(e).context("Error while checking output directory")?, - Ok(m) if m.is_dir() => (), - Ok(_) => bail!("Output directory is not a directory"), - } - Ok(()) -} |