1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 05:21:10 +00:00

remove draft content

This commit is contained in:
Luro02 2019-10-04 11:19:03 +02:00
parent 4b4cffc248
commit f96207c93e

View file

@ -11,14 +11,6 @@ use crate::Error;
/// present in any [`MediaSegment`] in the rendition. For example, an /// present in any [`MediaSegment`] in the rendition. For example, an
/// `AC-3 5.1` rendition would have a `CHANNELS="6"` attribute. /// `AC-3 5.1` rendition would have a `CHANNELS="6"` attribute.
/// ///
/// The second parameter identifies the encoding of object-based audio used by
/// the rendition. This parameter is a comma-separated list of Audio
/// Object Coding Identifiers. It is optional. An Audio Object
/// Coding Identifier is a string containing characters from the set
/// `[A..Z]`, `[0..9]`, and `'-'`. They are codec-specific. A parameter
/// value of consisting solely of the dash character (`'-'`) indicates
/// that the audio is not object-based.
///
/// # Example /// # Example
/// Creating a `CHANNELS="6"` attribute /// Creating a `CHANNELS="6"` attribute
/// ``` /// ```
@ -31,16 +23,11 @@ use crate::Error;
/// ); /// );
/// ``` /// ```
/// ///
/// # Note
/// Currently there are no example playlists in the documentation,
/// or in popular m3u8 libraries, showing a usage for the second parameter
/// of [`Channels`], so if you have one please open an issue on github!
///
/// [`MediaSegment`]: crate::MediaSegment /// [`MediaSegment`]: crate::MediaSegment
#[derive(Debug, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Debug, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Channels { pub struct Channels {
first_parameter: u64, channel_number: u64,
second_parameter: Option<Vec<String>>, unknown: Vec<String>,
} }
impl Channels { impl Channels {
@ -51,77 +38,36 @@ impl Channels {
/// # use hls_m3u8::types::Channels; /// # use hls_m3u8::types::Channels;
/// let mut channels = Channels::new(6); /// let mut channels = Channels::new(6);
/// ``` /// ```
pub const fn new(value: u64) -> Self { pub fn new(value: u64) -> Self {
Self { Self {
first_parameter: value, channel_number: value,
second_parameter: None, unknown: vec![],
} }
} }
/// Returns the first parameter. /// Returns the channel number.
/// ///
/// # Example /// # Example
/// ``` /// ```
/// # use hls_m3u8::types::Channels; /// # use hls_m3u8::types::Channels;
/// let mut channels = Channels::new(6); /// let mut channels = Channels::new(6);
/// ///
/// assert_eq!(channels.first_parameter(), 6); /// assert_eq!(channels.channel_number(), 6);
/// ``` /// ```
pub const fn first_parameter(&self) -> u64 { self.first_parameter } pub const fn channel_number(&self) -> u64 { self.channel_number }
/// Sets the first parameter. /// Sets the channel number.
/// ///
/// # Example /// # Example
/// ``` /// ```
/// # use hls_m3u8::types::Channels; /// # use hls_m3u8::types::Channels;
/// let mut channels = Channels::new(3); /// let mut channels = Channels::new(3);
/// ///
/// channels.set_first_parameter(6); /// channels.set_channel_number(6);
/// assert_eq!(channels.first_parameter(), 6) /// assert_eq!(channels.channel_number(), 6)
/// ``` /// ```
pub fn set_first_parameter(&mut self, value: u64) -> &mut Self { pub fn set_channel_number(&mut self, value: u64) -> &mut Self {
self.first_parameter = value; self.channel_number = value;
self
}
/// Returns the second parameter, if there is any!
///
/// # Example
/// ```
/// # use hls_m3u8::types::Channels;
/// let mut channels = Channels::new(3);
/// # assert_eq!(channels.second_parameter(), &None);
///
/// channels.set_second_parameter(Some(vec!["AAC", "MP3"]));
/// assert_eq!(
/// channels.second_parameter(),
/// &Some(vec!["AAC".to_string(), "MP3".to_string()])
/// )
/// ```
///
/// # Note
/// Currently there is no use for this parameter.
pub const fn second_parameter(&self) -> &Option<Vec<String>> { &self.second_parameter }
/// Sets the second parameter.
///
/// # Example
/// ```
/// # use hls_m3u8::types::Channels;
/// let mut channels = Channels::new(3);
/// # assert_eq!(channels.second_parameter(), &None);
///
/// channels.set_second_parameter(Some(vec!["AAC", "MP3"]));
/// assert_eq!(
/// channels.second_parameter(),
/// &Some(vec!["AAC".to_string(), "MP3".to_string()])
/// )
/// ```
///
/// # Note
/// Currently there is no use for this parameter.
pub fn set_second_parameter<T: ToString>(&mut self, value: Option<Vec<T>>) -> &mut Self {
self.second_parameter = value.map(|v| v.into_iter().map(|s| s.to_string()).collect());
self self
} }
} }
@ -131,28 +77,23 @@ impl FromStr for Channels {
fn from_str(input: &str) -> Result<Self, Self::Err> { fn from_str(input: &str) -> Result<Self, Self::Err> {
let parameters = input.split('/').collect::<Vec<_>>(); let parameters = input.split('/').collect::<Vec<_>>();
let first_parameter = parameters let channel_number = parameters
.first() .first()
.ok_or_else(|| Error::missing_attribute("First parameter of channels!"))? .ok_or_else(|| Error::missing_attribute("First parameter of channels!"))?
.parse()?; .parse()?;
let second_parameter = parameters
.get(1)
.map(|v| v.split(',').map(|v| v.to_string()).collect());
Ok(Self { Ok(Self {
first_parameter, channel_number,
second_parameter, unknown: parameters[1..].iter().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.first_parameter)?; write!(f, "{}", self.channel_number)?;
if !self.unknown.is_empty() {
if let Some(second) = &self.second_parameter { write!(f, "{}", self.unknown.join(","))?;
write!(f, "/{}", second.join(","))?;
} }
Ok(()) Ok(())
@ -168,25 +109,13 @@ mod tests {
let mut channels = Channels::new(6); let mut channels = Channels::new(6);
assert_eq!(channels.to_string(), "6".to_string()); assert_eq!(channels.to_string(), "6".to_string());
channels.set_first_parameter(7); channels.set_channel_number(7);
assert_eq!(channels.to_string(), "7".to_string()); assert_eq!(channels.to_string(), "7".to_string());
assert_eq!(
"6/P,K,J".to_string(),
Channels::new(6)
.set_second_parameter(Some(vec!["P", "K", "J"]))
.to_string()
);
} }
#[test] #[test]
fn test_parser() { fn test_parser() {
assert_eq!("6".parse::<Channels>().unwrap(), Channels::new(6)); assert_eq!("6".parse::<Channels>().unwrap(), Channels::new(6));
let mut result = Channels::new(6);
result.set_second_parameter(Some(vec!["P", "K", "J"]));
assert_eq!("6/P,K,J".parse::<Channels>().unwrap(), result);
assert!("garbage".parse::<Channels>().is_err()); assert!("garbage".parse::<Channels>().is_err());
assert!("".parse::<Channels>().is_err()); assert!("".parse::<Channels>().is_err());
} }