mirror of
https://github.com/sile/hls_m3u8.git
synced 2024-11-25 16:41:00 +00:00
improve Channels
This commit is contained in:
parent
03b0d2cf0c
commit
88a5fa4460
1 changed files with 38 additions and 84 deletions
|
@ -1,37 +1,33 @@
|
|||
use core::fmt;
|
||||
use core::str::FromStr;
|
||||
|
||||
use shorthand::ShortHand;
|
||||
|
||||
use crate::Error;
|
||||
|
||||
/// Specifies a list of parameters.
|
||||
/// The maximum number of independent, simultaneous audio channels present in
|
||||
/// any [`MediaSegment`] in the rendition.
|
||||
///
|
||||
/// # `MediaType::Audio`
|
||||
/// For example, an `AC-3 5.1` rendition would have a maximum channel number of
|
||||
/// 6.
|
||||
///
|
||||
/// The first parameter is a count of audio channels expressed as a [`u64`],
|
||||
/// indicating the maximum number of independent, simultaneous audio channels
|
||||
/// present in any [`MediaSegment`] in the rendition.
|
||||
///
|
||||
/// For example, an `AC-3 5.1` rendition would have a `CHANNELS="6"` attribute.
|
||||
/// [`MediaSegment`]: crate::MediaSegment
|
||||
#[derive(ShortHand, Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
#[shorthand(enable(must_use))]
|
||||
pub struct Channels {
|
||||
/// The maximum number of independent simultaneous audio channels.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// Creating a `CHANNELS="6"` attribute
|
||||
///
|
||||
/// ```
|
||||
/// # use hls_m3u8::types::Channels;
|
||||
/// let mut channels = Channels::new(6);
|
||||
/// # assert_eq!(channels.number(), 6);
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// format!("CHANNELS=\"{}\"", channels),
|
||||
/// "CHANNELS=\"6\"".to_string()
|
||||
/// );
|
||||
/// channels.set_number(5);
|
||||
/// assert_eq!(channels.number(), 5);
|
||||
/// ```
|
||||
///
|
||||
/// [`MediaSegment`]: crate::MediaSegment
|
||||
#[derive(Debug, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct Channels {
|
||||
channel_number: u64,
|
||||
unknown: Vec<String>,
|
||||
number: u64,
|
||||
}
|
||||
|
||||
impl Channels {
|
||||
|
@ -41,70 +37,29 @@ impl Channels {
|
|||
///
|
||||
/// ```
|
||||
/// # use hls_m3u8::types::Channels;
|
||||
/// let mut channels = Channels::new(6);
|
||||
/// let channels = Channels::new(6);
|
||||
///
|
||||
/// println!("CHANNELS=\"{}\"", channels);
|
||||
/// # assert_eq!(format!("CHANNELS=\"{}\"", channels), "CHANNELS=\"6\"".to_string());
|
||||
/// ```
|
||||
pub fn new(value: u64) -> Self {
|
||||
Self {
|
||||
channel_number: value,
|
||||
unknown: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the channel number.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use hls_m3u8::types::Channels;
|
||||
/// let mut channels = Channels::new(6);
|
||||
///
|
||||
/// assert_eq!(channels.channel_number(), 6);
|
||||
/// ```
|
||||
pub const fn channel_number(&self) -> u64 { self.channel_number }
|
||||
|
||||
/// Sets the channel number.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use hls_m3u8::types::Channels;
|
||||
/// let mut channels = Channels::new(3);
|
||||
///
|
||||
/// channels.set_channel_number(6);
|
||||
/// assert_eq!(channels.channel_number(), 6)
|
||||
/// ```
|
||||
pub fn set_channel_number(&mut self, value: u64) -> &mut Self {
|
||||
self.channel_number = value;
|
||||
self
|
||||
}
|
||||
//#[inline]
|
||||
#[must_use]
|
||||
pub const fn new(number: u64) -> Self { Self { number } }
|
||||
}
|
||||
|
||||
impl FromStr for Channels {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
||||
let mut parameters = input.split('/');
|
||||
|
||||
let param_1 = parameters
|
||||
.next()
|
||||
.ok_or_else(|| Error::missing_attribute("first parameter of channels"))?;
|
||||
|
||||
let channel_number = param_1.parse().map_err(|e| Error::parse_int(param_1, e))?;
|
||||
|
||||
Ok(Self {
|
||||
channel_number,
|
||||
unknown: parameters.map(|v| (*v).to_string()).collect(),
|
||||
})
|
||||
Ok(Self::new(
|
||||
input.parse().map_err(|e| Error::parse_int(input, e))?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Channels {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.channel_number)?;
|
||||
|
||||
if !self.unknown.is_empty() {
|
||||
write!(f, "{}", self.unknown.join(","))?;
|
||||
}
|
||||
write!(f, "{}", self.number)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -117,17 +72,16 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_display() {
|
||||
let mut channels = Channels::new(6);
|
||||
assert_eq!(channels.to_string(), "6".to_string());
|
||||
assert_eq!(Channels::new(6).to_string(), "6".to_string());
|
||||
|
||||
channels.set_channel_number(7);
|
||||
assert_eq!(channels.to_string(), "7".to_string());
|
||||
assert_eq!(Channels::new(7).to_string(), "7".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parser() {
|
||||
assert_eq!("6".parse::<Channels>().unwrap(), Channels::new(6));
|
||||
assert!("garbage".parse::<Channels>().is_err());
|
||||
assert!("".parse::<Channels>().is_err());
|
||||
assert_eq!(Channels::new(6), Channels::from_str("6").unwrap());
|
||||
|
||||
assert!(Channels::from_str("garbage").is_err());
|
||||
assert!(Channels::from_str("").is_err());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue