From 3540fcd34f6dbf136202591655f596e7b3ad5d12 Mon Sep 17 00:00:00 2001 From: Marcelo Hernandez Date: Sun, 13 Oct 2024 01:26:52 -0400 Subject: [PATCH] fix: ensure alpha is always 0.0 - 1.0 inclusive made validate function because custom serde deserializer for options is hard... :c --- src/main.rs | 4 +++- src/opts.rs | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 1835200..4b2ce0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,13 +14,14 @@ use smithay_client_toolkit::{ fn main() -> anyhow::Result<()> { env_logger::init(); let args = DimOpts::parse(); + args.validate()?; if let Some(path) = args.gen_completions { DimOpts::generate_completions(&path)?; return Ok(()); } - let opts = match get_config(args.config.as_deref())? { + let opts = match get_config(args.config.as_deref()).context("Failed to read config!")? { Some(config) => config.merge_onto_self(args), None => args, }; @@ -67,6 +68,7 @@ fn get_config(dir: Option<&Path>) -> anyhow::Result> { let config: DimOpts = toml::from_str(&read_to_string(file)?)?; debug!("Config: {config:?}"); + config.validate()?; Ok(Some(config)) } diff --git a/src/opts.rs b/src/opts.rs index 46e91b6..46e8312 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -1,5 +1,6 @@ use std::path::{Path, PathBuf}; +use anyhow::{anyhow, Result}; use clap::{CommandFactory, Parser, ValueEnum}; use clap_complete::{generate_to, Shell}; use serde::Deserialize; @@ -53,4 +54,14 @@ impl DimOpts { ..self } } + + pub fn validate(&self) -> Result<()> { + if let Some(alpha) = self.alpha { + if !(0.0..=1.0).contains(&alpha) { + return Err(anyhow!("Alpha can only be from 0.0 to 1.0 inclusive.")); + } + } + + Ok(()) + } }