|
| 1 | +use crate::{ |
| 2 | + CheapCloneStr, GenericGridPlacement, GridAreaAxis, GridAreaEnd, GridPlacement, GridTemplateArea, Line, |
| 3 | + NonNamedGridPlacement, |
| 4 | +}; |
| 5 | +use core::borrow::Borrow; |
| 6 | +use std::{ |
| 7 | + collections::HashMap, |
| 8 | + hash::{Hash, Hasher}, |
| 9 | +}; |
| 10 | + |
| 11 | +/// Wrap an `AsRef<str>` type with a type which implements Hash by first |
| 12 | +/// deferring to the underlying `&str`'s implementation of Hash. |
| 13 | +#[derive(Debug, Clone)] |
| 14 | +pub(crate) struct StrHasher<T: CheapCloneStr>(pub T); |
| 15 | +impl<T: CheapCloneStr> Hash for StrHasher<T> { |
| 16 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 17 | + // Dele |
| 18 | + self.0.as_ref().hash(state) |
| 19 | + } |
| 20 | +} |
| 21 | +impl<T: CheapCloneStr> PartialEq for StrHasher<T> { |
| 22 | + fn eq(&self, other: &Self) -> bool { |
| 23 | + other.0.as_ref() == self.0.as_ref() |
| 24 | + } |
| 25 | +} |
| 26 | +impl<T: CheapCloneStr> Borrow<str> for StrHasher<T> { |
| 27 | + fn borrow(&self) -> &str { |
| 28 | + self.0.as_ref() |
| 29 | + } |
| 30 | +} |
| 31 | +impl<T: CheapCloneStr> Eq for StrHasher<T> {} |
| 32 | + |
| 33 | +/// Resolve lines for |
| 34 | +pub(crate) struct NamedLineResolver<S: CheapCloneStr> { |
| 35 | + lines: HashMap<StrHasher<S>, i16>, |
| 36 | + areas: HashMap<StrHasher<S>, GridTemplateArea<S>>, |
| 37 | + area_column_count: u16, |
| 38 | + area_row_count: u16, |
| 39 | +} |
| 40 | + |
| 41 | +impl<S: CheapCloneStr> NamedLineResolver<S> { |
| 42 | + pub(crate) fn new(area_styles: Option<impl Borrow<[GridTemplateArea<S>]>>) -> Self { |
| 43 | + let mut area_column_count = 0; |
| 44 | + let mut area_row_count = 0; |
| 45 | + let mut areas: HashMap<StrHasher<_>, GridTemplateArea<_>> = HashMap::new(); |
| 46 | + if let Some(area_iter) = area_styles { |
| 47 | + for area in area_iter.borrow() { |
| 48 | + // TODO: Investigate eliminating clones |
| 49 | + areas.insert(StrHasher(area.name.clone()), area.clone()); |
| 50 | + |
| 51 | + area_column_count = area_column_count.max(area.column_end); |
| 52 | + area_row_count = area_row_count.max(area.row_end); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + Self { area_column_count, area_row_count, areas, lines: HashMap::new() } |
| 57 | + } |
| 58 | + |
| 59 | + #[inline(always)] |
| 60 | + pub(crate) fn resolve_row_names(&self, line: &Line<GridPlacement<S>>) -> Line<NonNamedGridPlacement> { |
| 61 | + self.resolve_line_names(line, GridAreaAxis::Row) |
| 62 | + } |
| 63 | + |
| 64 | + #[inline(always)] |
| 65 | + pub(crate) fn resolve_column_names(&self, line: &Line<GridPlacement<S>>) -> Line<NonNamedGridPlacement> { |
| 66 | + self.resolve_line_names(line, GridAreaAxis::Column) |
| 67 | + } |
| 68 | + |
| 69 | + #[inline(always)] |
| 70 | + pub(crate) fn resolve_line_names( |
| 71 | + &self, |
| 72 | + line: &Line<GridPlacement<S>>, |
| 73 | + axis: GridAreaAxis, |
| 74 | + ) -> Line<NonNamedGridPlacement> { |
| 75 | + Line { |
| 76 | + start: self.resolve_line_name(&line.start, axis, GridAreaEnd::Start), |
| 77 | + end: self.resolve_line_name(&line.end, axis, GridAreaEnd::End), |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + pub(crate) fn resolve_line_name( |
| 82 | + &self, |
| 83 | + placement: &GridPlacement<S>, |
| 84 | + axis: GridAreaAxis, |
| 85 | + end: GridAreaEnd, |
| 86 | + ) -> NonNamedGridPlacement { |
| 87 | + match placement { |
| 88 | + GridPlacement::Auto => NonNamedGridPlacement::Auto, |
| 89 | + GridPlacement::Line(grid_line) => NonNamedGridPlacement::Line(*grid_line), |
| 90 | + GridPlacement::Span(span) => NonNamedGridPlacement::Span(*span), |
| 91 | + GridPlacement::Named(name) => { |
| 92 | + let name = name.as_ref(); |
| 93 | + if name.ends_with("-start") { |
| 94 | + let area_name = &name[0..(name.len() - 6)]; |
| 95 | + if let Some(area) = self.areas.get(area_name) { |
| 96 | + return GenericGridPlacement::Line(area.get_side(axis, GridAreaEnd::Start)); |
| 97 | + } |
| 98 | + } else if name.ends_with("-end") { |
| 99 | + let area_name = &name[0..(name.len() - 6)]; |
| 100 | + if let Some(area) = self.areas.get(area_name) { |
| 101 | + return GenericGridPlacement::Line(area.get_side(axis, GridAreaEnd::End)); |
| 102 | + } |
| 103 | + } else { |
| 104 | + if let Some(area) = self.areas.get(name) { |
| 105 | + return GenericGridPlacement::Line(area.get_side(axis, end)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // TODO: lookup names lines |
| 110 | + NonNamedGridPlacement::Auto |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + pub(crate) fn area_column_count(&self) -> u16 { |
| 116 | + self.area_column_count |
| 117 | + } |
| 118 | + |
| 119 | + pub(crate) fn area_row_count(&self) -> u16 { |
| 120 | + self.area_row_count |
| 121 | + } |
| 122 | +} |
0 commit comments