From 2bf14aed5e45e956931f114fc6b28dc2bfa7060d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kopel?= Date: Fri, 17 Nov 2023 16:19:53 +0100 Subject: [PATCH] implement initial cli --- .gitignore | 3 +- Cargo.lock | 148 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/fig.rs | 89 +++++++++++++++++++++++++---- 4 files changed, 229 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 61d9034..8a39863 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -.ignore \ No newline at end of file +.ignore +fig \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 89d633c..5e192af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,54 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "anstream" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "approx" version = "0.5.1" @@ -74,12 +122,58 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2275f18819641850fa26c89acc84d465c1bf91ce57bc2748b28c420473352f64" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07cdf1b148b25c1e1f7a42225e30a0d99a615cd4637eae7365548dd4529b95bc" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + [[package]] name = "color_quant" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "conv" version = "0.3.3" @@ -189,6 +283,7 @@ name = "filegram" version = "0.1.0" dependencies = [ "block-padding", + "clap", "image", "imageproc", "kdam", @@ -253,6 +348,12 @@ dependencies = [ "crunchy", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "image" version = "0.24.7" @@ -500,6 +601,15 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + [[package]] name = "qoi" version = "0.4.1" @@ -509,6 +619,15 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rand" version = "0.7.3" @@ -657,6 +776,23 @@ dependencies = [ "lock_api", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "terminal_size" version = "0.3.0" @@ -690,6 +826,18 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 84d8333..217f958 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] block-padding = "0.3.3" +clap = { version = "4.4.8", features = ["derive"] } image = "0.24.7" imageproc = "0.23.0" kdam = "0.5.0" diff --git a/src/bin/fig.rs b/src/bin/fig.rs index d8b625e..3f5366a 100644 --- a/src/bin/fig.rs +++ b/src/bin/fig.rs @@ -1,27 +1,94 @@ use std::{ fs::{self, File}, - io::{BufReader, Error}, + io::{BufReader, Error, ErrorKind}, path::Path, }; +use clap::{Args, Parser, Subcommand}; use filegram::{decode, encode}; use image::ImageFormat; -fn main() -> Result<(), Error> { - { - let file = File::open(".ignore/file_large.txt")?; - let file_size = file.metadata().unwrap().len() as usize; +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Cli { + #[command(subcommand)] + command: Command, +} + +impl Cli { + fn execute(self) -> Result<(), Error> { + let command: Box = match self.command { + Command::Encode(encode) => Box::new(encode), + Command::Decode(decode) => Box::new(decode), + }; + command.execute() + } +} + +#[derive(Subcommand)] +enum Command { + Encode(Encode), + Decode(Decode), +} + +trait CommandTrait { + fn execute(&self) -> Result<(), Error>; + fn default_output(&self) -> String; +} + +#[derive(Args)] +struct Encode { + #[arg(short, long)] + file: String, + #[arg(short, long)] + output: Option, +} + +impl CommandTrait for Encode { + fn execute(&self) -> Result<(), Error> { + let output = self.output.clone().unwrap_or_else(|| self.default_output()); + let file = File::open(self.file.clone())?; + let file_size = file.metadata()?.len() as usize; let mut file = BufReader::new(file); let rgb = encode::to_rgb(&mut file, file_size); - let path = Path::new("file_large.txt.png"); - rgb.save(path).unwrap(); + let path = Path::new(&output); + rgb.save(path) + .map_err(|err| Error::new(ErrorKind::Other, err))?; + Ok(()) + } + + fn default_output(&self) -> String { + self.file.clone() + ".png" } +} + +#[derive(Args)] +struct Decode { + #[arg(short, long)] + file: String, + #[arg(short, long)] + output: Option, +} - { - let file = File::open("file_large.txt.png")?; +impl CommandTrait for Decode { + fn execute(&self) -> Result<(), Error> { + let output = self.output.clone().unwrap_or_else(|| self.default_output()); + let file = File::open(self.file.clone())?; let data = decode::from_file(BufReader::new(file), ImageFormat::Png); - fs::write("file_large_decoded.txt", data)?; + fs::write(output, data)?; + Ok(()) } - Ok(()) + fn default_output(&self) -> String { + match self.file.strip_suffix(".png") { + Some(output) => output.to_string(), + None => self.file.clone() + ".decoded", + } + } +} + +fn main() -> Result<(), Error> { + let cli = Cli::parse(); + + cli.execute() }