liquidrust 0.2.2

A simple Rust application for displaying information and setting RGB colors for the Corsair H115i RGB PRO XT AIO.
Documentation
extern crate hidapi;
mod colors;
mod fans;
mod hid;
mod info;
mod pump;
mod utils;

use clap::Parser;
use colors::{gradient, parse_color, rainbow, set_color, set_colors};
use hid::get_device;
use info::print_measurements;
use pump::PumpMode;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
  /// Output in JSON format
  #[arg(short, long)]
  json: bool,
  /// RGB color
  #[arg(short, long)]
  color: Option<String>,
  /// Gradient color 1
  #[arg(short = 'a', long)]
  gradient1: Option<String>,
  /// Gradient color 2
  #[arg(short = 'b', long)]
  gradient2: Option<String>,
  /// Rainbow
  #[arg(short, long)]
  rainbow: bool,
  /// Device info & measurements
  #[arg(short, long)]
  info: bool,
  /// Set the pump mode
  #[arg(short, long, value_enum)]
  pump: Option<PumpMode>,
  /// Set the fan speed percentage
  #[arg(short, long)]
  fan: Option<u32>,
  /// Test
  #[arg(short, long)]
  test: bool,
}

fn main() {
  let args = Args::parse();
  let mut print_info = args.info;
  if let Some(device) = get_device(0x1b1c, 0x0c21) {
    if let Some(ref color_str) = args.color {
      match parse_color(color_str) {
        Ok(color) => {
          println!("Setting single color to #{:06X}", color);
          set_color(&device, color)
        }
        Err(e) => eprintln!("Error: {}", e),
      }
    }
    if let (Some(ref gradient1), Some(ref gradient2)) =
      (args.gradient1.as_ref(), args.gradient2.as_ref())
    {
      match (parse_color(gradient1), parse_color(gradient2)) {
        (Ok(start_color), Ok(end_color)) => {
          println!(
            "Setting hex color gradient from #{:06X} to #{:06X}",
            start_color, end_color
          );
          let colors = gradient(start_color, end_color);
          set_colors(&device, colors);
        }
        (Err(err), _) | (_, Err(err)) => {
          eprintln!("Error: {}", err);
        }
      }
    }
    if args.rainbow {
      println!("Setting rainbow colors");
      let colors = rainbow();
      set_colors(&device, colors);
    }
    if args.pump.is_some() {
      pump::set_pump_mode(&device, args.pump.unwrap().value());
    }
    if let Some(fan) = args.fan {
      fans::set_fan_mode(&device, fan);
    }
    if args.color.is_none()
      && args.gradient1.is_none()
      && !args.rainbow
      && !args.info
      && !args.test
      && args.fan.is_none()
      && args.pump.is_none()
    {
      print_info = true;
    }
    if print_info {
      print_measurements(&device, args.json);
    }
  }
}