Skip to main content

leptos_css/
gap.rs

1use std::fmt;
2
3use crate::checked_value::impl_checked_value;
4use crate::{
5    LengthPercentageCalculation, NonNegativeLengthPercentage, NonNegativeLengthPercentageValue,
6    serialization::CssWriteTo,
7};
8
9/// One component of the `gap`, `row-gap`, or `column-gap` grammar.
10///
11/// [CSS Box Alignment gap grammar]: https://www.w3.org/TR/css-align-3/#column-row-gap
12#[derive(Clone, Debug, PartialEq)]
13#[non_exhaustive]
14pub enum GapValue {
15    /// The `normal` keyword.
16    Normal,
17    /// A non-negative length or percentage.
18    LengthPercentage(NonNegativeLengthPercentageValue),
19}
20
21impl From<NonNegativeLengthPercentage> for GapValue {
22    fn from(value: NonNegativeLengthPercentage) -> Self {
23        Self::LengthPercentage(value.into())
24    }
25}
26
27impl From<LengthPercentageCalculation> for GapValue {
28    fn from(value: LengthPercentageCalculation) -> Self {
29        Self::LengthPercentage(value.into())
30    }
31}
32
33impl CssWriteTo for GapValue {
34    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
35        match self {
36            Self::Normal => w.write_str("normal"),
37            Self::LengthPercentage(value) => value.css_fmt(w),
38        }
39    }
40}
41
42/// A checked `gap` shorthand value.
43#[derive(Clone, Debug, PartialEq)]
44#[non_exhaustive]
45pub enum Gap {
46    /// One value for both row and column gaps.
47    One(GapValue),
48    /// Separate row and column gaps.
49    RowColumn(GapValue, GapValue),
50}
51
52impl CssWriteTo for Gap {
53    fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
54        match self {
55            Self::One(value) => value.css_fmt(w),
56            Self::RowColumn(row, column) => {
57                row.css_fmt(w)?;
58                w.write_char(' ')?;
59                column.css_fmt(w)
60            }
61        }
62    }
63}
64
65impl_checked_value!(GapValue, Gap);