summaryrefslogtreecommitdiff
path: root/src/model/value.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-01-28 23:36:27 +0100
committerLaurenz <laurmaedje@gmail.com>2023-01-28 23:38:03 +0100
commit1e97d5c8cbeb96d35e5a34a8340c4ec1860fa1b6 (patch)
treeaa4a341af10dc0729132a42cdb1cacb1e1d21518 /src/model/value.rs
parent76048a8ef45ac5892235f2e69cb7cb6c35a037e4 (diff)
Better analysis for literals
Diffstat (limited to 'src/model/value.rs')
-rw-r--r--src/model/value.rs14
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 {