From 244c33a07952f0ee22cc3641f35eb5af55f405f1 Mon Sep 17 00:00:00 2001 From: gennyble Date: Mon, 9 Oct 2023 20:41:03 -0500 Subject: cleanup squash --- squash/src/cli.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 squash/src/cli.rs (limited to 'squash/src/cli.rs') diff --git a/squash/src/cli.rs b/squash/src/cli.rs new file mode 100644 index 0000000..8a127b7 --- /dev/null +++ b/squash/src/cli.rs @@ -0,0 +1,85 @@ +use camino::Utf8PathBuf; + +pub struct Cli { + pub color_count: u8, + pub input: Utf8PathBuf, + pub in_type: InType, + pub output: Utf8PathBuf, + pub out_type: OutType, +} + +pub enum InType { + Jpeg, + Png, +} + +pub enum OutType { + Png, + Gif, +} + +// Get's the CLI arguments or dies trying +pub fn get() -> Cli { + let usage = || -> ! { + println!("usage: squash "); + std::process::exit(0); + }; + let mut argv = std::env::args().skip(1); + + let color_count: u8 = if let Some(Ok(count)) = argv.next().map(|r| r.parse::()) { + if count > 256 { + eprintln!("max colour count must be 256 or below"); + std::process::exit(1); + } else { + (count - 1) as u8 + } + } else { + usage() + }; + + let input: Utf8PathBuf = if let Some(path) = argv.next() { + path.into() + } else { + usage(); + }; + + let in_type = match input.extension() { + None => { + eprintln!("can't determine input filetype!\nSupported input types: PNG, JPG"); + std::process::exit(1); + } + Some("png") => InType::Png, + Some("jpg") | Some("jpeg") => InType::Jpeg, + Some(ext) => { + eprintln!("unknown filetype '{ext}'!\nSupported input types: PNG, JPG"); + std::process::exit(1); + } + }; + + let output: Utf8PathBuf = if let Some(path) = argv.next() { + path.into() + } else { + usage(); + }; + + let out_type = match output.extension() { + None => { + eprintln!("can't determine output filetype!"); + std::process::exit(1); + } + Some("png") => OutType::Png, + Some("gif") => OutType::Gif, + Some(ext) => { + eprintln!("unknown filetype '{ext}'!\nSupport output types are: GIF, PNG"); + std::process::exit(1); + } + }; + + Cli { + color_count, + input, + in_type, + output, + out_type, + } +} -- cgit 1.4.1-3-g733a5