tailwind_rs_core/utilities/grid/
placement.rs

1//! Grid placement utilities for tailwind-rs
2
3use crate::classes::ClassBuilder;
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7/// Grid column start values
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub enum GridColumnStart {
10    /// Start at column 1
11    Start1,
12    /// Start at column 2
13    Start2,
14    /// Start at column 3
15    Start3,
16    /// Start at column 4
17    Start4,
18    /// Start at column 5
19    Start5,
20    /// Start at column 6
21    Start6,
22    /// Start at column 7
23    Start7,
24    /// Start at column 8
25    Start8,
26    /// Start at column 9
27    Start9,
28    /// Start at column 10
29    Start10,
30    /// Start at column 11
31    Start11,
32    /// Start at column 12
33    Start12,
34    /// Start at column 13
35    Start13,
36    /// Auto start
37    Auto,
38}
39
40/// Grid column end values
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
42pub enum GridColumnEnd {
43    /// End at column 1
44    End1,
45    /// End at column 2
46    End2,
47    /// End at column 3
48    End3,
49    /// End at column 4
50    End4,
51    /// End at column 5
52    End5,
53    /// End at column 6
54    End6,
55    /// End at column 7
56    End7,
57    /// End at column 8
58    End8,
59    /// End at column 9
60    End9,
61    /// End at column 10
62    End10,
63    /// End at column 11
64    End11,
65    /// End at column 12
66    End12,
67    /// End at column 13
68    End13,
69    /// Auto end
70    Auto,
71}
72
73/// Grid row start values
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
75pub enum GridRowStart {
76    /// Start at row 1
77    Start1,
78    /// Start at row 2
79    Start2,
80    /// Start at row 3
81    Start3,
82    /// Start at row 4
83    Start4,
84    /// Start at row 5
85    Start5,
86    /// Start at row 6
87    Start6,
88    /// Start at row 7
89    Start7,
90    /// Auto start
91    Auto,
92}
93
94/// Grid row end values
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
96pub enum GridRowEnd {
97    /// End at row 1
98    End1,
99    /// End at row 2
100    End2,
101    /// End at row 3
102    End3,
103    /// End at row 4
104    End4,
105    /// End at row 5
106    End5,
107    /// End at row 6
108    End6,
109    /// End at row 7
110    End7,
111    /// Auto end
112    Auto,
113}
114
115impl fmt::Display for GridColumnStart {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        match self {
118            GridColumnStart::Start1 => write!(f, "col-start-1"),
119            GridColumnStart::Start2 => write!(f, "col-start-2"),
120            GridColumnStart::Start3 => write!(f, "col-start-3"),
121            GridColumnStart::Start4 => write!(f, "col-start-4"),
122            GridColumnStart::Start5 => write!(f, "col-start-5"),
123            GridColumnStart::Start6 => write!(f, "col-start-6"),
124            GridColumnStart::Start7 => write!(f, "col-start-7"),
125            GridColumnStart::Start8 => write!(f, "col-start-8"),
126            GridColumnStart::Start9 => write!(f, "col-start-9"),
127            GridColumnStart::Start10 => write!(f, "col-start-10"),
128            GridColumnStart::Start11 => write!(f, "col-start-11"),
129            GridColumnStart::Start12 => write!(f, "col-start-12"),
130            GridColumnStart::Start13 => write!(f, "col-start-13"),
131            GridColumnStart::Auto => write!(f, "col-start-auto"),
132        }
133    }
134}
135
136impl fmt::Display for GridColumnEnd {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        match self {
139            GridColumnEnd::End1 => write!(f, "col-end-1"),
140            GridColumnEnd::End2 => write!(f, "col-end-2"),
141            GridColumnEnd::End3 => write!(f, "col-end-3"),
142            GridColumnEnd::End4 => write!(f, "col-end-4"),
143            GridColumnEnd::End5 => write!(f, "col-end-5"),
144            GridColumnEnd::End6 => write!(f, "col-end-6"),
145            GridColumnEnd::End7 => write!(f, "col-end-7"),
146            GridColumnEnd::End8 => write!(f, "col-end-8"),
147            GridColumnEnd::End9 => write!(f, "col-end-9"),
148            GridColumnEnd::End10 => write!(f, "col-end-10"),
149            GridColumnEnd::End11 => write!(f, "col-end-11"),
150            GridColumnEnd::End12 => write!(f, "col-end-12"),
151            GridColumnEnd::End13 => write!(f, "col-end-13"),
152            GridColumnEnd::Auto => write!(f, "col-end-auto"),
153        }
154    }
155}
156
157impl fmt::Display for GridRowStart {
158    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159        match self {
160            GridRowStart::Start1 => write!(f, "row-start-1"),
161            GridRowStart::Start2 => write!(f, "row-start-2"),
162            GridRowStart::Start3 => write!(f, "row-start-3"),
163            GridRowStart::Start4 => write!(f, "row-start-4"),
164            GridRowStart::Start5 => write!(f, "row-start-5"),
165            GridRowStart::Start6 => write!(f, "row-start-6"),
166            GridRowStart::Start7 => write!(f, "row-start-7"),
167            GridRowStart::Auto => write!(f, "row-start-auto"),
168        }
169    }
170}
171
172impl fmt::Display for GridRowEnd {
173    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174        match self {
175            GridRowEnd::End1 => write!(f, "row-end-1"),
176            GridRowEnd::End2 => write!(f, "row-end-2"),
177            GridRowEnd::End3 => write!(f, "row-end-3"),
178            GridRowEnd::End4 => write!(f, "row-end-4"),
179            GridRowEnd::End5 => write!(f, "row-end-5"),
180            GridRowEnd::End6 => write!(f, "row-end-6"),
181            GridRowEnd::End7 => write!(f, "row-end-7"),
182            GridRowEnd::Auto => write!(f, "row-end-auto"),
183        }
184    }
185}
186
187/// Trait for adding grid placement utilities to a class builder
188pub trait GridPlacementUtilities {
189    fn grid_column_start(self, start: GridColumnStart) -> Self;
190    fn grid_column_end(self, end: GridColumnEnd) -> Self;
191    fn grid_row_start(self, start: GridRowStart) -> Self;
192    fn grid_row_end(self, end: GridRowEnd) -> Self;
193}
194
195impl GridPlacementUtilities for ClassBuilder {
196    fn grid_column_start(self, start: GridColumnStart) -> Self {
197        self.class(start.to_string())
198    }
199    
200    fn grid_column_end(self, end: GridColumnEnd) -> Self {
201        self.class(end.to_string())
202    }
203    
204    fn grid_row_start(self, start: GridRowStart) -> Self {
205        self.class(start.to_string())
206    }
207    
208    fn grid_row_end(self, end: GridRowEnd) -> Self {
209        self.class(end.to_string())
210    }
211}
212
213#[cfg(test)]
214mod tests {
215    use super::*;
216
217    #[test]
218    fn test_grid_placement_display() {
219        assert_eq!(GridColumnStart::Start1.to_string(), "col-start-1");
220        assert_eq!(GridColumnEnd::End1.to_string(), "col-end-1");
221        assert_eq!(GridRowStart::Start1.to_string(), "row-start-1");
222        assert_eq!(GridRowEnd::End1.to_string(), "row-end-1");
223    }
224
225    #[test]
226    fn test_grid_placement_utilities() {
227        let classes = ClassBuilder::new()
228            .grid_column_start(GridColumnStart::Start1)
229            .grid_column_end(GridColumnEnd::End3)
230            .grid_row_start(GridRowStart::Start1)
231            .grid_row_end(GridRowEnd::End2)
232            .build();
233        
234        let css_classes = classes.to_css_classes();
235        assert!(css_classes.contains("col-start-1"));
236        assert!(css_classes.contains("col-end-3"));
237        assert!(css_classes.contains("row-start-1"));
238        assert!(css_classes.contains("row-end-2"));
239    }
240}