From 7caf914a26be9e0e9303abf7dd414f1a818bdd2f Mon Sep 17 00:00:00 2001 From: clizia Date: Tue, 12 Nov 2024 21:52:57 +0100 Subject: [PATCH] making it usable from cli --- src/args.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/args.rs b/src/args.rs index e69de29..2695975 100644 --- a/src/args.rs +++ b/src/args.rs @@ -0,0 +1,39 @@ +use clap::{Parser, ValueEnum}; + +#[derive(Parser, Debug)] +#[command(version, about)] +pub struct Args { + #[arg(long, value_enum, required = true)] + pub mode: Mode, + + #[arg( + short, + long, + required = true, + )] + pub input_file: Option, + + #[arg( + short, + long, + required_if_eq("mode", "Mode::Encode"), + required_if_eq("mode", "Mode::Decode"), + required_if_eq("mode", "Mode::Remove") + )] + pub chunk_type: Option, + + #[arg( + short, + long, + required_if_eq("mode", "Mode::Encode") + )] + pub message: Option, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] +pub enum Mode { + Encode, + Decode, + Print, + Remove +} diff --git a/src/main.rs b/src/main.rs index daec155..524582d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,58 @@ mod chunk_type; mod commands; mod png; +use core::panic; +use std::path::PathBuf; +use std::fs::File; + +use args::Mode; +use chunk_type::ChunkType; +use clap::Parser; +use crate::args::Args; + pub type Error = Box; pub type Result = std::result::Result; -fn main() -> Result<()> { +fn get_image(path: &str) -> File { + let image_path: PathBuf = PathBuf::from(path); + let path_display = image_path.display(); + + let image = match File::open(&image_path) { + Err(why) => panic!("couldn't open {}: {}", path_display, why), + Ok(file) => file + }; + + image +} + +fn encode(image: File, chunk_type: ChunkType, message: &str) { todo!() } + +fn decode(image: File, chunk_type: ChunkType) { + todo!() +} + +fn print_message(image: File) { + todo!() +} + +fn remove_message(image: File, chunk_type: ChunkType) { + todo!() +} + +fn main() -> Result<()> { + let args = Args::parse(); + + let image = get_image(&args.input_file.unwrap()); + + match args.mode { + Mode::Encode => todo!(), + Mode::Decode => todo!(), + Mode::Print => todo!(), + Mode::Remove => todo!(), + _ => Err("not a valid mode")? + } + + Ok(()) +}