diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-06-06 21:13:59 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-06-06 22:06:16 +0200 |
| commit | fd417da04f7ca4b995de7f6510abafd3e9c31307 (patch) | |
| tree | 3675529c75ca7363701ac8ea306de2cc1d3cbcb3 /src/doc.rs | |
| parent | 168bdf35bd773e67343c965cb473492cc5cae9e7 (diff) | |
Improve value casting infrastructure
Diffstat (limited to 'src/doc.rs')
| -rw-r--r-- | src/doc.rs | 56 |
1 files changed, 25 insertions, 31 deletions
@@ -8,7 +8,7 @@ use std::sync::Arc; use ecow::EcoString; -use crate::eval::{cast_from_value, cast_to_value, dict, Dict, Value}; +use crate::eval::{cast, dict, Dict, Value}; use crate::font::Font; use crate::geom::{ self, rounded_rect, Abs, Align, Axes, Color, Corners, Dir, Em, Geometry, Length, @@ -567,15 +567,12 @@ impl FromStr for Lang { } } -cast_from_value! { +cast! { Lang, + self => self.as_str().into_value(), string: EcoString => Self::from_str(&string)?, } -cast_to_value! { - v: Lang => v.as_str().into() -} - /// An identifier for a region somewhere in the world. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct Region([u8; 2]); @@ -608,15 +605,12 @@ impl FromStr for Region { } } -cast_from_value! { +cast! { Region, + self => self.as_str().into_value(), string: EcoString => Self::from_str(&string)?, } -cast_to_value! { - v: Region => v.as_str().into() -} - /// Meta information that isn't visible or renderable. #[derive(Clone, PartialEq, Hash)] pub enum Meta { @@ -633,6 +627,10 @@ pub enum Meta { Hide, } +cast! { + type Meta: "meta", +} + impl Debug for Meta { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { @@ -644,10 +642,6 @@ impl Debug for Meta { } } -cast_from_value! { - Meta: "meta", -} - /// A link destination. #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub enum Destination { @@ -659,21 +653,18 @@ pub enum Destination { Location(Location), } -cast_from_value! { +cast! { Destination, + self => match self { + Self::Url(v) => v.into_value(), + Self::Position(v) => v.into_value(), + Self::Location(v) => v.into_value(), + }, v: EcoString => Self::Url(v), v: Position => Self::Position(v), v: Location => Self::Location(v), } -cast_to_value! { - v: Destination => match v { - Destination::Url(v) => v.into(), - Destination::Position(v) => v.into(), - Destination::Location(v) => v.into(), - } -} - /// A physical position in a document. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct Position { @@ -683,8 +674,9 @@ pub struct Position { pub point: Point, } -cast_from_value! { +cast! { Position, + self => Value::Dict(self.into()), mut dict: Dict => { let page = dict.take("page")?.cast()?; let x: Length = dict.take("x")?.cast()?; @@ -694,12 +686,14 @@ cast_from_value! { }, } -cast_to_value! { - v: Position => Value::Dict(dict! { - "page" => Value::Int(v.page.get() as i64), - "x" => Value::Length(v.point.x.into()), - "y" => Value::Length(v.point.y.into()), - }) +impl From<Position> for Dict { + fn from(pos: Position) -> Self { + dict! { + "page" => pos.page, + "x" => pos.point.x, + "y" => pos.point.y, + } + } } #[cfg(test)] |
