1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-03 05:59:22 +00:00
hls_m3u8/src/types/decimal_resolution.rs

81 lines
2 KiB
Rust
Raw Normal View History

2019-09-06 11:20:40 +00:00
use std::fmt;
use std::str::{self, 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 {
/// Horizontal pixel dimension.
pub width: usize,
/// Vertical pixel dimension.
pub height: usize,
}
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> {
let mut tokens = input.splitn(2, 'x');
let width = tokens.next().ok_or(Error::missing_value("width"))?;
let height = tokens.next().ok_or(Error::missing_value("height"))?;
2019-09-06 11:20:40 +00:00
Ok(DecimalResolution {
2019-09-13 14:06:52 +00:00
width: width.parse().map_err(|e| Error::custom(e))?,
height: height.parse().map_err(|e| Error::custom(e))?,
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() {
let decimal_resolution = DecimalResolution {
width: 1920,
height: 1080,
};
assert_eq!(decimal_resolution.to_string(), "1920x1080".to_string());
let decimal_resolution = DecimalResolution {
width: 1280,
height: 720,
};
assert_eq!(decimal_resolution.to_string(), "1280x720".to_string());
}
#[test]
fn test_parse() {
let decimal_resolution = DecimalResolution {
width: 1920,
height: 1080,
};
assert_eq!(
decimal_resolution,
"1920x1080".parse::<DecimalResolution>().unwrap()
);
let decimal_resolution = DecimalResolution {
width: 1280,
height: 720,
};
assert_eq!(
decimal_resolution,
"1280x720".parse::<DecimalResolution>().unwrap()
);
}
}