takumi_css/style/properties/grid/
grid_auto_flow.rs1use cssparser::Parser;
2
3use crate::style::{CssToken, FromCss, MakeComputed, ParseResult, ToCss};
4
5#[derive(Debug, Clone, Copy, PartialEq, Default)]
7#[non_exhaustive]
8pub enum GridDirection {
9 #[default]
11 Row,
12 Column,
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Default)]
18#[non_exhaustive]
19pub struct GridAutoFlow {
20 pub direction: GridDirection,
22 pub dense: bool,
24}
25
26impl MakeComputed for GridAutoFlow {}
27
28impl From<GridAutoFlow> for taffy::GridAutoFlow {
29 fn from(value: GridAutoFlow) -> Self {
30 match (value.direction, value.dense) {
31 (GridDirection::Row, false) => taffy::GridAutoFlow::Row,
32 (GridDirection::Column, false) => taffy::GridAutoFlow::Column,
33 (GridDirection::Row, true) => taffy::GridAutoFlow::RowDense,
34 (GridDirection::Column, true) => taffy::GridAutoFlow::ColumnDense,
35 }
36 }
37}
38
39impl GridAutoFlow {
40 pub const fn row() -> Self {
42 Self {
43 direction: GridDirection::Row,
44 dense: false,
45 }
46 }
47
48 pub const fn column() -> Self {
50 Self {
51 direction: GridDirection::Column,
52 dense: false,
53 }
54 }
55
56 pub const fn dense(self) -> Self {
58 Self {
59 dense: true,
60 ..self
61 }
62 }
63}
64
65impl<'i> FromCss<'i> for GridAutoFlow {
66 fn from_css(input: &mut Parser<'i, '_>) -> ParseResult<'i, Self> {
67 let mut direction = GridDirection::default();
68 let mut dense = false;
69
70 loop {
71 if input.is_exhausted() {
72 break;
73 }
74
75 if input
76 .try_parse(|input| input.expect_ident_matching("dense"))
77 .is_ok()
78 {
79 dense = true;
80 continue;
81 }
82
83 if input
84 .try_parse(|input| input.expect_ident_matching("row"))
85 .is_ok()
86 {
87 direction = GridDirection::Row;
88 continue;
89 }
90
91 if input
92 .try_parse(|input| input.expect_ident_matching("column"))
93 .is_ok()
94 {
95 direction = GridDirection::Column;
96 continue;
97 }
98
99 return Err(input.new_error_for_next_token());
100 }
101
102 Ok(GridAutoFlow { direction, dense })
103 }
104
105 const VALID_TOKENS: &'static [CssToken] = &[
106 CssToken::Keyword("row"),
107 CssToken::Keyword("column"),
108 CssToken::Keyword("dense"),
109 ];
110}
111
112impl ToCss for GridAutoFlow {
113 fn to_css<W: std::fmt::Write>(&self, dest: &mut W) -> std::fmt::Result {
114 match self.direction {
115 GridDirection::Row => dest.write_str("row")?,
116 GridDirection::Column => dest.write_str("column")?,
117 }
118 if self.dense {
119 dest.write_str(" dense")?;
120 }
121 Ok(())
122 }
123}