#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Breakpoint {
Xs,
Sm,
Md,
Lg,
Xl,
Xxl,
}
impl Breakpoint {
pub fn for_width(width: f32) -> Self {
if width < 576.0 {
Breakpoint::Xs
} else if width < 768.0 {
Breakpoint::Sm
} else if width < 992.0 {
Breakpoint::Md
} else if width < 1200.0 {
Breakpoint::Lg
} else if width < 1536.0 {
Breakpoint::Xl
} else {
Breakpoint::Xxl
}
}
pub fn min_width(self) -> f32 {
match self {
Breakpoint::Xs => 0.0,
Breakpoint::Sm => 576.0,
Breakpoint::Md => 768.0,
Breakpoint::Lg => 992.0,
Breakpoint::Xl => 1200.0,
Breakpoint::Xxl => 1536.0,
}
}
pub fn matches_width(self, width: f32) -> bool {
width >= self.min_width()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn breakpoint_xs_below_576() {
assert_eq!(Breakpoint::for_width(400.0), Breakpoint::Xs);
assert_eq!(Breakpoint::for_width(0.0), Breakpoint::Xs);
}
#[test]
fn breakpoint_sm_at_576() {
assert_eq!(Breakpoint::for_width(576.0), Breakpoint::Sm);
assert_eq!(Breakpoint::for_width(767.9), Breakpoint::Sm);
}
#[test]
fn breakpoint_md_768_to_991() {
assert_eq!(Breakpoint::for_width(900.0), Breakpoint::Md);
}
#[test]
fn breakpoint_xxl_at_1536() {
assert_eq!(Breakpoint::for_width(1536.0), Breakpoint::Xxl);
assert_eq!(Breakpoint::for_width(2000.0), Breakpoint::Xxl);
}
#[test]
fn breakpoint_ordering() {
assert!(Breakpoint::Xs < Breakpoint::Sm);
assert!(Breakpoint::Sm < Breakpoint::Md);
assert!(Breakpoint::Md < Breakpoint::Lg);
assert!(Breakpoint::Lg < Breakpoint::Xl);
assert!(Breakpoint::Xl < Breakpoint::Xxl);
}
#[test]
fn min_width_xs_is_zero() {
assert_eq!(Breakpoint::Xs.min_width(), 0.0);
}
#[test]
fn matches_width_true_and_false() {
assert!(Breakpoint::Md.matches_width(900.0));
assert!(!Breakpoint::Xl.matches_width(900.0));
}
}