From fd417da04f7ca4b995de7f6510abafd3e9c31307 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 6 Jun 2023 21:13:59 +0200 Subject: Improve value casting infrastructure --- library/src/visualize/path.rs | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'library/src/visualize/path.rs') diff --git a/library/src/visualize/path.rs b/library/src/visualize/path.rs index c9b596c7..641095c5 100644 --- a/library/src/visualize/path.rs +++ b/library/src/visualize/path.rs @@ -1,6 +1,9 @@ -use self::PathVertex::{AllControlPoints, MirroredControlPoint, Vertex}; -use crate::prelude::*; use kurbo::{CubicBez, ParamCurveExtrema}; +use typst::eval::Reflect; + +use crate::prelude::*; + +use PathVertex::{AllControlPoints, MirroredControlPoint, Vertex}; /// A path through a list of points, connected by Bezier curves. /// @@ -179,8 +182,13 @@ impl PathVertex { } } -cast_from_value! { +cast! { PathVertex, + self => match self { + Vertex(x) => x.into_value(), + MirroredControlPoint(x, c) => array![x, c].into_value(), + AllControlPoints(x, c1, c2) => array![x, c1, c2].into_value(), + }, array: Array => { let mut iter = array.into_iter(); match (iter.next(), iter.next(), iter.next(), iter.next()) { @@ -188,7 +196,7 @@ cast_from_value! { Vertex(a.cast()?) }, (Some(a), Some(b), None, None) => { - if Axes::>::is(&a) { + if Axes::>::castable(&a) { MirroredControlPoint(a.cast()?, b.cast()?) } else { Vertex(Axes::new(a.cast()?, b.cast()?)) @@ -201,19 +209,3 @@ cast_from_value! { } }, } - -cast_to_value! { - v: PathVertex => { - match v { - PathVertex::Vertex(x) => { - Value::from(x) - }, - PathVertex::MirroredControlPoint(x, c) => { - Value::Array(array![x, c]) - }, - PathVertex::AllControlPoints(x, c1, c2) => { - Value::Array(array![x, c1, c2]) - }, - } - } -} -- cgit v1.2.3