typwire 0.2.0

Typed data exchange for Typst plugins.
Documentation
use serde::{Deserialize, Serialize};

use crate::Length;

/// The corner radii of a rectangle, where `None` leaves a corner at Typst's
/// default.
///
/// Typst has no dedicated runtime type for this: `rect(radius: ..)` takes a
/// plain dictionary of corner keys, so this round-trips as an untagged
/// dictionary.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(from = "RadiusTypst", into = "RadiusTypst", rename_all = "kebab-case")]
pub struct Radius {
    /// The radius of the top-left corner.
    pub top_left: Option<Length>,
    /// The radius of the top-right corner.
    pub top_right: Option<Length>,
    /// The radius of the bottom-left corner.
    pub bottom_left: Option<Length>,
    /// The radius of the bottom-right corner.
    pub bottom_right: Option<Length>,
}

impl Radius {
    /// Creates a new `Radius` from the radius of each corner.
    pub const fn new(
        top_left: Option<Length>,
        top_right: Option<Length>,
        bottom_left: Option<Length>,
        bottom_right: Option<Length>,
    ) -> Self {
        Self {
            top_left,
            top_right,
            bottom_left,
            bottom_right,
        }
    }

    /// Creates a new builder for constructing a `Radius`, which also accepts
    /// per-side and fallback radii as Typst does.
    pub const fn builder() -> RadiusBuilder {
        RadiusBuilder {
            top_left: None,
            top_right: None,
            bottom_left: None,
            bottom_right: None,
            top: None,
            bottom: None,
            left: None,
            right: None,
            rest: None,
        }
    }
}

pub struct RadiusBuilder {
    top_left: Option<Length>,
    top_right: Option<Length>,
    bottom_left: Option<Length>,
    bottom_right: Option<Length>,
    top: Option<Length>,
    bottom: Option<Length>,
    left: Option<Length>,
    right: Option<Length>,
    rest: Option<Length>,
}

impl RadiusBuilder {
    pub const fn top_left(mut self, value: Length) -> Self {
        self.top_left = Some(value);
        self
    }

    pub const fn top_right(mut self, value: Length) -> Self {
        self.top_right = Some(value);
        self
    }

    pub const fn bottom_left(mut self, value: Length) -> Self {
        self.bottom_left = Some(value);
        self
    }

    pub const fn bottom_right(mut self, value: Length) -> Self {
        self.bottom_right = Some(value);
        self
    }

    pub const fn top(mut self, value: Length) -> Self {
        self.top = Some(value);
        self
    }

    pub const fn bottom(mut self, value: Length) -> Self {
        self.bottom = Some(value);
        self
    }

    pub const fn left(mut self, value: Length) -> Self {
        self.left = Some(value);
        self
    }

    pub const fn right(mut self, value: Length) -> Self {
        self.right = Some(value);
        self
    }

    pub const fn rest(mut self, value: Length) -> Self {
        self.rest = Some(value);
        self
    }

    pub fn build(self) -> Radius {
        Radius::new(
            self.top_left
                .or_else(|| self.left.clone())
                .or_else(|| self.top.clone())
                .or_else(|| self.rest.clone()),
            self.top_right
                .or(self.top)
                .or_else(|| self.right.clone())
                .or_else(|| self.rest.clone()),
            self.bottom_left
                .or(self.left)
                .or_else(|| self.bottom.clone())
                .or_else(|| self.rest.clone()),
            self.bottom_right
                .or(self.right)
                .or(self.bottom)
                .or(self.rest),
        )
    }
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct RadiusTypst {
    top_left: Option<Length>,
    top_right: Option<Length>,
    bottom_left: Option<Length>,
    bottom_right: Option<Length>,
    left: Option<Length>,
    top: Option<Length>,
    right: Option<Length>,
    bottom: Option<Length>,
    rest: Option<Length>,
}

impl From<Radius> for RadiusTypst {
    fn from(value: Radius) -> Self {
        Self {
            top_left: value.top_left,
            top_right: value.top_right,
            bottom_left: value.bottom_left,
            bottom_right: value.bottom_right,
            left: None,
            top: None,
            right: None,
            bottom: None,
            rest: None,
        }
    }
}

impl From<RadiusTypst> for Radius {
    fn from(value: RadiusTypst) -> Self {
        Self::new(
            value
                .top_left
                .or_else(|| value.left.clone())
                .or_else(|| value.top.clone())
                .or_else(|| value.rest.clone()),
            value
                .top_right
                .or(value.top)
                .or_else(|| value.right.clone())
                .or_else(|| value.rest.clone()),
            value
                .bottom_left
                .or(value.left)
                .or_else(|| value.bottom.clone())
                .or_else(|| value.rest.clone()),
            value
                .bottom_right
                .or(value.right)
                .or(value.bottom)
                .or(value.rest),
        )
    }
}