2019-09-15 08:40:45 +00:00
|
|
|
use std::str::FromStr;
|
2019-09-13 14:06:52 +00:00
|
|
|
|
2019-09-22 18:33:40 +00:00
|
|
|
use derive_more::Display;
|
|
|
|
|
2019-09-13 14:06:52 +00:00
|
|
|
use crate::Error;
|
2019-09-06 11:20:40 +00:00
|
|
|
|
2019-10-05 10:49:08 +00:00
|
|
|
/// This is a simple wrapper type for the display resolution. (1920x1080,
|
|
|
|
/// 1280x720, ...).
|
2019-09-06 11:20:40 +00:00
|
|
|
///
|
|
|
|
/// See: [4.2. Attribute Lists]
|
|
|
|
///
|
|
|
|
/// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2
|
2019-09-22 18:33:40 +00:00
|
|
|
#[derive(Ord, PartialOrd, Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
|
|
|
|
#[display(fmt = "{}x{}", width, height)]
|
2019-10-05 10:49:08 +00:00
|
|
|
pub struct DecimalResolution {
|
2019-09-15 08:40:45 +00:00
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DecimalResolution {
|
2019-10-03 14:23:27 +00:00
|
|
|
/// Creates a new [`DecimalResolution`].
|
2019-10-03 15:01:15 +00:00
|
|
|
pub const fn new(width: usize, height: usize) -> Self { Self { width, height } }
|
2019-09-15 08:40:45 +00:00
|
|
|
|
2019-09-06 11:20:40 +00:00
|
|
|
/// Horizontal pixel dimension.
|
2019-10-03 15:01:15 +00:00
|
|
|
pub const fn width(&self) -> usize { self.width }
|
2019-09-15 08:40:45 +00:00
|
|
|
|
|
|
|
/// Sets Horizontal pixel dimension.
|
|
|
|
pub fn set_width(&mut self, value: usize) -> &mut Self {
|
|
|
|
self.width = value;
|
|
|
|
self
|
|
|
|
}
|
2019-09-06 11:20:40 +00:00
|
|
|
|
|
|
|
/// Vertical pixel dimension.
|
2019-10-03 15:01:15 +00:00
|
|
|
pub const fn height(&self) -> usize { self.height }
|
2019-09-15 08:40:45 +00:00
|
|
|
|
|
|
|
/// Sets Vertical pixel dimension.
|
|
|
|
pub fn set_height(&mut self, value: usize) -> &mut Self {
|
|
|
|
self.height = value;
|
|
|
|
self
|
|
|
|
}
|
2019-09-06 11:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 14:23:27 +00:00
|
|
|
/// [`DecimalResolution`] can be constructed from a tuple; `(width, height)`.
|
2019-09-22 18:33:40 +00:00
|
|
|
impl From<(usize, usize)> for DecimalResolution {
|
2019-10-05 14:08:03 +00:00
|
|
|
fn from(value: (usize, usize)) -> Self { Self::new(value.0, value.1) }
|
2019-09-06 11:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for DecimalResolution {
|
|
|
|
type Err = Error;
|
2019-09-13 14:06:52 +00:00
|
|
|
|
|
|
|
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
2019-09-15 08:40:45 +00:00
|
|
|
let tokens = input.splitn(2, 'x').collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if tokens.len() != 2 {
|
|
|
|
return Err(Error::custom(format!(
|
|
|
|
"InvalidInput: Expected input format: [width]x[height] (ex. 1920x1080), got {:?}",
|
|
|
|
input,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2019-09-22 18:33:40 +00:00
|
|
|
Ok(Self {
|
2020-01-23 18:13:26 +00:00
|
|
|
width: tokens[0].parse().map_err(Error::parse_int)?,
|
|
|
|
height: tokens[1].parse().map_err(Error::parse_int)?,
|
2019-09-06 11:20:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 11:46:21 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-10-08 13:42:33 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2019-09-06 11:46:21 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_display() {
|
2019-09-15 08:40:45 +00:00
|
|
|
assert_eq!(
|
|
|
|
DecimalResolution::new(1920, 1080).to_string(),
|
|
|
|
"1920x1080".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
DecimalResolution::new(1280, 720).to_string(),
|
|
|
|
"1280x720".to_string()
|
|
|
|
);
|
2019-09-06 11:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-09-21 11:24:05 +00:00
|
|
|
fn test_parser() {
|
2019-09-06 11:46:21 +00:00
|
|
|
assert_eq!(
|
2019-09-15 08:40:45 +00:00
|
|
|
DecimalResolution::new(1920, 1080),
|
2019-09-06 11:46:21 +00:00
|
|
|
"1920x1080".parse::<DecimalResolution>().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-09-15 08:40:45 +00:00
|
|
|
DecimalResolution::new(1280, 720),
|
2019-09-06 11:46:21 +00:00
|
|
|
"1280x720".parse::<DecimalResolution>().unwrap()
|
|
|
|
);
|
2019-09-15 08:40:45 +00:00
|
|
|
|
|
|
|
assert!("1280".parse::<DecimalResolution>().is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_width() {
|
|
|
|
assert_eq!(DecimalResolution::new(1920, 1080).width(), 1920);
|
|
|
|
assert_eq!(DecimalResolution::new(1920, 1080).set_width(12).width(), 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_height() {
|
|
|
|
assert_eq!(DecimalResolution::new(1920, 1080).height(), 1080);
|
|
|
|
assert_eq!(
|
|
|
|
DecimalResolution::new(1920, 1080).set_height(12).height(),
|
|
|
|
12
|
|
|
|
);
|
2019-09-06 11:46:21 +00:00
|
|
|
}
|
2019-09-22 18:33:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from() {
|
|
|
|
assert_eq!(
|
|
|
|
DecimalResolution::from((1920, 1080)),
|
|
|
|
DecimalResolution::new(1920, 1080)
|
|
|
|
);
|
|
|
|
}
|
2019-09-06 11:46:21 +00:00
|
|
|
}
|