From dc12db9fad692d3ca220af8334390ba721b07370 Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Mon, 24 Feb 2020 14:49:20 +0100 Subject: [PATCH] improve KeyFormatVersions --- src/types/key_format_versions.rs | 67 ++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/src/types/key_format_versions.rs b/src/types/key_format_versions.rs index 0bd1189..214b725 100644 --- a/src/types/key_format_versions.rs +++ b/src/types/key_format_versions.rs @@ -1,4 +1,3 @@ -use std::convert::Infallible; use std::fmt; use std::str::FromStr; @@ -6,9 +5,10 @@ use derive_more::{Deref, DerefMut}; use crate::types::ProtocolVersion; use crate::utils::{quote, unquote}; +use crate::Error; 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 /// [`KeyFormat`] is defined. /// @@ -18,9 +18,27 @@ pub struct KeyFormatVersions(Vec); impl 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() } - /// 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) { if self.is_default() { self.0 = vec![value]; @@ -31,6 +49,14 @@ impl KeyFormatVersions { /// Returns `true`, if [`KeyFormatVersions`] has the default value of /// `vec![1]`. + /// + /// # Example + /// + /// ``` + /// # use hls_m3u8::types::KeyFormatVersions; + /// assert!(KeyFormatVersions::from(vec![1]).is_default()); + /// ``` + #[must_use] pub fn is_default(&self) -> bool { // self.0 == vec![1] && self.0.len() == 1 || self.0.is_empty() @@ -47,13 +73,13 @@ impl RequiredVersion for KeyFormatVersions { } impl FromStr for KeyFormatVersions { - type Err = Infallible; + type Err = Error; fn from_str(input: &str) -> Result { let mut result = unquote(input) .split('/') - .filter_map(|v| v.parse().ok()) - .collect::>(); + .map(|v| v.parse().map_err(|e| Error::parse_int(v, e))) + .collect::, Error>>()?; if result.is_empty() { result.push(1); @@ -69,23 +95,22 @@ impl fmt::Display for KeyFormatVersions { return write!(f, "{}", quote("1")); } - write!( - f, - "{}", - quote( - // vec![1, 2, 3] -> "1/2/3" - self.0 - .iter() - .map(ToString::to_string) - .collect::>() - .join("/") - ) - ) + if let Some(value) = self.0.iter().next() { + write!(f, "\"{}", value)?; + + for value in self.0.iter().skip(1) { + write!(f, "/{}", value)?; + } + + write!(f, "\"")?; + } + + Ok(()) } } -impl>> From for KeyFormatVersions { - fn from(value: T) -> Self { Self(value.into()) } +impl> From for KeyFormatVersions { + fn from(value: I) -> Self { Self(value.into_iter().collect()) } } #[cfg(test)] @@ -101,7 +126,6 @@ mod tests { ); assert_eq!(KeyFormatVersions::from(vec![]).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, 2]), "1/2".parse().unwrap()); + assert!("1/b".parse::().is_err()); } #[test]