1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 13:39:27 +00:00
hls_m3u8/src/types/channels.rs

88 lines
2.1 KiB
Rust
Raw Permalink Normal View History

2019-10-03 14:23:27 +00:00
use core::fmt;
use core::str::FromStr;
2020-02-23 22:19:54 +00:00
use shorthand::ShortHand;
2019-10-03 14:23:27 +00:00
use crate::Error;
2020-02-23 22:19:54 +00:00
/// The maximum number of independent, simultaneous audio channels present in
/// any [`MediaSegment`] in the rendition.
2019-10-03 14:23:27 +00:00
///
2020-02-23 22:19:54 +00:00
/// For example, an `AC-3 5.1` rendition would have a maximum channel number of
/// 6.
2019-10-03 14:23:27 +00:00
///
/// [`MediaSegment`]: crate::MediaSegment
2020-02-23 22:19:54 +00:00
#[derive(ShortHand, Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[shorthand(enable(must_use))]
2019-10-03 14:23:27 +00:00
pub struct Channels {
2020-02-23 22:19:54 +00:00
/// The maximum number of independent simultaneous audio channels.
2019-10-03 14:23:27 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-10-03 14:23:27 +00:00
/// ```
/// # use hls_m3u8::types::Channels;
/// let mut channels = Channels::new(6);
2020-02-23 22:19:54 +00:00
/// # assert_eq!(channels.number(), 6);
2020-02-02 12:38:11 +00:00
///
2020-02-23 22:19:54 +00:00
/// channels.set_number(5);
/// assert_eq!(channels.number(), 5);
2019-10-03 14:23:27 +00:00
/// ```
2020-02-23 22:19:54 +00:00
number: u64,
}
2019-10-03 14:23:27 +00:00
2020-02-23 22:19:54 +00:00
impl Channels {
/// Makes a new [`Channels`] struct.
2019-10-03 14:23:27 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-10-03 14:23:27 +00:00
/// ```
/// # use hls_m3u8::types::Channels;
2020-02-23 22:19:54 +00:00
/// let channels = Channels::new(6);
2019-10-03 14:23:27 +00:00
///
2020-02-23 22:19:54 +00:00
/// println!("CHANNELS=\"{}\"", channels);
/// # assert_eq!(format!("CHANNELS=\"{}\"", channels), "CHANNELS=\"6\"".to_string());
2019-10-03 14:23:27 +00:00
/// ```
2020-02-23 22:19:54 +00:00
//#[inline]
#[must_use]
pub const fn new(number: u64) -> Self { Self { number } }
2019-10-03 14:23:27 +00:00
}
impl FromStr for Channels {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
2020-02-23 22:19:54 +00:00
Ok(Self::new(
input.parse().map_err(|e| Error::parse_int(input, e))?,
))
2019-10-03 14:23:27 +00:00
}
}
impl fmt::Display for Channels {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2020-02-23 22:19:54 +00:00
write!(f, "{}", self.number)?;
2019-10-03 14:23:27 +00:00
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
2019-10-03 14:23:27 +00:00
#[test]
fn test_display() {
2020-02-23 22:19:54 +00:00
assert_eq!(Channels::new(6).to_string(), "6".to_string());
2019-10-03 14:23:27 +00:00
2020-02-23 22:19:54 +00:00
assert_eq!(Channels::new(7).to_string(), "7".to_string());
2019-10-03 14:23:27 +00:00
}
#[test]
fn test_parser() {
2020-02-23 22:19:54 +00:00
assert_eq!(Channels::new(6), Channels::from_str("6").unwrap());
assert!(Channels::from_str("garbage").is_err());
assert!(Channels::from_str("").is_err());
2019-10-03 14:23:27 +00:00
}
}