use serde::{Deserialize, Serialize};
use crate::tag::{Tag, Tagged};
const TYPE_NAME: &str = "length";
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(from = "LengthTypst", into = "LengthTypst")]
pub struct Length {
points: f64,
}
impl Length {
pub const fn new(points: f64) -> Self {
Self { points }
}
pub const fn pt(&self) -> f64 {
self.points
}
pub const fn mm(&self) -> f64 {
self.points * (25.4 / 72.0)
}
pub const fn cm(&self) -> f64 {
self.points * (25.4 / 720.0)
}
pub const fn inches(&self) -> f64 {
self.points / 72.0
}
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct LengthTypst {
typwire_type: Tag<Length>,
points: f64,
}
impl Tagged for Length {
const TYPE_NAME: &'static str = TYPE_NAME;
}
impl From<Length> for LengthTypst {
fn from(value: Length) -> Self {
Self {
typwire_type: Tag::new(),
points: value.points,
}
}
}
impl From<LengthTypst> for Length {
fn from(value: LengthTypst) -> Self {
Length::new(value.points)
}
}