diff --git a/fj-app/src/args.rs b/fj-app/src/args.rs index f47d14120..16cada701 100644 --- a/fj-app/src/args.rs +++ b/fj-app/src/args.rs @@ -1,4 +1,7 @@ -use std::path::PathBuf; +use std::{path::PathBuf, str::FromStr as _}; + +use fj_kernel::algorithms::Tolerance; +use fj_math::Scalar; /// Fornjot - Experimental CAD System #[derive(clap::Parser)] @@ -16,8 +19,8 @@ pub struct Args { pub parameters: Vec, /// Model deviation tolerance - #[clap[short, long]] - pub tolerance: Option, + #[clap[short, long, parse(try_from_str = parse_tolerance)]] + pub tolerance: Option, } impl Args { @@ -29,3 +32,11 @@ impl Args { ::parse() } } + +fn parse_tolerance(input: &str) -> anyhow::Result { + let tolerance = f64::from_str(input)?; + let tolerance = Scalar::from_f64(tolerance); + let tolerance = Tolerance::from_scalar(tolerance)?; + + Ok(tolerance) +} diff --git a/fj-app/src/main.rs b/fj-app/src/main.rs index ea1e832f4..91008970e 100644 --- a/fj-app/src/main.rs +++ b/fj-app/src/main.rs @@ -242,22 +242,7 @@ struct ShapeProcessor { } impl ShapeProcessor { - fn new(tolerance: Option) -> anyhow::Result { - if let Some(tolerance) = tolerance { - if tolerance <= 0. { - anyhow::bail!( - "Invalid user defined model deviation tolerance: {}.\n\ - Tolerance must be larger than zero", - tolerance - ); - } - } - - let tolerance = tolerance - .map(Scalar::from_f64) - .map(Tolerance::from_scalar) - .map(|result| result.unwrap()); - + fn new(tolerance: Option) -> anyhow::Result { Ok(Self { tolerance }) }