diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-10-28 16:43:38 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-10-28 16:43:38 +0200 |
| commit | 95e9134a3c7d7a14d8c8928413fdffc665671895 (patch) | |
| tree | 822b5f6c2d23aba33cfe4d199405e493e37c3d70 /src/model/ops.rs | |
| parent | 66030ae5d73d85a0463562230b87f8ec7554c746 (diff) | |
Refactor `geom` module
Diffstat (limited to 'src/model/ops.rs')
| -rw-r--r-- | src/model/ops.rs | 37 |
1 files changed, 13 insertions, 24 deletions
diff --git a/src/model/ops.rs b/src/model/ops.rs index c521f704..c7d6ac78 100644 --- a/src/model/ops.rs +++ b/src/model/ops.rs @@ -2,9 +2,9 @@ use std::cmp::Ordering; -use super::{RawAlign, RawLength, RawStroke, Regex, Smart, Value}; +use super::{RawAlign, RawStroke, Regex, Smart, Value}; use crate::diag::StrResult; -use crate::geom::{Numeric, Relative, Spec, SpecAxis}; +use crate::geom::{Axes, Axis, Length, Numeric, Rel}; use Value::*; /// Bail with a type mismatch error. @@ -106,8 +106,8 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> { { if a.axis() != b.axis() { Value::dynamic(match a.axis() { - SpecAxis::Horizontal => Spec { x: a, y: b }, - SpecAxis::Vertical => Spec { x: b, y: a }, + Axis::X => Axes { x: a, y: b }, + Axis::Y => Axes { x: b, y: a }, }) } else { return Err(format!("cannot add two {:?} alignments", a.axis())); @@ -203,8 +203,8 @@ pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> { (Length(a), Int(b)) => Length(a / b as f64), (Length(a), Float(b)) => Length(a / b), - (Length(a), Length(b)) => Float(div_length(a, b)?), - (Length(a), Relative(b)) if b.rel.is_zero() => Float(div_length(a, b.abs)?), + (Length(a), Length(b)) => Float(try_div_length(a, b)?), + (Length(a), Relative(b)) if b.rel.is_zero() => Float(try_div_length(a, b.abs)?), (Angle(a), Int(b)) => Angle(a / b as f64), (Angle(a), Float(b)) => Angle(a / b), @@ -217,9 +217,9 @@ pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> { (Relative(a), Int(b)) => Relative(a / b as f64), (Relative(a), Float(b)) => Relative(a / b), - (Relative(a), Length(b)) if a.rel.is_zero() => Float(div_length(a.abs, b)?), + (Relative(a), Length(b)) if a.rel.is_zero() => Float(try_div_length(a.abs, b)?), (Relative(a), Ratio(b)) if a.abs.is_zero() => Float(a.rel / b), - (Relative(a), Relative(b)) => Float(div_relative(a, b)?), + (Relative(a), Relative(b)) => Float(try_div_relative(a, b)?), (Fraction(a), Int(b)) => Fraction(a / b as f64), (Fraction(a), Float(b)) => Fraction(a / b), @@ -230,25 +230,14 @@ pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> { } /// Try to divide two lengths. -fn div_length(a: RawLength, b: RawLength) -> StrResult<f64> { - if a.length.is_zero() && b.length.is_zero() { - Ok(a.em / b.em) - } else if a.em.is_zero() && b.em.is_zero() { - Ok(a.length / b.length) - } else { - return Err("cannot divide these two lengths".into()); - } +fn try_div_length(a: Length, b: Length) -> StrResult<f64> { + a.try_div(b).ok_or_else(|| "cannot divide these two lengths".into()) } /// Try to divide two relative lengths. -fn div_relative(a: Relative<RawLength>, b: Relative<RawLength>) -> StrResult<f64> { - if a.rel.is_zero() && b.rel.is_zero() { - div_length(a.abs, b.abs) - } else if a.abs.is_zero() && b.abs.is_zero() { - Ok(a.rel / b.rel) - } else { - return Err("cannot divide these two relative lengths".into()); - } +fn try_div_relative(a: Rel<Length>, b: Rel<Length>) -> StrResult<f64> { + a.try_div(b) + .ok_or_else(|| "cannot divide these two relative lengths".into()) } /// Compute the logical "not" of a value. |
