1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-21 01:38:07 +00:00
hls_m3u8/src/types/decimal_resolution.rs

130 lines
3.1 KiB
Rust
Raw Normal View History

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
/// Decimal resolution.
///
/// 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-09-15 08:51:04 +00:00
pub(crate) 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
}
2019-09-22 18:33:40 +00:00
/// [DecimalResolution] can be constructed from a tuple; `(width, height)`.
impl From<(usize, usize)> for DecimalResolution {
fn from(value: (usize, usize)) -> Self {
DecimalResolution::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 {
width: tokens[0].parse()?,
height: tokens[1].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]
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
}