Skip to content

Commit

Permalink
fix: ensure alpha is always 0.0 - 1.0 inclusive
Browse files Browse the repository at this point in the history
made validate function because custom serde deserializer for options is
hard... :c
  • Loading branch information
marcelohdez committed Oct 13, 2024
1 parent e010be3 commit 3540fcd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -67,6 +68,7 @@ fn get_config(dir: Option<&Path>) -> anyhow::Result<Option<DimOpts>> {
let config: DimOpts = toml::from_str(&read_to_string(file)?)?;

debug!("Config: {config:?}");
config.validate()?;
Ok(Some(config))
}

Expand Down
11 changes: 11 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 3540fcd

Please sign in to comment.