use std::fmt;
use crate::checked_value::impl_checked_value;
use crate::{
CssDimension, CssDimensionExpr, DeclarationValue, LengthPercentageAuto,
LengthPercentageCalculation, serialization::CssWriteTo,
};
macro_rules! box_model_shorthand {
($(#[$meta:meta])* $name:ident, $value:ty) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum $name {
Top($value),
Right($value),
Bottom($value),
Left($value),
All($value),
Double($value, $value),
Full($value, $value, $value, $value),
}
impl CssWriteTo for $name {
fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
match self {
Self::Top(size) => {
size.css_fmt(w)?;
w.write_str(" 0 0 0")
}
Self::Right(size) => {
w.write_str("0 ")?;
size.css_fmt(w)?;
w.write_str(" 0 0")
}
Self::Bottom(size) => {
w.write_str("0 0 ")?;
size.css_fmt(w)?;
w.write_str(" 0")
}
Self::Left(size) => {
w.write_str("0 0 0 ")?;
size.css_fmt(w)
}
Self::All(size) => size.css_fmt(w),
Self::Double(vertical, horizontal) => {
vertical.css_fmt(w)?;
w.write_char(' ')?;
horizontal.css_fmt(w)
}
Self::Full(top, right, bottom, left) => {
top.css_fmt(w)?;
w.write_char(' ')?;
right.css_fmt(w)?;
w.write_char(' ')?;
bottom.css_fmt(w)?;
w.write_char(' ')?;
left.css_fmt(w)
}
}
}
}
};
}
box_model_shorthand!(
Margin,
LengthPercentageAuto
);
box_model_shorthand!(
Inset,
LengthPercentageAuto
);
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NonNegativeLengthPercentage(CssDimension);
impl NonNegativeLengthPercentage {
#[track_caller]
pub fn new(value: CssDimension) -> Self {
Self::try_from(value).unwrap_or_else(|error| panic!("{error}"))
}
pub const fn get(self) -> CssDimension {
self.0
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum InvalidNonNegativeLengthPercentage {
Negative(f64),
}
impl fmt::Display for InvalidNonNegativeLengthPercentage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Negative(value) => write!(f, "CSS value must be non-negative, got {value}"),
}
}
}
impl std::error::Error for InvalidNonNegativeLengthPercentage {}
impl TryFrom<CssDimension> for NonNegativeLengthPercentage {
type Error = InvalidNonNegativeLengthPercentage;
fn try_from(value: CssDimension) -> Result<Self, Self::Error> {
let number = match value {
CssDimension::Length(length) => length.value(),
CssDimension::Percent(number) => number.get(),
CssDimension::Zero => 0.0,
};
if number < 0.0 {
Err(InvalidNonNegativeLengthPercentage::Negative(number))
} else {
Ok(Self(value))
}
}
}
impl CssWriteTo for NonNegativeLengthPercentage {
fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
self.0.css_fmt(w)
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum NonNegativeLengthPercentageValue {
Direct(NonNegativeLengthPercentage),
Calculation(LengthPercentageCalculation),
}
impl From<NonNegativeLengthPercentage> for NonNegativeLengthPercentageValue {
fn from(value: NonNegativeLengthPercentage) -> Self {
Self::Direct(value)
}
}
impl From<LengthPercentageCalculation> for NonNegativeLengthPercentageValue {
fn from(value: LengthPercentageCalculation) -> Self {
Self::Calculation(value)
}
}
impl From<NonNegativeLengthPercentage> for DeclarationValue<NonNegativeLengthPercentageValue> {
fn from(value: NonNegativeLengthPercentage) -> Self {
NonNegativeLengthPercentageValue::from(value).into()
}
}
impl From<LengthPercentageCalculation> for DeclarationValue<NonNegativeLengthPercentageValue> {
fn from(value: LengthPercentageCalculation) -> Self {
NonNegativeLengthPercentageValue::from(value).into()
}
}
impl CssWriteTo for NonNegativeLengthPercentageValue {
fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
match self {
Self::Direct(value) => value.css_fmt(w),
Self::Calculation(value) => value.css_fmt(w),
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum BorderCornerRadius {
Circular(NonNegativeLengthPercentageValue),
Elliptical(
NonNegativeLengthPercentageValue,
NonNegativeLengthPercentageValue,
),
}
impl BorderCornerRadius {
pub fn circular(value: CssDimension) -> Self {
Self::Circular(NonNegativeLengthPercentage::new(value).into())
}
pub fn elliptical(horizontal: CssDimension, vertical: CssDimension) -> Self {
Self::Elliptical(
NonNegativeLengthPercentage::new(horizontal).into(),
NonNegativeLengthPercentage::new(vertical).into(),
)
}
pub fn calculated(expression: impl Into<CssDimensionExpr>) -> Self {
Self::Circular(LengthPercentageCalculation::new(expression).into())
}
}
impl CssWriteTo for BorderCornerRadius {
fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
match self {
Self::Circular(radius) => radius.css_fmt(w),
Self::Elliptical(horizontal, vertical) => {
horizontal.css_fmt(w)?;
w.write_char(' ')?;
vertical.css_fmt(w)
}
}
}
}
box_model_shorthand!(
Padding,
NonNegativeLengthPercentageValue
);
impl Padding {
#[track_caller]
pub fn all(value: CssDimension) -> Self {
Self::All(NonNegativeLengthPercentage::new(value).into())
}
#[track_caller]
pub fn double(vertical: CssDimension, horizontal: CssDimension) -> Self {
Self::Double(
NonNegativeLengthPercentage::new(vertical).into(),
NonNegativeLengthPercentage::new(horizontal).into(),
)
}
#[track_caller]
pub fn full(
top: CssDimension,
right: CssDimension,
bottom: CssDimension,
left: CssDimension,
) -> Self {
Self::Full(
NonNegativeLengthPercentage::new(top).into(),
NonNegativeLengthPercentage::new(right).into(),
NonNegativeLengthPercentage::new(bottom).into(),
NonNegativeLengthPercentage::new(left).into(),
)
}
pub fn calculated(expression: impl Into<CssDimensionExpr>) -> Self {
Self::All(LengthPercentageCalculation::new(expression).into())
}
}
macro_rules! axis_shorthand {
($(#[$meta:meta])* $name:ident, $value:ty) => {
$(#[$meta])*
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum $name {
One($value),
StartEnd($value, $value),
}
impl CssWriteTo for $name {
fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
match self {
Self::One(value) => value.css_fmt(w),
Self::StartEnd(start, end) => {
start.css_fmt(w)?;
w.write_char(' ')?;
end.css_fmt(w)
}
}
}
}
};
}
axis_shorthand!(
PaddingAxis,
NonNegativeLengthPercentageValue
);
impl PaddingAxis {
pub fn one(value: CssDimension) -> Self {
Self::One(NonNegativeLengthPercentage::new(value).into())
}
pub fn start_end(start: CssDimension, end: CssDimension) -> Self {
Self::StartEnd(
NonNegativeLengthPercentage::new(start).into(),
NonNegativeLengthPercentage::new(end).into(),
)
}
pub fn calculated(expression: impl Into<CssDimensionExpr>) -> Self {
Self::One(LengthPercentageCalculation::new(expression).into())
}
}
axis_shorthand!(
MarginAxis,
LengthPercentageAuto
);
axis_shorthand!(
InsetAxis,
LengthPercentageAuto
);
impl_checked_value!(
Margin,
Inset,
Padding,
PaddingAxis,
MarginAxis,
InsetAxis,
NonNegativeLengthPercentage,
NonNegativeLengthPercentageValue,
BorderCornerRadius,
);