taffy/style/
grid.rs

1//! Style types for CSS Grid layout
2use super::{
3    AlignContent, AlignItems, AlignSelf, CheapCloneStr, CompactLength, CoreStyle, Dimension, JustifyContent,
4    LengthPercentage, LengthPercentageAuto, Style,
5};
6use crate::compute::grid::{GridCoordinate, GridLine, OriginZeroLine};
7use crate::geometry::{AbsoluteAxis, AbstractAxis, Line, MinMax, Size};
8use crate::style_helpers::*;
9use crate::sys::{DefaultCheapStr, Vec};
10use core::cmp::{max, min};
11use core::fmt::Debug;
12
13/// Defines a grid area
14#[derive(Debug, Clone, PartialEq)]
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16pub struct GridTemplateArea<CustomIdent: CheapCloneStr> {
17    /// The name of the grid area which
18    #[cfg_attr(feature = "serde", serde(deserialize_with = "crate::util::deserialize_from_str"))]
19    pub name: CustomIdent,
20    /// The index of the row at which the grid area starts in grid coordinates.
21    pub row_start: u16,
22    /// The index of the row at which the grid area ends in grid coordinates.
23    pub row_end: u16,
24    /// The index of the column at which the grid area starts in grid coordinates.
25    pub column_start: u16,
26    /// The index of the column at which the grid area end in grid coordinates.
27    pub column_end: u16,
28}
29
30/// Defines a named grid line
31#[derive(Debug, Clone, PartialEq)]
32#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
33pub struct NamedGridLine<CustomIdent: CheapCloneStr> {
34    /// The name of the grid area which
35    #[cfg_attr(feature = "serde", serde(deserialize_with = "crate::util::deserialize_from_str"))]
36    pub name: CustomIdent,
37    /// The index of the row at which the grid area starts in grid coordinates.
38    pub index: u16,
39}
40
41/// Axis as `Row` or `Column`
42#[derive(Debug, Clone, Copy, PartialEq)]
43pub(crate) enum GridAreaAxis {
44    /// The `Row` axis
45    Row,
46    /// The `Column` axis
47    Column,
48}
49
50/// Logical end (`Start` or `End`)
51#[derive(Debug, Clone, Copy, PartialEq)]
52pub(crate) enum GridAreaEnd {
53    /// The `Start` end
54    Start,
55    /// The `End` end
56    End,
57}
58
59/// A trait to represent a `repeat()` clause in a `grid-template-*` definition
60pub trait GenericRepetition {
61    /// The type that represents `<custom-ident>`s (for named lines)
62    type CustomIdent: CheapCloneStr;
63    /// The type which represents an iterator over the list of repeated tracks
64    type RepetitionTrackList<'a>: Iterator<Item = TrackSizingFunction> + ExactSizeIterator + Clone
65    where
66        Self: 'a;
67
68    /// A nested iterator of line names (nested because each line may have multiple associated names)
69    type TemplateLineNames<'a>: TemplateLineNames<'a, Self::CustomIdent>
70    where
71        Self: 'a;
72    /// The repetition count (integer, auto-fill, or auto-fit)
73    fn count(&self) -> RepetitionCount;
74    /// Get an iterator over the repeated tracks
75    fn tracks(&self) -> Self::RepetitionTrackList<'_>;
76    /// Returns the number of repeated tracks
77    fn track_count(&self) -> u16 {
78        self.tracks().len() as u16
79    }
80    /// Returns an iterator over the lines names
81    fn lines_names(&self) -> Self::TemplateLineNames<'_>;
82}
83
84/// A nested list of line names. This is effectively a generic representation of `Vec<Vec<String>>` that allows
85/// both the collection and string type to be customised.
86#[rustfmt::skip]
87pub trait TemplateLineNames<'a, S: CheapCloneStr> : Iterator<Item = Self::LineNameSet<'a>> + ExactSizeIterator + Clone where Self: 'a {
88    /// A simple list line names. This is effectively a generic representation of `VecString>` that allows
89    /// both the collection and string type to be customised.
90    type LineNameSet<'b>: Iterator<Item = &'b S> + ExactSizeIterator + Clone where Self: 'b;
91}
92
93impl<'a, S: CheapCloneStr> TemplateLineNames<'a, S>
94    for core::iter::Map<core::slice::Iter<'a, Vec<S>>, fn(&Vec<S>) -> core::slice::Iter<'_, S>>
95{
96    type LineNameSet<'b>
97        = core::slice::Iter<'b, S>
98    where
99        Self: 'b;
100}
101
102#[derive(Copy, Clone)]
103/// A type representing a component in a `grid-template-*` defintion where the type
104/// representing `repeat()`s is generic
105pub enum GenericGridTemplateComponent<S, Repetition>
106where
107    S: CheapCloneStr,
108    Repetition: GenericRepetition<CustomIdent = S>,
109{
110    /// A single track sizing function
111    Single(TrackSizingFunction),
112    /// A `repeat()`
113    Repeat(Repetition),
114}
115
116impl<S, Repetition> GenericGridTemplateComponent<S, Repetition>
117where
118    S: CheapCloneStr,
119    Repetition: GenericRepetition<CustomIdent = S>,
120{
121    /// Whether the track definition is a auto-repeated fragment
122    pub fn is_auto_repetition(&self) -> bool {
123        match self {
124            Self::Single(_) => false,
125            Self::Repeat(repeat) => matches!(repeat.count(), RepetitionCount::AutoFit | RepetitionCount::AutoFill),
126        }
127    }
128}
129
130/// The set of styles required for a CSS Grid container
131pub trait GridContainerStyle: CoreStyle {
132    /// The type for a `repeat()` within a grid_template_rows or grid_template_columns
133    type Repetition<'a>: GenericRepetition<CustomIdent = Self::CustomIdent>
134    where
135        Self: 'a;
136
137    /// The type returned by grid_template_rows and grid_template_columns
138    type TemplateTrackList<'a>: Iterator<Item = GenericGridTemplateComponent<Self::CustomIdent, Self::Repetition<'a>>>
139        + ExactSizeIterator
140        + Clone
141    where
142        Self: 'a;
143
144    /// The type returned by grid_auto_rows and grid_auto_columns
145    type AutoTrackList<'a>: Iterator<Item = TrackSizingFunction> + ExactSizeIterator + Clone
146    where
147        Self: 'a;
148
149    /// The type returned by grid_template_row_names and grid_template_column_names
150    //IntoIterator<Item = &'a Self::LineNameSet<'a>>
151    type TemplateLineNames<'a>: TemplateLineNames<'a, Self::CustomIdent>
152    where
153        Self: 'a;
154
155    /// The type of custom identifiers used to identify named grid lines and areas
156    type GridTemplateAreas<'a>: IntoIterator<Item = GridTemplateArea<Self::CustomIdent>>
157    where
158        Self: 'a;
159
160    // FIXME: re-add default implemenations for grid_{template,auto}_{rows,columns} once the
161    // associated_type_defaults feature (https://github.com/rust-lang/rust/issues/29661) is stabilised.
162
163    /// Defines the track sizing functions (heights) of the grid rows
164    fn grid_template_rows(&self) -> Option<Self::TemplateTrackList<'_>>;
165    /// Defines the track sizing functions (widths) of the grid columns
166    fn grid_template_columns(&self) -> Option<Self::TemplateTrackList<'_>>;
167    /// Defines the size of implicitly created rows
168    fn grid_auto_rows(&self) -> Self::AutoTrackList<'_>;
169    /// Defined the size of implicitly created columns
170    fn grid_auto_columns(&self) -> Self::AutoTrackList<'_>;
171
172    /// Named grid areas
173    fn grid_template_areas(&self) -> Option<Self::GridTemplateAreas<'_>>;
174    /// Defines the line names for row lines
175    fn grid_template_column_names(&self) -> Option<Self::TemplateLineNames<'_>>;
176    /// Defines the size of implicitly created rows
177    fn grid_template_row_names(&self) -> Option<Self::TemplateLineNames<'_>>;
178
179    /// Controls how items get placed into the grid for auto-placed items
180    #[inline(always)]
181    fn grid_auto_flow(&self) -> GridAutoFlow {
182        Style::<Self::CustomIdent>::DEFAULT.grid_auto_flow
183    }
184
185    /// How large should the gaps between items in a grid or flex container be?
186    #[inline(always)]
187    fn gap(&self) -> Size<LengthPercentage> {
188        Style::<Self::CustomIdent>::DEFAULT.gap
189    }
190
191    // Alignment properties
192
193    /// How should content contained within this item be aligned in the cross/block axis
194    #[inline(always)]
195    fn align_content(&self) -> Option<AlignContent> {
196        Style::<Self::CustomIdent>::DEFAULT.align_content
197    }
198    /// How should contained within this item be aligned in the main/inline axis
199    #[inline(always)]
200    fn justify_content(&self) -> Option<JustifyContent> {
201        Style::<Self::CustomIdent>::DEFAULT.justify_content
202    }
203    /// How this node's children aligned in the cross/block axis?
204    #[inline(always)]
205    fn align_items(&self) -> Option<AlignItems> {
206        Style::<Self::CustomIdent>::DEFAULT.align_items
207    }
208    /// How this node's children should be aligned in the inline axis
209    #[inline(always)]
210    fn justify_items(&self) -> Option<AlignItems> {
211        Style::<Self::CustomIdent>::DEFAULT.justify_items
212    }
213
214    /// Get a grid item's row or column placement depending on the axis passed
215    #[inline(always)]
216    fn grid_template_tracks(&self, axis: AbsoluteAxis) -> Option<Self::TemplateTrackList<'_>> {
217        match axis {
218            AbsoluteAxis::Horizontal => self.grid_template_columns(),
219            AbsoluteAxis::Vertical => self.grid_template_rows(),
220        }
221    }
222
223    /// Get a grid container's align-content or justify-content alignment depending on the axis passed
224    #[inline(always)]
225    fn grid_align_content(&self, axis: AbstractAxis) -> AlignContent {
226        match axis {
227            AbstractAxis::Inline => self.justify_content().unwrap_or(AlignContent::Stretch),
228            AbstractAxis::Block => self.align_content().unwrap_or(AlignContent::Stretch),
229        }
230    }
231}
232
233/// The set of styles required for a CSS Grid item (child of a CSS Grid container)
234pub trait GridItemStyle: CoreStyle {
235    /// Defines which row in the grid the item should start and end at
236    #[inline(always)]
237    fn grid_row(&self) -> Line<GridPlacement<Self::CustomIdent>> {
238        Default::default()
239    }
240    /// Defines which column in the grid the item should start and end at
241    #[inline(always)]
242    fn grid_column(&self) -> Line<GridPlacement<Self::CustomIdent>> {
243        Default::default()
244    }
245
246    /// How this node should be aligned in the cross/block axis
247    /// Falls back to the parents [`AlignItems`] if not set
248    #[inline(always)]
249    fn align_self(&self) -> Option<AlignSelf> {
250        Style::<Self::CustomIdent>::DEFAULT.align_self
251    }
252    /// How this node should be aligned in the inline axis
253    /// Falls back to the parents [`super::JustifyItems`] if not set
254    #[inline(always)]
255    fn justify_self(&self) -> Option<AlignSelf> {
256        Style::<Self::CustomIdent>::DEFAULT.justify_self
257    }
258
259    /// Get a grid item's row or column placement depending on the axis passed
260    #[inline(always)]
261    fn grid_placement(&self, axis: AbsoluteAxis) -> Line<GridPlacement<Self::CustomIdent>> {
262        match axis {
263            AbsoluteAxis::Horizontal => self.grid_column(),
264            AbsoluteAxis::Vertical => self.grid_row(),
265        }
266    }
267}
268
269/// Controls whether grid items are placed row-wise or column-wise. And whether the sparse or dense packing algorithm is used.
270///
271/// The "dense" packing algorithm attempts to fill in holes earlier in the grid, if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items.
272///
273/// Defaults to [`GridAutoFlow::Row`]
274///
275/// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow)
276#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
277#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
278pub enum GridAutoFlow {
279    /// Items are placed by filling each row in turn, adding new rows as necessary
280    #[default]
281    Row,
282    /// Items are placed by filling each column in turn, adding new columns as necessary.
283    Column,
284    /// Combines `Row` with the dense packing algorithm.
285    RowDense,
286    /// Combines `Column` with the dense packing algorithm.
287    ColumnDense,
288}
289
290impl GridAutoFlow {
291    /// Whether grid auto placement uses the sparse placement algorithm or the dense placement algorithm
292    /// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow#values>
293    pub fn is_dense(&self) -> bool {
294        match self {
295            Self::Row | Self::Column => false,
296            Self::RowDense | Self::ColumnDense => true,
297        }
298    }
299
300    /// Whether grid auto placement fills areas row-wise or column-wise
301    /// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow#values>
302    pub fn primary_axis(&self) -> AbsoluteAxis {
303        match self {
304            Self::Row | Self::RowDense => AbsoluteAxis::Horizontal,
305            Self::Column | Self::ColumnDense => AbsoluteAxis::Vertical,
306        }
307    }
308}
309
310/// A grid line placement specification which is generic over the coordinate system that it uses to define
311/// grid line positions.
312///
313/// GenericGridPlacement<GridLine> is aliased as GridPlacement and is exposed to users of Taffy to define styles.
314/// GenericGridPlacement<OriginZeroLine> is aliased as OriginZeroGridPlacement and is used internally for placement computations.
315///
316/// See [`crate::compute::grid::type::coordinates`] for documentation on the different coordinate systems.
317#[derive(Copy, Clone, PartialEq, Eq, Debug)]
318#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
319pub enum GenericGridPlacement<LineType: GridCoordinate> {
320    /// Place item according to the auto-placement algorithm, and the parent's grid_auto_flow property
321    Auto,
322    /// Place item at specified line (column or row) index
323    Line(LineType),
324    /// Item should span specified number of tracks (columns or rows)
325    Span(u16),
326}
327
328/// A grid line placement using the normalized OriginZero coordinates to specify line positions.
329pub(crate) type OriginZeroGridPlacement = GenericGridPlacement<OriginZeroLine>;
330
331/// A grid line placement using CSS grid line coordinates to specify line positions. This uses the same coordinate
332/// system as the public `GridPlacement` type but doesn't support named lines (these are expected to have already
333/// been resolved by the time values of this type are constructed).
334pub(crate) type NonNamedGridPlacement = GenericGridPlacement<GridLine>;
335
336/// A grid line placement specification. Used for grid-[row/column]-[start/end]. Named tracks are not implemented.
337///
338/// Defaults to `GridPlacement::Auto`
339///
340/// [Specification](https://www.w3.org/TR/css3-grid-layout/#typedef-grid-row-start-grid-line)
341#[derive(Clone, PartialEq, Debug, Default)]
342#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
343pub enum GridPlacement<S: CheapCloneStr = DefaultCheapStr> {
344    /// Place item according to the auto-placement algorithm, and the parent's grid_auto_flow property
345    #[default]
346    Auto,
347    /// Place item at specified line (column or row) index
348    Line(GridLine),
349    /// Place item at specified named line (column or row)
350    NamedLine(S, i16),
351    /// Item should span specified number of tracks (columns or rows)
352    Span(u16),
353    /// Item should span until the nth line named <name>.
354    ///
355    /// If there are less than n lines named <name> in the specified direction then
356    /// all implicit lines will be counted.
357    NamedSpan(S, u16),
358}
359impl<S: CheapCloneStr> TaffyAuto for GridPlacement<S> {
360    const AUTO: Self = Self::Auto;
361}
362impl<S: CheapCloneStr> TaffyGridLine for GridPlacement<S> {
363    fn from_line_index(index: i16) -> Self {
364        GridPlacement::<S>::Line(GridLine::from(index))
365    }
366}
367impl<S: CheapCloneStr> TaffyGridLine for Line<GridPlacement<S>> {
368    fn from_line_index(index: i16) -> Self {
369        Line { start: GridPlacement::<S>::from_line_index(index), end: GridPlacement::<S>::Auto }
370    }
371}
372impl<S: CheapCloneStr> TaffyGridSpan for GridPlacement<S> {
373    fn from_span(span: u16) -> Self {
374        GridPlacement::<S>::Span(span)
375    }
376}
377impl<S: CheapCloneStr> TaffyGridSpan for Line<GridPlacement<S>> {
378    fn from_span(span: u16) -> Self {
379        Line { start: GridPlacement::<S>::from_span(span), end: GridPlacement::<S>::Auto }
380    }
381}
382
383impl<S: CheapCloneStr> GridPlacement<S> {
384    /// Apply a mapping function if the [`GridPlacement`] is a `Line`. Otherwise return `self` unmodified.
385    pub fn into_origin_zero_placement_ignoring_named(&self, explicit_track_count: u16) -> OriginZeroGridPlacement {
386        match self {
387            Self::Auto => OriginZeroGridPlacement::Auto,
388            Self::Span(span) => OriginZeroGridPlacement::Span(*span),
389            // Grid line zero is an invalid index, so it gets treated as Auto
390            // See: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row-start#values
391            Self::Line(line) => match line.as_i16() {
392                0 => OriginZeroGridPlacement::Auto,
393                _ => OriginZeroGridPlacement::Line(line.into_origin_zero_line(explicit_track_count)),
394            },
395            Self::NamedLine(_, _) => OriginZeroGridPlacement::Auto,
396            Self::NamedSpan(_, _) => OriginZeroGridPlacement::Auto,
397        }
398    }
399}
400
401impl<S: CheapCloneStr> Line<GridPlacement<S>> {
402    /// Apply a mapping function if the [`GridPlacement`] is a `Line`. Otherwise return `self` unmodified.
403    pub fn into_origin_zero_ignoring_named(&self, explicit_track_count: u16) -> Line<OriginZeroGridPlacement> {
404        Line {
405            start: self.start.into_origin_zero_placement_ignoring_named(explicit_track_count),
406            end: self.end.into_origin_zero_placement_ignoring_named(explicit_track_count),
407        }
408    }
409}
410
411impl NonNamedGridPlacement {
412    /// Apply a mapping function if the [`GridPlacement`] is a `Track`. Otherwise return `self` unmodified.
413    pub fn into_origin_zero_placement(
414        &self,
415        explicit_track_count: u16,
416        // resolve_named: impl Fn(&str) -> Option<GridLine>
417    ) -> OriginZeroGridPlacement {
418        match self {
419            Self::Auto => OriginZeroGridPlacement::Auto,
420            Self::Span(span) => OriginZeroGridPlacement::Span(*span),
421            // Grid line zero is an invalid index, so it gets treated as Auto
422            // See: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row-start#values
423            Self::Line(line) => match line.as_i16() {
424                0 => OriginZeroGridPlacement::Auto,
425                _ => OriginZeroGridPlacement::Line(line.into_origin_zero_line(explicit_track_count)),
426            },
427        }
428    }
429}
430
431impl<T: GridCoordinate> Line<GenericGridPlacement<T>> {
432    /// Resolves the span for an indefinite placement (a placement that does not consist of two `Track`s).
433    /// Panics if called on a definite placement
434    pub fn indefinite_span(&self) -> u16 {
435        use GenericGridPlacement as GP;
436        match (self.start, self.end) {
437            (GP::Line(_), GP::Auto) => 1,
438            (GP::Auto, GP::Line(_)) => 1,
439            (GP::Auto, GP::Auto) => 1,
440            (GP::Line(_), GP::Span(span)) => span,
441            (GP::Span(span), GP::Line(_)) => span,
442            (GP::Span(span), GP::Auto) => span,
443            (GP::Auto, GP::Span(span)) => span,
444            (GP::Span(span), GP::Span(_)) => span,
445            (GP::Line(_), GP::Line(_)) => panic!("indefinite_span should only be called on indefinite grid tracks"),
446        }
447    }
448}
449
450impl<S: CheapCloneStr> Line<GridPlacement<S>> {
451    #[inline]
452    /// Whether the track position is definite in this axis (or the item will need auto placement)
453    /// The track position is definite if least one of the start and end positions is a NON-ZERO track index
454    /// (0 is an invalid line in GridLine coordinates, and falls back to "auto" which is indefinite)
455    pub fn is_definite(&self) -> bool {
456        match (&self.start, &self.end) {
457            (GridPlacement::Line(line), _) if line.as_i16() != 0 => true,
458            (_, GridPlacement::Line(line)) if line.as_i16() != 0 => true,
459            (GridPlacement::NamedLine(_, _), _) => true,
460            (_, GridPlacement::NamedLine(_, _)) => true,
461            _ => false,
462        }
463    }
464}
465
466impl Line<NonNamedGridPlacement> {
467    #[inline]
468    /// Whether the track position is definite in this axis (or the item will need auto placement)
469    /// The track position is definite if least one of the start and end positions is a NON-ZERO track index
470    /// (0 is an invalid line in GridLine coordinates, and falls back to "auto" which is indefinite)
471    pub fn is_definite(&self) -> bool {
472        match (&self.start, &self.end) {
473            (GenericGridPlacement::Line(line), _) if line.as_i16() != 0 => true,
474            (_, GenericGridPlacement::Line(line)) if line.as_i16() != 0 => true,
475            _ => false,
476        }
477    }
478
479    /// Apply a mapping function if the [`GridPlacement`] is a `Track`. Otherwise return `self` unmodified.
480    pub fn into_origin_zero(&self, explicit_track_count: u16) -> Line<OriginZeroGridPlacement> {
481        Line {
482            start: self.start.into_origin_zero_placement(explicit_track_count),
483            end: self.end.into_origin_zero_placement(explicit_track_count),
484        }
485    }
486}
487
488impl Line<OriginZeroGridPlacement> {
489    #[inline]
490    /// Whether the track position is definite in this axis (or the item will need auto placement)
491    /// The track position is definite if least one of the start and end positions is a track index
492    pub fn is_definite(&self) -> bool {
493        matches!((self.start, self.end), (GenericGridPlacement::Line(_), _) | (_, GenericGridPlacement::Line(_)))
494    }
495
496    /// If at least one of the of the start and end positions is a track index then the other end can be resolved
497    /// into a track index purely based on the information contained with the placement specification
498    pub fn resolve_definite_grid_lines(&self) -> Line<OriginZeroLine> {
499        use OriginZeroGridPlacement as GP;
500        match (self.start, self.end) {
501            (GP::Line(line1), GP::Line(line2)) => {
502                if line1 == line2 {
503                    Line { start: line1, end: line1 + 1 }
504                } else {
505                    Line { start: min(line1, line2), end: max(line1, line2) }
506                }
507            }
508            (GP::Line(line), GP::Span(span)) => Line { start: line, end: line + span },
509            (GP::Line(line), GP::Auto) => Line { start: line, end: line + 1 },
510            (GP::Span(span), GP::Line(line)) => Line { start: line - span, end: line },
511            (GP::Auto, GP::Line(line)) => Line { start: line - 1, end: line },
512            _ => panic!("resolve_definite_grid_tracks should only be called on definite grid tracks"),
513        }
514    }
515
516    /// For absolutely positioned items:
517    ///   - Tracks resolve to definite tracks
518    ///   - For Spans:
519    ///      - If the other position is a Track, they resolve to a definite track relative to the other track
520    ///      - Else resolve to None
521    ///   - Auto resolves to None
522    ///
523    /// When finally positioning the item, a value of None means that the item's grid area is bounded by the grid
524    /// container's border box on that side.
525    pub fn resolve_absolutely_positioned_grid_tracks(&self) -> Line<Option<OriginZeroLine>> {
526        use OriginZeroGridPlacement as GP;
527        match (self.start, self.end) {
528            (GP::Line(track1), GP::Line(track2)) => {
529                if track1 == track2 {
530                    Line { start: Some(track1), end: Some(track1 + 1) }
531                } else {
532                    Line { start: Some(min(track1, track2)), end: Some(max(track1, track2)) }
533                }
534            }
535            (GP::Line(track), GP::Span(span)) => Line { start: Some(track), end: Some(track + span) },
536            (GP::Line(track), GP::Auto) => Line { start: Some(track), end: None },
537            (GP::Span(span), GP::Line(track)) => Line { start: Some(track - span), end: Some(track) },
538            (GP::Auto, GP::Line(track)) => Line { start: None, end: Some(track) },
539            _ => Line { start: None, end: None },
540        }
541    }
542
543    /// If neither of the start and end positions is a track index then the other end can be resolved
544    /// into a track index if a definite start position is supplied externally
545    pub fn resolve_indefinite_grid_tracks(&self, start: OriginZeroLine) -> Line<OriginZeroLine> {
546        use OriginZeroGridPlacement as GP;
547        match (self.start, self.end) {
548            (GP::Auto, GP::Auto) => Line { start, end: start + 1 },
549            (GP::Span(span), GP::Auto) => Line { start, end: start + span },
550            (GP::Auto, GP::Span(span)) => Line { start, end: start + span },
551            (GP::Span(span), GP::Span(_)) => Line { start, end: start + span },
552            _ => panic!("resolve_indefinite_grid_tracks should only be called on indefinite grid tracks"),
553        }
554    }
555}
556
557/// Represents the start and end points of a GridItem within a given axis
558impl<S: CheapCloneStr> Default for Line<GridPlacement<S>> {
559    fn default() -> Self {
560        Line { start: GridPlacement::<S>::Auto, end: GridPlacement::<S>::Auto }
561    }
562}
563
564/// Maximum track sizing function
565///
566/// Specifies the maximum size of a grid track. A grid track will automatically size between it's minimum and maximum size based
567/// on the size of it's contents, the amount of available space, and the sizing constraint the grid is being size under.
568/// See <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns>
569#[derive(Copy, Clone, PartialEq, Debug)]
570#[cfg_attr(feature = "serde", derive(Serialize))]
571pub struct MaxTrackSizingFunction(pub(crate) CompactLength);
572impl TaffyZero for MaxTrackSizingFunction {
573    const ZERO: Self = Self(CompactLength::ZERO);
574}
575impl TaffyAuto for MaxTrackSizingFunction {
576    const AUTO: Self = Self(CompactLength::AUTO);
577}
578impl TaffyMinContent for MaxTrackSizingFunction {
579    const MIN_CONTENT: Self = Self(CompactLength::MIN_CONTENT);
580}
581impl TaffyMaxContent for MaxTrackSizingFunction {
582    const MAX_CONTENT: Self = Self(CompactLength::MAX_CONTENT);
583}
584impl FromLength for MaxTrackSizingFunction {
585    fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
586        Self::length(value.into())
587    }
588}
589impl FromPercent for MaxTrackSizingFunction {
590    fn from_percent<Input: Into<f32> + Copy>(value: Input) -> Self {
591        Self::percent(value.into())
592    }
593}
594impl TaffyFitContent for MaxTrackSizingFunction {
595    fn fit_content(argument: LengthPercentage) -> Self {
596        Self(CompactLength::fit_content(argument))
597    }
598}
599impl FromFr for MaxTrackSizingFunction {
600    fn from_fr<Input: Into<f32> + Copy>(value: Input) -> Self {
601        Self::fr(value.into())
602    }
603}
604impl From<LengthPercentage> for MaxTrackSizingFunction {
605    fn from(input: LengthPercentage) -> Self {
606        Self(input.0)
607    }
608}
609impl From<LengthPercentageAuto> for MaxTrackSizingFunction {
610    fn from(input: LengthPercentageAuto) -> Self {
611        Self(input.0)
612    }
613}
614impl From<Dimension> for MaxTrackSizingFunction {
615    fn from(input: Dimension) -> Self {
616        Self(input.0)
617    }
618}
619impl From<MinTrackSizingFunction> for MaxTrackSizingFunction {
620    fn from(input: MinTrackSizingFunction) -> Self {
621        Self(input.0)
622    }
623}
624#[cfg(feature = "serde")]
625impl<'de> serde::Deserialize<'de> for MaxTrackSizingFunction {
626    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
627    where
628        D: serde::Deserializer<'de>,
629    {
630        let inner = CompactLength::deserialize(deserializer)?;
631        // Note: validation intentionally excludes the CALC_TAG as deserializing calc() values is not supported
632        if matches!(
633            inner.tag(),
634            CompactLength::LENGTH_TAG
635                | CompactLength::PERCENT_TAG
636                | CompactLength::AUTO_TAG
637                | CompactLength::MIN_CONTENT_TAG
638                | CompactLength::MAX_CONTENT_TAG
639                | CompactLength::FIT_CONTENT_PX_TAG
640                | CompactLength::FIT_CONTENT_PERCENT_TAG
641                | CompactLength::FR_TAG
642        ) {
643            Ok(Self(inner))
644        } else {
645            Err(serde::de::Error::custom("Invalid tag"))
646        }
647    }
648}
649
650impl MaxTrackSizingFunction {
651    /// An absolute length in some abstract units. Users of Taffy may define what they correspond
652    /// to in their application (pixels, logical pixels, mm, etc) as they see fit.
653    #[inline(always)]
654    pub const fn length(val: f32) -> Self {
655        Self(CompactLength::length(val))
656    }
657
658    /// A percentage length relative to the size of the containing block.
659    ///
660    /// **NOTE: percentages are represented as a f32 value in the range [0.0, 1.0] NOT the range [0.0, 100.0]**
661    #[inline(always)]
662    pub const fn percent(val: f32) -> Self {
663        Self(CompactLength::percent(val))
664    }
665
666    /// The dimension should be automatically computed according to algorithm-specific rules
667    /// regarding the default size of boxes.
668    #[inline(always)]
669    pub const fn auto() -> Self {
670        Self(CompactLength::auto())
671    }
672
673    /// The size should be the "min-content" size.
674    /// This is the smallest size that can fit the item's contents with ALL soft line-wrapping opportunities taken
675    #[inline(always)]
676    pub const fn min_content() -> Self {
677        Self(CompactLength::min_content())
678    }
679
680    /// The size should be the "max-content" size.
681    /// This is the smallest size that can fit the item's contents with NO soft line-wrapping opportunities taken
682    #[inline(always)]
683    pub const fn max_content() -> Self {
684        Self(CompactLength::max_content())
685    }
686
687    /// The size should be computed according to the "fit content" formula:
688    ///    `max(min_content, min(max_content, limit))`
689    /// where:
690    ///    - `min_content` is the [min-content](Self::min_content) size
691    ///    - `max_content` is the [max-content](Self::max_content) size
692    ///    - `limit` is a LENGTH value passed to this function
693    ///
694    /// The effect of this is that the item takes the size of `limit` clamped
695    /// by the min-content and max-content sizes.
696    #[inline(always)]
697    pub const fn fit_content_px(limit: f32) -> Self {
698        Self(CompactLength::fit_content_px(limit))
699    }
700
701    /// The size should be computed according to the "fit content" formula:
702    ///    `max(min_content, min(max_content, limit))`
703    /// where:
704    ///    - `min_content` is the [min-content](Self::min_content) size
705    ///    - `max_content` is the [max-content](Self::max_content) size
706    ///    - `limit` is a PERCENTAGE value passed to this function
707    ///
708    /// The effect of this is that the item takes the size of `limit` clamped
709    /// by the min-content and max-content sizes.
710    #[inline(always)]
711    pub const fn fit_content_percent(limit: f32) -> Self {
712        Self(CompactLength::fit_content_percent(limit))
713    }
714
715    /// The dimension as a fraction of the total available grid space (`fr` units in CSS)
716    /// Specified value is the numerator of the fraction. Denominator is the sum of all fraction specified in that grid dimension
717    /// Spec: <https://www.w3.org/TR/css3-grid-layout/#fr-unit>
718    #[inline(always)]
719    pub const fn fr(val: f32) -> Self {
720        Self(CompactLength::fr(val))
721    }
722
723    /// A `calc()` value. The value passed here is treated as an opaque handle to
724    /// the actual calc representation and may be a pointer, index, etc.
725    ///
726    /// The low 3 bits are used as a tag value and will be returned as 0.
727    #[inline]
728    #[cfg(feature = "calc")]
729    pub fn calc(ptr: *const ()) -> Self {
730        Self(CompactLength::calc(ptr))
731    }
732
733    /// Create a LengthPercentageAuto from a raw `CompactLength`.
734    /// # Safety
735    /// CompactLength must represent a valid variant for LengthPercentageAuto
736    #[allow(unsafe_code)]
737    pub unsafe fn from_raw(val: CompactLength) -> Self {
738        Self(val)
739    }
740
741    /// Get the underlying `CompactLength` representation of the value
742    pub fn into_raw(self) -> CompactLength {
743        self.0
744    }
745
746    /// Returns true if the max track sizing function is `MinContent`, `MaxContent`, `FitContent` or `Auto`, else false.
747    #[inline(always)]
748    pub fn is_intrinsic(&self) -> bool {
749        self.0.is_intrinsic()
750    }
751
752    /// Returns true if the max track sizing function is `MaxContent`, `FitContent` or `Auto` else false.
753    /// "In all cases, treat auto and fit-content() as max-content, except where specified otherwise for fit-content()."
754    /// See: <https://www.w3.org/TR/css-grid-1/#algo-terms>
755    #[inline(always)]
756    pub fn is_max_content_alike(&self) -> bool {
757        self.0.is_max_content_alike()
758    }
759
760    /// Returns true if the an Fr value, else false.
761    #[inline(always)]
762    pub fn is_fr(&self) -> bool {
763        self.0.is_fr()
764    }
765
766    /// Returns true if the is `Auto`, else false.
767    #[inline(always)]
768    pub fn is_auto(&self) -> bool {
769        self.0.is_auto()
770    }
771
772    /// Returns true if value is MinContent
773    #[inline(always)]
774    pub fn is_min_content(&self) -> bool {
775        self.0.is_min_content()
776    }
777
778    /// Returns true if value is MaxContent
779    #[inline(always)]
780    pub fn is_max_content(&self) -> bool {
781        self.0.is_max_content()
782    }
783
784    /// Returns true if value is FitContent(...)
785    #[inline(always)]
786    pub fn is_fit_content(&self) -> bool {
787        self.0.is_fit_content()
788    }
789
790    /// Returns true if value is MaxContent or FitContent(...)
791    #[inline(always)]
792    pub fn is_max_or_fit_content(&self) -> bool {
793        self.0.is_max_or_fit_content()
794    }
795
796    /// Returns whether the value can be resolved using `Self::definite_value`
797    #[inline(always)]
798    pub fn has_definite_value(self, parent_size: Option<f32>) -> bool {
799        match self.0.tag() {
800            CompactLength::LENGTH_TAG => true,
801            CompactLength::PERCENT_TAG => parent_size.is_some(),
802            #[cfg(feature = "calc")]
803            _ if self.0.is_calc() => parent_size.is_some(),
804            _ => false,
805        }
806    }
807
808    /// Returns fixed point values directly. Attempts to resolve percentage values against
809    /// the passed available_space and returns if this results in a concrete value (which it
810    /// will if the available_space is `Some`). Otherwise returns None.
811    #[inline(always)]
812    pub fn definite_value(
813        self,
814        parent_size: Option<f32>,
815        calc_resolver: impl Fn(*const (), f32) -> f32,
816    ) -> Option<f32> {
817        match self.0.tag() {
818            CompactLength::LENGTH_TAG => Some(self.0.value()),
819            CompactLength::PERCENT_TAG => parent_size.map(|size| self.0.value() * size),
820            #[cfg(feature = "calc")]
821            _ if self.0.is_calc() => parent_size.map(|size| calc_resolver(self.0.calc_value(), size)),
822            _ => None,
823        }
824    }
825
826    /// Resolve the maximum size of the track as defined by either:
827    ///     - A fixed track sizing function
828    ///     - A percentage track sizing function (with definite available space)
829    ///     - A fit-content sizing function with fixed argument
830    ///     - A fit-content sizing function with percentage argument (with definite available space)
831    /// All other kinds of track sizing function return None.
832    #[inline(always)]
833    pub fn definite_limit(
834        self,
835        parent_size: Option<f32>,
836        calc_resolver: impl Fn(*const (), f32) -> f32,
837    ) -> Option<f32> {
838        match self.0.tag() {
839            CompactLength::FIT_CONTENT_PX_TAG => Some(self.0.value()),
840            CompactLength::FIT_CONTENT_PERCENT_TAG => parent_size.map(|size| self.0.value() * size),
841            _ => self.definite_value(parent_size, calc_resolver),
842        }
843    }
844
845    /// Resolve percentage values against the passed parent_size, returning Some(value)
846    /// Non-percentage values always return None.
847    #[inline(always)]
848    pub fn resolved_percentage_size(
849        self,
850        parent_size: f32,
851        calc_resolver: impl Fn(*const (), f32) -> f32,
852    ) -> Option<f32> {
853        self.0.resolved_percentage_size(parent_size, calc_resolver)
854    }
855
856    /// Whether the track sizing functions depends on the size of the parent node
857    #[inline(always)]
858    pub fn uses_percentage(self) -> bool {
859        self.0.uses_percentage()
860    }
861}
862
863/// Minimum track sizing function
864///
865/// Specifies the minimum size of a grid track. A grid track will automatically size between it's minimum and maximum size based
866/// on the size of it's contents, the amount of available space, and the sizing constraint the grid is being size under.
867/// See <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns>
868#[derive(Copy, Clone, PartialEq, Debug)]
869#[cfg_attr(feature = "serde", derive(Serialize))]
870pub struct MinTrackSizingFunction(pub(crate) CompactLength);
871impl TaffyZero for MinTrackSizingFunction {
872    const ZERO: Self = Self(CompactLength::ZERO);
873}
874impl TaffyAuto for MinTrackSizingFunction {
875    const AUTO: Self = Self(CompactLength::AUTO);
876}
877impl TaffyMinContent for MinTrackSizingFunction {
878    const MIN_CONTENT: Self = Self(CompactLength::MIN_CONTENT);
879}
880impl TaffyMaxContent for MinTrackSizingFunction {
881    const MAX_CONTENT: Self = Self(CompactLength::MAX_CONTENT);
882}
883impl FromLength for MinTrackSizingFunction {
884    fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
885        Self::length(value.into())
886    }
887}
888impl FromPercent for MinTrackSizingFunction {
889    fn from_percent<Input: Into<f32> + Copy>(value: Input) -> Self {
890        Self::percent(value.into())
891    }
892}
893impl From<LengthPercentage> for MinTrackSizingFunction {
894    fn from(input: LengthPercentage) -> Self {
895        Self(input.0)
896    }
897}
898impl From<LengthPercentageAuto> for MinTrackSizingFunction {
899    fn from(input: LengthPercentageAuto) -> Self {
900        Self(input.0)
901    }
902}
903impl From<Dimension> for MinTrackSizingFunction {
904    fn from(input: Dimension) -> Self {
905        Self(input.0)
906    }
907}
908#[cfg(feature = "serde")]
909impl<'de> serde::Deserialize<'de> for MinTrackSizingFunction {
910    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
911    where
912        D: serde::Deserializer<'de>,
913    {
914        let inner = CompactLength::deserialize(deserializer)?;
915        // Note: validation intentionally excludes the CALC_TAG as deserializing calc() values is not supported
916        if matches!(
917            inner.tag(),
918            CompactLength::LENGTH_TAG
919                | CompactLength::PERCENT_TAG
920                | CompactLength::AUTO_TAG
921                | CompactLength::MIN_CONTENT_TAG
922                | CompactLength::MAX_CONTENT_TAG
923                | CompactLength::FIT_CONTENT_PX_TAG
924                | CompactLength::FIT_CONTENT_PERCENT_TAG
925        ) {
926            Ok(Self(inner))
927        } else {
928            Err(serde::de::Error::custom("Invalid tag"))
929        }
930    }
931}
932
933impl MinTrackSizingFunction {
934    /// An absolute length in some abstract units. Users of Taffy may define what they correspond
935    /// to in their application (pixels, logical pixels, mm, etc) as they see fit.
936    #[inline(always)]
937    pub const fn length(val: f32) -> Self {
938        Self(CompactLength::length(val))
939    }
940
941    /// A percentage length relative to the size of the containing block.
942    ///
943    /// **NOTE: percentages are represented as a f32 value in the range [0.0, 1.0] NOT the range [0.0, 100.0]**
944    #[inline(always)]
945    pub const fn percent(val: f32) -> Self {
946        Self(CompactLength::percent(val))
947    }
948
949    /// The dimension should be automatically computed according to algorithm-specific rules
950    /// regarding the default size of boxes.
951    #[inline(always)]
952    pub const fn auto() -> Self {
953        Self(CompactLength::auto())
954    }
955
956    /// The size should be the "min-content" size.
957    /// This is the smallest size that can fit the item's contents with ALL soft line-wrapping opportunities taken
958    #[inline(always)]
959    pub const fn min_content() -> Self {
960        Self(CompactLength::min_content())
961    }
962
963    /// The size should be the "max-content" size.
964    /// This is the smallest size that can fit the item's contents with NO soft line-wrapping opportunities taken
965    #[inline(always)]
966    pub const fn max_content() -> Self {
967        Self(CompactLength::max_content())
968    }
969
970    /// A `calc()` value. The value passed here is treated as an opaque handle to
971    /// the actual calc representation and may be a pointer, index, etc.
972    ///
973    /// The low 3 bits are used as a tag value and will be returned as 0.
974    #[inline]
975    #[cfg(feature = "calc")]
976    pub fn calc(ptr: *const ()) -> Self {
977        Self(CompactLength::calc(ptr))
978    }
979
980    /// Create a LengthPercentageAuto from a raw `CompactLength`.
981    /// # Safety
982    /// CompactLength must represent a valid variant for LengthPercentageAuto
983    #[allow(unsafe_code)]
984    pub unsafe fn from_raw(val: CompactLength) -> Self {
985        Self(val)
986    }
987
988    /// Get the underlying `CompactLength` representation of the value
989    pub fn into_raw(self) -> CompactLength {
990        self.0
991    }
992
993    /// Returns true if the min track sizing function is `MinContent`, `MaxContent` or `Auto`, else false.
994    #[inline(always)]
995    pub fn is_intrinsic(&self) -> bool {
996        self.0.is_intrinsic()
997    }
998
999    /// Returns true if the min track sizing function is `MinContent` or `MaxContent`, else false.
1000    #[inline(always)]
1001    pub fn is_min_or_max_content(&self) -> bool {
1002        self.0.is_min_or_max_content()
1003    }
1004
1005    /// Returns true if the value is an fr value
1006    #[inline(always)]
1007    pub fn is_fr(&self) -> bool {
1008        self.0.is_fr()
1009    }
1010
1011    /// Returns true if the is `Auto`, else false.
1012    #[inline(always)]
1013    pub fn is_auto(&self) -> bool {
1014        self.0.is_auto()
1015    }
1016
1017    /// Returns true if value is MinContent
1018    #[inline(always)]
1019    pub fn is_min_content(&self) -> bool {
1020        self.0.is_min_content()
1021    }
1022
1023    /// Returns true if value is MaxContent
1024    #[inline(always)]
1025    pub fn is_max_content(&self) -> bool {
1026        self.0.is_max_content()
1027    }
1028
1029    /// Returns fixed point values directly. Attempts to resolve percentage values against
1030    /// the passed available_space and returns if this results in a concrete value (which it
1031    /// will if the available_space is `Some`). Otherwise returns `None`.
1032    #[inline(always)]
1033    pub fn definite_value(
1034        self,
1035        parent_size: Option<f32>,
1036        calc_resolver: impl Fn(*const (), f32) -> f32,
1037    ) -> Option<f32> {
1038        match self.0.tag() {
1039            CompactLength::LENGTH_TAG => Some(self.0.value()),
1040            CompactLength::PERCENT_TAG => parent_size.map(|size| self.0.value() * size),
1041            #[cfg(feature = "calc")]
1042            _ if self.0.is_calc() => parent_size.map(|size| calc_resolver(self.0.calc_value(), size)),
1043            _ => None,
1044        }
1045    }
1046
1047    /// Resolve percentage values against the passed parent_size, returning Some(value)
1048    /// Non-percentage values always return None.
1049    #[inline(always)]
1050    pub fn resolved_percentage_size(
1051        self,
1052        parent_size: f32,
1053        calc_resolver: impl Fn(*const (), f32) -> f32,
1054    ) -> Option<f32> {
1055        self.0.resolved_percentage_size(parent_size, calc_resolver)
1056    }
1057
1058    /// Whether the track sizing functions depends on the size of the parent node
1059    #[inline(always)]
1060    pub fn uses_percentage(self) -> bool {
1061        #[cfg(feature = "calc")]
1062        {
1063            matches!(self.0.tag(), CompactLength::PERCENT_TAG) || self.0.is_calc()
1064        }
1065        #[cfg(not(feature = "calc"))]
1066        {
1067            matches!(self.0.tag(), CompactLength::PERCENT_TAG)
1068        }
1069    }
1070}
1071
1072/// The sizing function for a grid track (row/column)
1073///
1074/// May either be a MinMax variant which specifies separate values for the min-/max- track sizing functions
1075/// or a scalar value which applies to both track sizing functions.
1076pub type TrackSizingFunction = MinMax<MinTrackSizingFunction, MaxTrackSizingFunction>;
1077impl TrackSizingFunction {
1078    /// Extract the min track sizing function
1079    pub fn min_sizing_function(&self) -> MinTrackSizingFunction {
1080        self.min
1081    }
1082    /// Extract the max track sizing function
1083    pub fn max_sizing_function(&self) -> MaxTrackSizingFunction {
1084        self.max
1085    }
1086    /// Determine whether at least one of the components ("min" and "max") are fixed sizing function
1087    pub fn has_fixed_component(&self) -> bool {
1088        self.min.0.is_length_or_percentage() || self.max.0.is_length_or_percentage()
1089    }
1090}
1091impl TaffyAuto for TrackSizingFunction {
1092    const AUTO: Self = Self { min: MinTrackSizingFunction::AUTO, max: MaxTrackSizingFunction::AUTO };
1093}
1094impl TaffyMinContent for TrackSizingFunction {
1095    const MIN_CONTENT: Self =
1096        Self { min: MinTrackSizingFunction::MIN_CONTENT, max: MaxTrackSizingFunction::MIN_CONTENT };
1097}
1098impl TaffyMaxContent for TrackSizingFunction {
1099    const MAX_CONTENT: Self =
1100        Self { min: MinTrackSizingFunction::MAX_CONTENT, max: MaxTrackSizingFunction::MAX_CONTENT };
1101}
1102impl TaffyFitContent for TrackSizingFunction {
1103    fn fit_content(argument: LengthPercentage) -> Self {
1104        Self { min: MinTrackSizingFunction::AUTO, max: MaxTrackSizingFunction::fit_content(argument) }
1105    }
1106}
1107impl TaffyZero for TrackSizingFunction {
1108    const ZERO: Self = Self { min: MinTrackSizingFunction::ZERO, max: MaxTrackSizingFunction::ZERO };
1109}
1110impl FromLength for TrackSizingFunction {
1111    fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
1112        Self { min: MinTrackSizingFunction::from_length(value), max: MaxTrackSizingFunction::from_length(value) }
1113    }
1114}
1115impl FromPercent for TrackSizingFunction {
1116    fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
1117        Self { min: MinTrackSizingFunction::from_percent(percent), max: MaxTrackSizingFunction::from_percent(percent) }
1118    }
1119}
1120impl FromFr for TrackSizingFunction {
1121    fn from_fr<Input: Into<f32> + Copy>(flex: Input) -> Self {
1122        Self { min: MinTrackSizingFunction::AUTO, max: MaxTrackSizingFunction::from_fr(flex) }
1123    }
1124}
1125impl From<LengthPercentage> for TrackSizingFunction {
1126    fn from(input: LengthPercentage) -> Self {
1127        Self { min: input.into(), max: input.into() }
1128    }
1129}
1130impl From<LengthPercentageAuto> for TrackSizingFunction {
1131    fn from(input: LengthPercentageAuto) -> Self {
1132        Self { min: input.into(), max: input.into() }
1133    }
1134}
1135impl From<Dimension> for TrackSizingFunction {
1136    fn from(input: Dimension) -> Self {
1137        Self { min: input.into(), max: input.into() }
1138    }
1139}
1140
1141/// The first argument to a repeated track definition. This type represents the type of automatic repetition to perform.
1142///
1143/// See <https://www.w3.org/TR/css-grid-1/#auto-repeat> for an explanation of how auto-repeated track definitions work
1144/// and the difference between AutoFit and AutoFill.
1145#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1146#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1147pub enum RepetitionCount {
1148    /// Auto-repeating tracks should be generated to fit the container
1149    /// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/repeat#auto-fill>
1150    AutoFill,
1151    /// Auto-repeating tracks should be generated to fit the container
1152    /// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/repeat#auto-fit>
1153    AutoFit,
1154    /// The specified tracks should be repeated exacts N times
1155    Count(u16),
1156}
1157impl From<u16> for RepetitionCount {
1158    fn from(value: u16) -> Self {
1159        Self::Count(value)
1160    }
1161}
1162
1163/// Error returned when trying to convert a string to a GridTrackRepetition and that string is not
1164/// either "auto-fit" or "auto-fill"
1165#[derive(Debug)]
1166pub struct InvalidStringRepetitionValue;
1167#[cfg(feature = "std")]
1168impl std::error::Error for InvalidStringRepetitionValue {}
1169impl core::fmt::Display for InvalidStringRepetitionValue {
1170    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1171        f.write_str("&str can only be converted to GridTrackRepetition if it's value is 'auto-fit' or 'auto-fill'")
1172    }
1173}
1174impl TryFrom<&str> for RepetitionCount {
1175    type Error = InvalidStringRepetitionValue;
1176    fn try_from(value: &str) -> Result<Self, InvalidStringRepetitionValue> {
1177        match value {
1178            "auto-fit" => Ok(Self::AutoFit),
1179            "auto-fill" => Ok(Self::AutoFill),
1180            _ => Err(InvalidStringRepetitionValue),
1181        }
1182    }
1183}
1184
1185/// A typed representation of a `repeat(..)` in `grid-template-*` value
1186#[derive(Clone, PartialEq, Debug)]
1187#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1188pub struct GridTemplateRepetition<S: CheapCloneStr> {
1189    /// The number of the times the repeat is repeated
1190    pub count: RepetitionCount,
1191    /// The tracks to repeat
1192    pub tracks: Vec<TrackSizingFunction>,
1193    /// The line names for the repeated tracks
1194    pub line_names: Vec<Vec<S>>,
1195}
1196
1197#[rustfmt::skip]
1198impl<S: CheapCloneStr> GenericRepetition for &'_ GridTemplateRepetition<S> {
1199    type CustomIdent = S;
1200    type RepetitionTrackList<'a> = core::iter::Copied<core::slice::Iter<'a, TrackSizingFunction>> where Self: 'a;
1201    type TemplateLineNames<'a> = core::iter::Map<core::slice::Iter<'a, Vec<S>>, fn(&Vec<S>) -> core::slice::Iter<'_, S>> where Self: 'a;
1202    #[inline(always)]
1203    fn count(&self) -> RepetitionCount {
1204        self.count
1205    }
1206    #[inline(always)]
1207    fn track_count(&self) -> u16 {
1208        self.tracks.len() as u16
1209    }
1210    #[inline(always)]
1211    fn tracks(&self) -> Self::RepetitionTrackList<'_> {
1212        self.tracks.iter().copied()
1213    }
1214    #[inline(always)]
1215    fn lines_names(&self) -> Self::TemplateLineNames<'_> {
1216        self.line_names.iter().map(|names| names.iter())
1217    }
1218}
1219
1220/// An element in a `grid-template-columns` or `grid-template-rows` definition.
1221/// Either a track sizing function or a repeat().
1222///
1223/// See <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns>
1224#[derive(Clone, PartialEq, Debug)]
1225#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1226pub enum GridTemplateComponent<S: CheapCloneStr> {
1227    /// A single non-repeated track
1228    Single(TrackSizingFunction),
1229    /// Automatically generate grid tracks to fit the available space using the specified definite track lengths
1230    /// Only valid if every track in template (not just the repetition) has a fixed size.
1231    Repeat(GridTemplateRepetition<S>),
1232}
1233
1234impl<S: CheapCloneStr> GridTemplateComponent<S> {
1235    /// Convert a `GridTemplateComponent` into a `GridTemplateComponentRef`
1236    pub fn as_component_ref(&self) -> GenericGridTemplateComponent<S, &GridTemplateRepetition<S>> {
1237        match self {
1238            GridTemplateComponent::Single(size) => GenericGridTemplateComponent::Single(*size),
1239            GridTemplateComponent::Repeat(repetition) => GenericGridTemplateComponent::Repeat(repetition),
1240        }
1241    }
1242}
1243
1244impl<S: CheapCloneStr> GridTemplateComponent<S> {
1245    /// Whether the track definition is a auto-repeated fragment
1246    pub fn is_auto_repetition(&self) -> bool {
1247        matches!(
1248            self,
1249            Self::Repeat(GridTemplateRepetition { count: RepetitionCount::AutoFit | RepetitionCount::AutoFill, .. })
1250        )
1251    }
1252}
1253impl<S: CheapCloneStr> TaffyAuto for GridTemplateComponent<S> {
1254    const AUTO: Self = Self::Single(TrackSizingFunction::AUTO);
1255}
1256impl<S: CheapCloneStr> TaffyMinContent for GridTemplateComponent<S> {
1257    const MIN_CONTENT: Self = Self::Single(TrackSizingFunction::MIN_CONTENT);
1258}
1259impl<S: CheapCloneStr> TaffyMaxContent for GridTemplateComponent<S> {
1260    const MAX_CONTENT: Self = Self::Single(TrackSizingFunction::MAX_CONTENT);
1261}
1262impl<S: CheapCloneStr> TaffyFitContent for GridTemplateComponent<S> {
1263    fn fit_content(argument: LengthPercentage) -> Self {
1264        Self::Single(TrackSizingFunction::fit_content(argument))
1265    }
1266}
1267impl<S: CheapCloneStr> TaffyZero for GridTemplateComponent<S> {
1268    const ZERO: Self = Self::Single(TrackSizingFunction::ZERO);
1269}
1270impl<S: CheapCloneStr> FromLength for GridTemplateComponent<S> {
1271    fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
1272        Self::Single(TrackSizingFunction::from_length(value))
1273    }
1274}
1275impl<S: CheapCloneStr> FromPercent for GridTemplateComponent<S> {
1276    fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
1277        Self::Single(TrackSizingFunction::from_percent(percent))
1278    }
1279}
1280impl<S: CheapCloneStr> FromFr for GridTemplateComponent<S> {
1281    fn from_fr<Input: Into<f32> + Copy>(flex: Input) -> Self {
1282        Self::Single(TrackSizingFunction::from_fr(flex))
1283    }
1284}
1285impl<S: CheapCloneStr> From<MinMax<MinTrackSizingFunction, MaxTrackSizingFunction>> for GridTemplateComponent<S> {
1286    fn from(input: MinMax<MinTrackSizingFunction, MaxTrackSizingFunction>) -> Self {
1287        Self::Single(input)
1288    }
1289}