about summary refs log tree commit diff
path: root/squash/src/cli.rs
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2023-10-09 20:41:03 -0500
committergennyble <gen@nyble.dev>2023-10-09 20:41:03 -0500
commit244c33a07952f0ee22cc3641f35eb5af55f405f1 (patch)
tree8961b7ec53ffde381901ede726c29b3b9ad84730 /squash/src/cli.rs
parent6c2108ebbe8402baa8216d4db92e0444875415d1 (diff)
downloadcolorsquash-244c33a07952f0ee22cc3641f35eb5af55f405f1.tar.gz
colorsquash-244c33a07952f0ee22cc3641f35eb5af55f405f1.zip
cleanup squash
Diffstat (limited to 'squash/src/cli.rs')
-rw-r--r--squash/src/cli.rs85
1 files changed, 85 insertions, 0 deletions
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 <color count> <input> <output>");
+		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::<usize>()) {
+		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,
+	}
+}