use std::path::PathBuf;
use clap::{Parser, ValueEnum};
use reqwest::Url;
pub (crate) mod util;
#[derive(Debug, Parser)]
#[command(author, version, about)]
pub struct Args {
#[arg(long, long_help, help = "width in dots of the output image")]
#[arg(value_parser = validate_greater_than_zero)]
pub width: Option<u32>,
#[arg(long, long_help, help = "height in dots of output image")]
#[arg(value_parser = validate_greater_than_zero)]
pub height: Option<u32>,
#[arg(long, short, long_help, help = "height in dots of output image")]
pub frame: Option<u32>,
#[arg(long, long_help, default_value = "sierra2", help = "dithering algorithm to use")]
pub dithering: DitheringOption,
#[arg(long, long_help, help = "allow blank braille characters")]
pub allow_blank_chars: bool,
#[arg(long)]
pub invert: bool,
#[arg(long, long_help, default_value = "0.0", help = "adjust contrast")]
pub contrast: f32,
#[arg(long, long_help, default_value = "0", help = "adjust brightness")]
pub brighten: i32,
#[arg(short, action = clap::ArgAction::Count)]
pub verbose: u8,
#[arg(value_parser = parse_mode)]
pub input: Mode
}
#[derive(Debug, Clone)]
pub enum Mode {
File(PathBuf),
Url(Url),
Stdin
}
fn parse_mode(val: &str) -> Result<Mode, &'static str> {
if val == "-" {
return Ok(Mode::Stdin)
}
let path = PathBuf::from(val);
let exists = path.exists();
if exists && path.is_file() {
return Ok(Mode::File(path));
} else if exists {
return Err("the given path exists but is not a file");
}
match Url::try_from(val) {
Ok(url) => match url.scheme().to_ascii_lowercase() {
x if x == "http" || x == "https" => { Ok(Mode::Url(url)) },
_ => { Err("the given URL must be either http or https") }
},
Err(_) => Err("the given input was not a valid argument"),
}
}
fn validate_greater_than_zero(val: &str) -> Result<u32, &'static str> {
match val.parse::<u32>() {
Ok(o) => {
if o > 0 {
Ok(o)
} else {
Err("this argument cannot be 0")
}
},
Err(_) => Err("must be a positive integer"),
}
}
#[derive(Debug, Clone, ValueEnum, Default)]
pub enum DitheringOption {
#[default]
Sierra2,
Bayer4x4,
Bayer2x2,
None,
}