1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-16 20:10:33 +00:00

improve KeyFormatVersions

This commit is contained in:
Luro02 2020-02-24 14:49:20 +01:00
parent 90783fdd9d
commit dc12db9fad
No known key found for this signature in database
GPG key ID: B66FD4F74501A9CF

View file

@ -1,4 +1,3 @@
use std::convert::Infallible;
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
@ -6,9 +5,10 @@ use derive_more::{Deref, DerefMut};
use crate::types::ProtocolVersion; use crate::types::ProtocolVersion;
use crate::utils::{quote, unquote}; use crate::utils::{quote, unquote};
use crate::Error;
use crate::RequiredVersion; use crate::RequiredVersion;
/// A list of [`usize`], that can be used to indicate which version(s) /// A list of numbers that can be used to indicate which version(s)
/// this instance complies with, if more than one version of a particular /// this instance complies with, if more than one version of a particular
/// [`KeyFormat`] is defined. /// [`KeyFormat`] is defined.
/// ///
@ -18,9 +18,27 @@ pub struct KeyFormatVersions(Vec<usize>);
impl KeyFormatVersions { impl KeyFormatVersions {
/// Makes a new [`KeyFormatVersions`]. /// Makes a new [`KeyFormatVersions`].
///
/// # Example
///
/// ```
/// # use hls_m3u8::types::KeyFormatVersions;
/// let key_format_versions = KeyFormatVersions::new();
/// ```
#[inline]
#[must_use]
pub fn new() -> Self { Self::default() } pub fn new() -> Self { Self::default() }
/// Add a value to the [`KeyFormatVersions`]. /// Add a value to the end of [`KeyFormatVersions`].
///
/// # Example
///
/// ```
/// # use hls_m3u8::types::KeyFormatVersions;
/// let mut key_format_versions = KeyFormatVersions::new();
///
/// key_format_versions.push(1);
/// ```
pub fn push(&mut self, value: usize) { pub fn push(&mut self, value: usize) {
if self.is_default() { if self.is_default() {
self.0 = vec![value]; self.0 = vec![value];
@ -31,6 +49,14 @@ impl KeyFormatVersions {
/// Returns `true`, if [`KeyFormatVersions`] has the default value of /// Returns `true`, if [`KeyFormatVersions`] has the default value of
/// `vec![1]`. /// `vec![1]`.
///
/// # Example
///
/// ```
/// # use hls_m3u8::types::KeyFormatVersions;
/// assert!(KeyFormatVersions::from(vec![1]).is_default());
/// ```
#[must_use]
pub fn is_default(&self) -> bool { pub fn is_default(&self) -> bool {
// //
self.0 == vec![1] && self.0.len() == 1 || self.0.is_empty() self.0 == vec![1] && self.0.len() == 1 || self.0.is_empty()
@ -47,13 +73,13 @@ impl RequiredVersion for KeyFormatVersions {
} }
impl FromStr for KeyFormatVersions { impl FromStr for KeyFormatVersions {
type Err = Infallible; type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> { fn from_str(input: &str) -> Result<Self, Self::Err> {
let mut result = unquote(input) let mut result = unquote(input)
.split('/') .split('/')
.filter_map(|v| v.parse().ok()) .map(|v| v.parse().map_err(|e| Error::parse_int(v, e)))
.collect::<Vec<_>>(); .collect::<Result<Vec<_>, Error>>()?;
if result.is_empty() { if result.is_empty() {
result.push(1); result.push(1);
@ -69,23 +95,22 @@ impl fmt::Display for KeyFormatVersions {
return write!(f, "{}", quote("1")); return write!(f, "{}", quote("1"));
} }
write!( if let Some(value) = self.0.iter().next() {
f, write!(f, "\"{}", value)?;
"{}",
quote( for value in self.0.iter().skip(1) {
// vec![1, 2, 3] -> "1/2/3" write!(f, "/{}", value)?;
self.0 }
.iter()
.map(ToString::to_string) write!(f, "\"")?;
.collect::<Vec<String>>() }
.join("/")
) Ok(())
)
} }
} }
impl<T: Into<Vec<usize>>> From<T> for KeyFormatVersions { impl<I: IntoIterator<Item = usize>> From<I> for KeyFormatVersions {
fn from(value: T) -> Self { Self(value.into()) } fn from(value: I) -> Self { Self(value.into_iter().collect()) }
} }
#[cfg(test)] #[cfg(test)]
@ -101,7 +126,6 @@ mod tests {
); );
assert_eq!(KeyFormatVersions::from(vec![]).to_string(), quote("1")); assert_eq!(KeyFormatVersions::from(vec![]).to_string(), quote("1"));
assert_eq!(KeyFormatVersions::new().to_string(), quote("1")); assert_eq!(KeyFormatVersions::new().to_string(), quote("1"));
} }
@ -115,6 +139,7 @@ mod tests {
assert_eq!(KeyFormatVersions::from(vec![1]), "1".parse().unwrap()); assert_eq!(KeyFormatVersions::from(vec![1]), "1".parse().unwrap());
assert_eq!(KeyFormatVersions::from(vec![1, 2]), "1/2".parse().unwrap()); assert_eq!(KeyFormatVersions::from(vec![1, 2]), "1/2".parse().unwrap());
assert!("1/b".parse::<KeyFormatVersions>().is_err());
} }
#[test] #[test]