diff --git a/crates/typst-library/src/layout/axes.rs b/crates/typst-library/src/layout/axes.rs index 7a73ba796..e4303f98b 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 { } } -cast! { - Axes>, - 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 Reflect for Axes { + fn input() -> CastInfo { + Array::input() + } + + fn output() -> CastInfo { + Array::output() + } + + fn castable(value: &Value) -> bool { + Array::castable(value) + } } -cast! { - Axes, - self => array![self.x, self.y].into_value(), - array: Array => { +impl FromValue for Axes { + fn from_value(value: Value) -> HintedStrResult { + let array = value.cast::()?; 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, - 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 IntoValue for Axes { + fn into_value(self) -> Value { + array![self.x.into_value(), self.y.into_value()].into_value() + } } impl Resolve for Axes { diff --git a/tests/suite/visualize/line.typ b/tests/suite/visualize/line.typ index 6cbbbb493..4c763030f 100644 --- a/tests/suite/visualize/line.typ +++ b/tests/suite/visualize/line.typ @@ -84,7 +84,8 @@ --- line-bad-point-array --- // Test errors. -// Error: 12-19 point array must contain exactly two entries +// Error: 12-19 array must contain exactly two items +// Hint: 12-19 the first item determines the value for the X axis and the second item the value for the Y axis #line(end: (50pt,)) --- line-bad-point-component-type --- diff --git a/tests/suite/visualize/path.typ b/tests/suite/visualize/path.typ index e44b2270e..795fde981 100644 --- a/tests/suite/visualize/path.typ +++ b/tests/suite/visualize/path.typ @@ -76,7 +76,8 @@ #path(((0%, 0%), (0%, 0%), (0%, 0%), (0%, 0%))) --- path-bad-point-array --- -// Error: 7-31 point array must contain exactly two entries +// Error: 7-31 array must contain exactly two items +// Hint: 7-31 the first item determines the value for the X axis and the second item the value for the Y axis // Warning: 2-6 the `path` function is deprecated, use `curve` instead #path(((0%, 0%), (0%, 0%, 0%))) diff --git a/tests/suite/visualize/polygon.typ b/tests/suite/visualize/polygon.typ index ec27194df..6cc243d2b 100644 --- a/tests/suite/visualize/polygon.typ +++ b/tests/suite/visualize/polygon.typ @@ -49,7 +49,8 @@ ) --- polygon-bad-point-array --- -// Error: 10-17 point array must contain exactly two entries +// Error: 10-17 array must contain exactly two items +// Hint: 10-17 the first item determines the value for the X axis and the second item the value for the Y axis #polygon((50pt,)) --- polygon-infinite-size ---