| 1 | const VERY_NARROW_MAX_WIDTH: f32 = 216.; |
| 2 | const NARROW_MAX_WIDTH: f32 = 348.; |
| 3 | const STANDARD_MAX_WIDTH: f32 = 540.; |
| 4 | const AT_LEAST_PREFIX: &str = "atleast:"; |
| 5 | const AT_MOST_PREFIX: &str = "atmost:"; |
| 6 | |
| 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 8 | pub enum WidthClass { |
| 9 | VeryNarrow, |
| 10 | Narrow, |
| 11 | Standard, |
| 12 | Wide, |
| 13 | } |
| 14 | |
| 15 | impl WidthClass { |
| 16 | pub fn from_pixels(width: f32) -> WidthClass { |
| 17 | if width <= VERY_NARROW_MAX_WIDTH { |
| 18 | WidthClass::VeryNarrow |
| 19 | } else if width <= NARROW_MAX_WIDTH { |
| 20 | WidthClass::Narrow |
| 21 | } else if width <= STANDARD_MAX_WIDTH { |
| 22 | WidthClass::Standard |
| 23 | } else { |
| 24 | WidthClass::Wide |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn parse(name: &str) -> Option<WidthClass> { |
| 29 | match name.trim() { |
| 30 | "verynarrow" => Some(WidthClass::VeryNarrow), |
| 31 | "narrow" => Some(WidthClass::Narrow), |
| 32 | "standard" => Some(WidthClass::Standard), |
| 33 | "wide" => Some(WidthClass::Wide), |
| 34 | _ => None, |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 40 | pub struct TargetWidth { |
| 41 | at_least: Option<WidthClass>, |
| 42 | at_most: Option<WidthClass>, |
| 43 | } |
| 44 | |
| 45 | impl TargetWidth { |
| 46 | pub fn parse(value: &str) -> Option<TargetWidth> { |
| 47 | let value = value.trim().to_ascii_lowercase(); |
| 48 | if let Some(name) = value.strip_prefix(AT_LEAST_PREFIX) { |
| 49 | return Some(TargetWidth { |
| 50 | at_least: Some(WidthClass::parse(name)?), |
| 51 | at_most: None, |
| 52 | }); |
| 53 | } |
| 54 | if let Some(name) = value.strip_prefix(AT_MOST_PREFIX) { |
| 55 | return Some(TargetWidth { |
| 56 | at_least: None, |
| 57 | at_most: Some(WidthClass::parse(name)?), |
| 58 | }); |
| 59 | } |
| 60 | let exact = WidthClass::parse(&value)?; |
| 61 | Some(TargetWidth { |
| 62 | at_least: Some(exact), |
| 63 | at_most: Some(exact), |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | pub fn matches(&self, width: WidthClass) -> bool { |
| 68 | self.at_least.is_none_or(|lowest| width >= lowest) |
| 69 | && self.at_most.is_none_or(|highest| width <= highest) |
| 70 | } |
| 71 | } |