Skip to content

Commit

Permalink
Fix panic when GridLine 0 is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jun 25, 2024
1 parent aeb0b44 commit e3292ba
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/style/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,6 @@ impl GridPlacement {
}

impl<T: GridCoordinate> Line<GenericGridPlacement<T>> {
#[inline]
/// Whether the track position is definite in this axis (or the item will need auto placement)
/// The track position is definite if least one of the start and end positions is a track index
pub fn is_definite(&self) -> bool {
matches!((self.start, self.end), (GenericGridPlacement::Line(_), _) | (_, GenericGridPlacement::Line(_)))
}

/// Resolves the span for an indefinite placement (a placement that does not consist of two `Track`s).
/// Panics if called on a definite placement
pub fn indefinite_span(&self) -> u16 {
Expand All @@ -154,6 +147,18 @@ impl<T: GridCoordinate> Line<GenericGridPlacement<T>> {
}

impl Line<GridPlacement> {
#[inline]
/// Whether the track position is definite in this axis (or the item will need auto placement)
/// The track position is definite if least one of the start and end positions is a NON-ZERO track index
/// (0 is an invalid line in GridLine coordinates, and falls back to "auto" which is indefinite)
pub fn is_definite(&self) -> bool {
match (self.start, self.end) {
(GenericGridPlacement::Line(line), _) if line.as_i16() != 0 => true,
(_, GenericGridPlacement::Line(line)) if line.as_i16() != 0 => true,
_ => false,
}
}

/// Apply a mapping function if the [`GridPlacement`] is a `Track`. Otherwise return `self` unmodified.
pub fn into_origin_zero(&self, explicit_track_count: u16) -> Line<OriginZeroGridPlacement> {
Line {
Expand All @@ -164,6 +169,13 @@ impl Line<GridPlacement> {
}

impl Line<OriginZeroGridPlacement> {
#[inline]
/// Whether the track position is definite in this axis (or the item will need auto placement)
/// The track position is definite if least one of the start and end positions is a track index
pub fn is_definite(&self) -> bool {
matches!((self.start, self.end), (GenericGridPlacement::Line(_), _) | (_, GenericGridPlacement::Line(_)))
}

/// If at least one of the of the start and end positions is a track index then the other end can be resolved
/// into a track index purely based on the information contained with the placement specification
pub fn resolve_definite_grid_lines(&self) -> Line<OriginZeroLine> {
Expand Down

0 comments on commit e3292ba

Please sign in to comment.