fix: make color parsing prettier

This commit is contained in:
Xiretza 2022-06-17 22:33:59 +02:00
parent 1b7fb6aeaa
commit 11a929eec6

View file

@ -4,20 +4,17 @@ use std::{
f32::consts::FRAC_PI_2, f32::consts::FRAC_PI_2,
io::stdout, io::stdout,
net::{Ipv4Addr, SocketAddr, UdpSocket}, net::{Ipv4Addr, SocketAddr, UdpSocket},
num::ParseIntError,
path::PathBuf, path::PathBuf,
str::FromStr,
thread::sleep, thread::sleep,
time::Duration, time::Duration,
}; };
use bracket_color::prelude::{HSV, RGB}; use bracket_color::prelude::{HSV, RGB};
use clap::{Parser, Subcommand, ValueEnum}; use clap::{builder::TypedValueParser, Parser, Subcommand, ValueEnum};
use image::{imageops::FilterType, io::Reader as ImageReader, Pixel, Rgb, RgbImage}; use image::{imageops::FilterType, io::Reader as ImageReader, Pixel, Rgb, RgbImage};
use rand::Rng; use rand::Rng;
use splink_client::{send_frame, Layout}; use splink_client::{send_frame, Layout};
use thiserror::Error;
/// Blinkenwall v3 prototype client /// Blinkenwall v3 prototype client
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -39,42 +36,37 @@ struct Args {
action: Action, action: Action,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy)]
struct Color { struct ColorParser;
r: u8, impl TypedValueParser for ColorParser {
g: u8, type Value = Rgb<u8>;
b: u8,
}
#[derive(Clone, Debug, Error)] fn parse_ref(
enum ColorError { &self,
#[error("Wrong parameter length")] _cmd: &clap::Command,
WrongLength, _arg: Option<&clap::Arg>,
#[error("Illegal integer")] value: &std::ffi::OsStr,
BadNumber(#[from] ParseIntError), ) -> Result<Self::Value, clap::Error> {
} let s = value
.to_str()
impl FromStr for Color { .ok_or(clap::Error::raw(clap::ErrorKind::InvalidUtf8, ""))?;
type Err = ColorError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.strip_prefix('#').unwrap_or(s); let s = s.strip_prefix('#').unwrap_or(s);
if s.len() != 6 { if s.len() != 6 {
return Err(ColorError::WrongLength); return Err(clap::Error::raw(
clap::ErrorKind::InvalidValue,
"Must be a 6-digit hexadecimal number",
));
} }
let r = u8::from_str_radix(&s[0..2], 16)?; let mut channels = [0; 3];
let g = u8::from_str_radix(&s[2..4], 16)?; for i in 0..3 {
let b = u8::from_str_radix(&s[4..6], 16)?; channels[i] = u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).map_err(|_| {
clap::Error::raw(clap::ErrorKind::InvalidValue, "Invalid hex literal")
Ok(Self { r, g, b }) })?;
}
} }
impl From<Color> for Rgb<u8> { Ok(Rgb(channels))
fn from(c: Color) -> Rgb<u8> {
Rgb([c.r, c.g, c.b])
} }
} }
@ -85,7 +77,8 @@ enum Action {
animation: Animation, animation: Animation,
}, },
Solid { Solid {
color: Color, #[clap(value_parser = ColorParser)]
color: Rgb<u8>,
}, },
Image { Image {
path: PathBuf, path: PathBuf,
@ -196,7 +189,7 @@ fn main() -> anyhow::Result<()> {
let image = match args.action { let image = match args.action {
Action::Solid { color } => { Action::Solid { color } => {
RgbImage::from_pixel(layout.width_px(), layout.height_px(), color.into()) RgbImage::from_pixel(layout.width_px(), layout.height_px(), color)
} }
Action::Clear => RgbImage::new(layout.width_px(), layout.height_px()), Action::Clear => RgbImage::new(layout.width_px(), layout.height_px()),
Action::Image { path } => ImageReader::open(path)? Action::Image { path } => ImageReader::open(path)?