Skip to content

Commit

Permalink
implement initial cli
Browse files Browse the repository at this point in the history
  • Loading branch information
PKopel committed Nov 17, 2023
1 parent e1ab6e6 commit 2bf14ae
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.ignore
.ignore
fig
148 changes: 148 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
89 changes: 78 additions & 11 deletions src/bin/fig.rs
Original file line number Diff line number Diff line change
@@ -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<dyn CommandTrait> = 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<String>,
}

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<String>,
}

{
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()
}

0 comments on commit 2bf14ae

Please sign in to comment.