css_style/
gap.rs

1use crate::{unit::*, Style, StyleUpdater};
2
3/// ```
4/// use css_style::{prelude::*, Gap, unit::{px, em}};
5///
6/// style()
7///     .gap(px(2))
8///     // this can take percent value too (.e.g 40%).
9///     .gap(0.4)
10///     // and can take row and column each with different value
11///     .gap((em(4.), em(8.)))
12///     // we can also use declarative style like this:
13///     .gap(Gap::row(em(4.)).and_column(em(4.)));
14/// ```
15#[derive(Clone, Debug, PartialEq, Display)]
16pub enum Gap {
17    Value(LengthPercent),
18    #[display(fmt = "{} {}", _0, _1)]
19    RowColumn(LengthPercent, LengthPercent),
20}
21
22impl Gap {
23    pub fn row(len: impl Into<LengthPercent>) -> Self {
24        Gap::RowColumn(len.into(), px(0).into())
25    }
26
27    pub fn column(len: impl Into<LengthPercent>) -> Self {
28        Gap::RowColumn(px(0).into(), len.into())
29    }
30
31    pub fn and_row(self, len: impl Into<LengthPercent>) -> Self {
32        match self {
33            Gap::Value(val) => Gap::RowColumn(len.into(), val),
34            Gap::RowColumn(_, col) => Gap::RowColumn(len.into(), col),
35        }
36    }
37
38    pub fn and_column(self, len: impl Into<LengthPercent>) -> Self {
39        match self {
40            Gap::Value(val) => Gap::RowColumn(val, len.into()),
41            Gap::RowColumn(row, _) => Gap::RowColumn(row, len.into()),
42        }
43    }
44}
45
46impl<T> From<T> for Gap
47where
48    T: Into<LengthPercent>,
49{
50    fn from(source: T) -> Self {
51        Gap::Value(source.into())
52    }
53}
54
55impl<T1, T2> From<(T1, T2)> for Gap
56where
57    T1: Into<LengthPercent>,
58    T2: Into<LengthPercent>,
59{
60    fn from((row, col): (T1, T2)) -> Self {
61        let row = row.into();
62        let col = col.into();
63        Gap::RowColumn(row, col)
64    }
65}
66
67impl StyleUpdater for Gap {
68    fn update_style(self, style: Style) -> Style {
69        style.insert("gap", self)
70    }
71}