mirror of
https://github.com/sile/hls_m3u8.git
synced 2024-12-01 11:01:11 +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::fmt;
|
||||||
use core::str::FromStr;
|
use core::str::FromStr;
|
||||||
|
|
||||||
|
use shorthand::ShortHand;
|
||||||
|
|
||||||
use crate::Error;
|
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.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// Creating a `CHANNELS="6"` attribute
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use hls_m3u8::types::Channels;
|
|
||||||
/// let mut channels = Channels::new(6);
|
|
||||||
///
|
|
||||||
/// assert_eq!(
|
|
||||||
/// format!("CHANNELS=\"{}\"", channels),
|
|
||||||
/// "CHANNELS=\"6\"".to_string()
|
|
||||||
/// );
|
|
||||||
/// ```
|
|
||||||
///
|
///
|
||||||
/// [`MediaSegment`]: crate::MediaSegment
|
/// [`MediaSegment`]: crate::MediaSegment
|
||||||
#[derive(Debug, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
#[derive(ShortHand, Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||||
|
#[shorthand(enable(must_use))]
|
||||||
pub struct Channels {
|
pub struct Channels {
|
||||||
channel_number: u64,
|
/// The maximum number of independent simultaneous audio channels.
|
||||||
unknown: Vec<String>,
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use hls_m3u8::types::Channels;
|
||||||
|
/// let mut channels = Channels::new(6);
|
||||||
|
/// # assert_eq!(channels.number(), 6);
|
||||||
|
///
|
||||||
|
/// channels.set_number(5);
|
||||||
|
/// assert_eq!(channels.number(), 5);
|
||||||
|
/// ```
|
||||||
|
number: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Channels {
|
impl Channels {
|
||||||
|
@ -41,70 +37,29 @@ impl Channels {
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use hls_m3u8::types::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 {
|
//#[inline]
|
||||||
Self {
|
#[must_use]
|
||||||
channel_number: value,
|
pub const fn new(number: u64) -> Self { Self { number } }
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Channels {
|
impl FromStr for Channels {
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
|
|
||||||
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
||||||
let mut parameters = input.split('/');
|
Ok(Self::new(
|
||||||
|
input.parse().map_err(|e| Error::parse_int(input, e))?,
|
||||||
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(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Channels {
|
impl fmt::Display for Channels {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self.channel_number)?;
|
write!(f, "{}", self.number)?;
|
||||||
|
|
||||||
if !self.unknown.is_empty() {
|
|
||||||
write!(f, "{}", self.unknown.join(","))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -117,17 +72,16 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_display() {
|
fn test_display() {
|
||||||
let mut channels = Channels::new(6);
|
assert_eq!(Channels::new(6).to_string(), "6".to_string());
|
||||||
assert_eq!(channels.to_string(), "6".to_string());
|
|
||||||
|
|
||||||
channels.set_channel_number(7);
|
assert_eq!(Channels::new(7).to_string(), "7".to_string());
|
||||||
assert_eq!(channels.to_string(), "7".to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parser() {
|
fn test_parser() {
|
||||||
assert_eq!("6".parse::<Channels>().unwrap(), Channels::new(6));
|
assert_eq!(Channels::new(6), Channels::from_str("6").unwrap());
|
||||||
assert!("garbage".parse::<Channels>().is_err());
|
|
||||||
assert!("".parse::<Channels>().is_err());
|
assert!(Channels::from_str("garbage").is_err());
|
||||||
|
assert!(Channels::from_str("").is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue