#[derive(Debug, Clone, PartialEq)]
pub enum Unit {
Px,
Pt,
Pct,
Deg,
Unknown(String),
}
impl Unit {
pub fn from_annotation(s: &str) -> Self {
match s {
"px" => Self::Px,
"pt" => Self::Pt,
"pct" => Self::Pct,
"deg" => Self::Deg,
other => Self::Unknown(other.to_owned()),
}
}
pub fn as_annotation(&self) -> &str {
match self {
Self::Px => "px",
Self::Pt => "pt",
Self::Pct => "pct",
Self::Deg => "deg",
Self::Unknown(s) => s.as_str(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Dimension {
pub value: f64,
pub unit: Unit,
}
impl Dimension {
pub fn to_kdl_string(&self) -> String {
let value = if self.value.fract() == 0.0 && self.value.is_finite() {
format!("{}", self.value as i64)
} else {
format!("{}", self.value)
};
format!("({}){value}", self.unit.as_annotation())
}
}
pub fn dim_to_px(value: f64, unit: &Unit) -> Option<f64> {
match unit {
Unit::Px => Some(value),
Unit::Pt => Some(value * 96.0 / 72.0),
Unit::Pct | Unit::Deg | Unit::Unknown(_) => None,
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum PropertyValue {
TokenRef(String),
Literal(String),
Dimension(Dimension),
DataRef(String),
}