2019-09-06 11:20:40 +00:00
|
|
|
use std::fmt;
|
2019-09-15 08:40:45 +00:00
|
|
|
use std::str::FromStr;
|
2019-09-13 14:06:52 +00:00
|
|
|
|
|
|
|
use crate::Error;
|
2019-09-06 11:20:40 +00:00
|
|
|
|
|
|
|
/// Decimal resolution.
|
|
|
|
///
|
|
|
|
/// See: [4.2. Attribute Lists]
|
|
|
|
///
|
|
|
|
/// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct DecimalResolution {
|
2019-09-15 08:40:45 +00:00
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DecimalResolution {
|
|
|
|
/// Creates a new DecimalResolution.
|
|
|
|
pub const fn new(width: usize, height: usize) -> Self {
|
|
|
|
Self { width, height }
|
|
|
|
}
|
|
|
|
|
2019-09-06 11:20:40 +00:00
|
|
|
/// Horizontal pixel dimension.
|
2019-09-15 08:40:45 +00:00
|
|
|
pub const fn width(&self) -> usize {
|
|
|
|
self.width
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-09-15 08:40:45 +00:00
|
|
|
pub const fn height(&self) -> usize {
|
|
|
|
self.height
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DecimalResolution {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}x{}", self.width, self.height)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let width = tokens[0];
|
|
|
|
let height = tokens[1];
|
2019-09-13 14:06:52 +00:00
|
|
|
|
2019-09-06 11:20:40 +00:00
|
|
|
Ok(DecimalResolution {
|
2019-09-15 08:40:45 +00:00
|
|
|
width: width.parse()?,
|
|
|
|
height: height.parse()?,
|
2019-09-06 11:20:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 11:46:21 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[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]
|
|
|
|
fn test_parse() {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|