diff options
| author | Laurenz <laurmaedje@gmail.com> | 2025-06-19 17:20:17 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2025-06-20 17:32:37 +0200 |
| commit | d821633f50f7f4c9edc49b6ac5e88d43802cb206 (patch) | |
| tree | cf627f96062d1549fce62f90fabd35785b3c99c7 /crates/typst-library/src | |
| parent | 3b35f0cecf37c00d33334be6a596a105167875dd (diff) | |
Generic casting for `Axes<T>`
Diffstat (limited to 'crates/typst-library/src')
| -rw-r--r-- | crates/typst-library/src/layout/axes.rs | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/crates/typst-library/src/layout/axes.rs b/crates/typst-library/src/layout/axes.rs index 7a73ba79..e4303f98 100644 --- a/crates/typst-library/src/layout/axes.rs +++ b/crates/typst-library/src/layout/axes.rs @@ -4,9 +4,12 @@ use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Deref, Not}; use typst_utils::Get; -use crate::diag::bail; -use crate::foundations::{array, cast, Array, Resolve, Smart, StyleChain}; -use crate::layout::{Abs, Dir, Length, Ratio, Rel, Size}; +use crate::diag::{bail, HintedStrResult}; +use crate::foundations::{ + array, cast, Array, CastInfo, FromValue, IntoValue, Reflect, Resolve, Smart, + StyleChain, Value, +}; +use crate::layout::{Abs, Dir, Rel, Size}; /// A container with a horizontal and vertical component. #[derive(Default, Copy, Clone, Eq, PartialEq, Hash)] @@ -275,40 +278,39 @@ impl BitAndAssign for Axes<bool> { } } -cast! { - Axes<Rel<Length>>, - self => array![self.x, self.y].into_value(), - array: Array => { - let mut iter = array.into_iter(); - match (iter.next(), iter.next(), iter.next()) { - (Some(a), Some(b), None) => Axes::new(a.cast()?, b.cast()?), - _ => bail!("point array must contain exactly two entries"), - } - }, +impl<T: Reflect> Reflect for Axes<T> { + fn input() -> CastInfo { + Array::input() + } + + fn output() -> CastInfo { + Array::output() + } + + fn castable(value: &Value) -> bool { + Array::castable(value) + } } -cast! { - Axes<Ratio>, - self => array![self.x, self.y].into_value(), - array: Array => { +impl<T: FromValue> FromValue for Axes<T> { + fn from_value(value: Value) -> HintedStrResult<Self> { + let array = value.cast::<Array>()?; let mut iter = array.into_iter(); match (iter.next(), iter.next(), iter.next()) { - (Some(a), Some(b), None) => Axes::new(a.cast()?, b.cast()?), - _ => bail!("ratio array must contain exactly two entries"), + (Some(a), Some(b), None) => Ok(Axes::new(a.cast()?, b.cast()?)), + _ => bail!( + "array must contain exactly two items"; + hint: "the first item determines the value for the X axis \ + and the second item the value for the Y axis" + ), } - }, + } } -cast! { - Axes<Length>, - self => array![self.x, self.y].into_value(), - array: Array => { - let mut iter = array.into_iter(); - match (iter.next(), iter.next(), iter.next()) { - (Some(a), Some(b), None) => Axes::new(a.cast()?, b.cast()?), - _ => bail!("length array must contain exactly two entries"), - } - }, +impl<T: IntoValue> IntoValue for Axes<T> { + fn into_value(self) -> Value { + array![self.x.into_value(), self.y.into_value()].into_value() + } } impl<T: Resolve> Resolve for Axes<T> { |
