use anyhow::Result;
use clap::{Args, ValueEnum};
use openlogi_core::color::Rgb;
use openlogi_hid::LightingMethod;
use super::select_device;
const LIGHTING_FEATURES: &[u16] = &[0x8070, 0x8071, 0x8080, 0x8081];
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Method {
Auto,
Effects,
Perkey,
Perkeyv2,
}
impl From<Method> for LightingMethod {
fn from(m: Method) -> Self {
match m {
Method::Auto => Self::Auto,
Method::Effects => Self::Effects,
Method::Perkey => Self::PerKey,
Method::Perkeyv2 => Self::PerKeyV2,
}
}
}
#[derive(Debug, Args)]
pub struct LightingArgs {
pub color: String,
#[arg(long, value_name = "NAME")]
pub device: Option<String>,
#[arg(long, value_enum, default_value_t = Method::Auto)]
pub method: Method,
}
pub async fn run(args: LightingArgs) -> Result<()> {
let color: Rgb = args.color.trim_start_matches('#').parse()?;
let (r, g, b) = color.components();
let (route, name) = select_device(args.device.as_deref(), LIGHTING_FEATURES).await?;
let method: LightingMethod = args.method.into();
println!("setting {name} ({route}) to #{r:02x}{g:02x}{b:02x} via {method:?}");
openlogi_hid::set_keyboard_color_with(&route, method, r, g, b).await?;
println!("done — {name} should now be solid #{r:02x}{g:02x}{b:02x}");
Ok(())
}
#[cfg(test)]
mod color_validation_tests {
use openlogi_core::color::RgbParseError;
use super::{LightingArgs, Method, run};
fn args(color: &str) -> LightingArgs {
LightingArgs {
color: color.to_string(),
device: None,
method: Method::Auto,
}
}
#[tokio::test]
async fn rejects_malformed_colors_before_touching_hardware() {
for bad in ["zzz", "ff000", "ff00001", "gg0000", ""] {
let err = run(args(bad)).await.unwrap_err();
assert!(
err.downcast_ref::<RgbParseError>().is_some(),
"{bad:?} should fail Rgb parsing, got: {err}"
);
}
}
#[tokio::test]
async fn hash_prefix_is_stripped_before_validation() {
let err = run(args("#zzzzzz")).await.unwrap_err();
let parse = err
.downcast_ref::<RgbParseError>()
.expect("Rgb parse error");
assert_eq!(
parse.to_string(),
r#"invalid RGB color "zzzzzz": expected 6 hex digits ("RRGGBB", no '#')"#
);
}
}