#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScreenSize {
Physical {
width_cm: u8,
height_cm: u8,
},
Landscape(u8),
Portrait(u8),
}
impl ScreenSize {
pub fn landscape_ratio(&self) -> Option<f32> {
if let Self::Landscape(v) = self {
Some((*v as f32 + 99.0) / 100.0)
} else {
None
}
}
pub fn portrait_ratio(&self) -> Option<f32> {
if let Self::Portrait(v) = self {
Some(100.0 / (*v as f32 + 99.0))
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn landscape_ratio_correct_formula() {
assert_eq!(ScreenSize::Landscape(1).landscape_ratio(), Some(1.00));
assert!((ScreenSize::Landscape(100).landscape_ratio().unwrap() - 1.99).abs() < 1e-5);
}
#[test]
fn landscape_ratio_none_for_other_variants() {
assert_eq!(
ScreenSize::Physical {
width_cm: 60,
height_cm: 34
}
.landscape_ratio(),
None
);
assert_eq!(ScreenSize::Portrait(1).landscape_ratio(), None);
}
#[test]
fn portrait_ratio_correct_formula() {
assert_eq!(ScreenSize::Portrait(1).portrait_ratio(), Some(1.00));
let r = ScreenSize::Portrait(100).portrait_ratio().unwrap();
assert!((r - 100.0 / 199.0).abs() < 1e-5);
}
#[test]
fn portrait_ratio_none_for_other_variants() {
assert_eq!(
ScreenSize::Physical {
width_cm: 60,
height_cm: 34
}
.portrait_ratio(),
None
);
assert_eq!(ScreenSize::Landscape(1).portrait_ratio(), None);
}
}