-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
229 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
.ignore | ||
.ignore | ||
fig |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |