1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-11-26 00:51:00 +00:00

improve documentation and tests for Resolution

This commit is contained in:
Luro02 2020-02-24 13:00:20 +01:00
parent cdb6367dbd
commit 0be0c7ddfb
No known key found for this signature in database
GPG key ID: B66FD4F74501A9CF

View file

@ -5,33 +5,61 @@ use shorthand::ShortHand;
use crate::Error; use crate::Error;
/// This is a simple wrapper type for the display resolution. /// The number of distinct pixels in each dimension that can be displayed (e.g.
/// 1920x1080).
/// ///
/// For example Full HD has a resolution of 1920x1080. /// For example Full HD has a resolution of 1920x1080.
///
/// See: [4.2. Attribute Lists]
///
/// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2
#[derive(ShortHand, Ord, PartialOrd, Debug, Clone, Copy, PartialEq, Eq, Hash, Display)] #[derive(ShortHand, Ord, PartialOrd, Debug, Clone, Copy, PartialEq, Eq, Hash, Display)]
#[display(fmt = "{}x{}", width, height)] #[display(fmt = "{}x{}", width, height)]
#[shorthand(enable(must_use))] #[shorthand(enable(must_use))]
pub struct Resolution { pub struct Resolution {
/// Horizontal pixel dimension. /// Horizontal pixel dimension.
///
/// # Example
///
/// ```
/// # use hls_m3u8::types::Resolution;
/// let mut resolution = Resolution::new(1280, 720);
///
/// resolution.set_width(1000);
/// assert_eq!(resolution.width(), 1000);
/// ```
width: usize, width: usize,
/// Vertical pixel dimension. /// Vertical pixel dimension.
///
/// # Example
///
/// ```
/// # use hls_m3u8::types::Resolution;
/// let mut resolution = Resolution::new(1280, 720);
///
/// resolution.set_height(800);
/// assert_eq!(resolution.height(), 800);
/// ```
height: usize, height: usize,
} }
impl Resolution { impl Resolution {
/// Creates a new [`Resolution`]. /// Constructs a new [`Resolution`].
///
/// # Example
///
/// ```
/// # use hls_m3u8::types::Resolution;
/// let resolution = Resolution::new(1920, 1080);
/// ```
#[must_use]
pub const fn new(width: usize, height: usize) -> Self { Self { width, height } } pub const fn new(width: usize, height: usize) -> Self { Self { width, height } }
} }
/// A [`Resolution`] can be constructed from a tuple `(width, height)`.
impl From<(usize, usize)> for Resolution { impl From<(usize, usize)> for Resolution {
fn from(value: (usize, usize)) -> Self { Self::new(value.0, value.1) } fn from(value: (usize, usize)) -> Self { Self::new(value.0, value.1) }
} }
impl Into<(usize, usize)> for Resolution {
fn into(self) -> (usize, usize) { (self.width, self.height) }
}
impl FromStr for Resolution { impl FromStr for Resolution {
type Err = Error; type Err = Error;
@ -101,4 +129,9 @@ mod tests {
fn test_from() { fn test_from() {
assert_eq!(Resolution::from((1920, 1080)), Resolution::new(1920, 1080)); assert_eq!(Resolution::from((1920, 1080)), Resolution::new(1920, 1080));
} }
#[test]
fn test_into() {
assert_eq!((1920, 1080), Resolution::new(1920, 1080).into());
}
} }