diff options
Diffstat (limited to 'src/model/value.rs')
| -rw-r--r-- | src/model/value.rs | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/model/value.rs b/src/model/value.rs index ea17349e..b860a3f6 100644 --- a/src/model/value.rs +++ b/src/model/value.rs @@ -12,7 +12,7 @@ use super::{ }; use crate::diag::StrResult; use crate::geom::{Abs, Angle, Color, Em, Fr, Length, Ratio, Rel, RgbaColor}; -use crate::syntax::Span; +use crate::syntax::{ast, Span}; use crate::util::{format_eco, EcoString}; /// A computational value. @@ -71,6 +71,18 @@ impl Value { Self::Dyn(Dynamic::new(any)) } + /// Create a numeric value from a number with a unit. + pub fn numeric(pair: (f64, ast::Unit)) -> Self { + let (v, unit) = pair; + match unit { + ast::Unit::Length(unit) => Abs::with_unit(v, unit).into(), + ast::Unit::Angle(unit) => Angle::with_unit(v, unit).into(), + ast::Unit::Em => Em::new(v).into(), + ast::Unit::Fr => Fr::new(v).into(), + ast::Unit::Percent => Ratio::new(v / 100.0).into(), + } + } + /// The name of the stored value's type. pub fn type_name(&self) -> &'static str { match self { |
