1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-26 00:10:32 +00:00
hls_m3u8/src/types/key_format_versions.rs

155 lines
4 KiB
Rust
Raw Normal View History

use std::convert::Infallible;
2019-09-22 16:00:38 +00:00
use std::fmt;
use std::str::FromStr;
2020-02-02 14:23:47 +00:00
use derive_more::{Deref, DerefMut};
2019-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
2019-09-22 16:00:38 +00:00
use crate::utils::{quote, unquote};
use crate::RequiredVersion;
2019-09-22 16:00:38 +00:00
2019-10-06 14:39:18 +00:00
/// A list of [`usize`], that can be used to indicate which version(s)
2019-09-22 16:00:38 +00:00
/// this instance complies with, if more than one version of a particular
2019-10-03 14:23:27 +00:00
/// [`KeyFormat`] is defined.
2019-09-22 16:00:38 +00:00
///
2019-10-03 14:23:27 +00:00
/// [`KeyFormat`]: crate::types::KeyFormat
2020-02-02 14:23:47 +00:00
#[derive(Deref, DerefMut, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
2019-09-22 16:00:38 +00:00
pub struct KeyFormatVersions(Vec<usize>);
impl KeyFormatVersions {
2019-10-03 14:23:27 +00:00
/// Makes a new [`KeyFormatVersions`].
2019-10-03 15:01:15 +00:00
pub fn new() -> Self { Self::default() }
2019-09-22 16:00:38 +00:00
2019-10-03 14:23:27 +00:00
/// Add a value to the [`KeyFormatVersions`].
2019-09-22 16:00:38 +00:00
pub fn push(&mut self, value: usize) {
if self.is_default() {
self.0 = vec![value];
} else {
self.0.push(value);
}
}
2019-10-03 15:01:15 +00:00
/// Returns `true`, if [`KeyFormatVersions`] has the default value of
/// `vec![1]`.
2020-02-10 12:21:48 +00:00
pub fn is_default(&self) -> bool {
//
self.0 == vec![1] && self.0.len() == 1 || self.0.is_empty()
}
2019-09-22 16:00:38 +00:00
}
impl Default for KeyFormatVersions {
fn default() -> Self { Self(vec![1]) }
}
/// This tag requires [`ProtocolVersion::V5`].
2019-09-22 16:00:38 +00:00
impl RequiredVersion for KeyFormatVersions {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V5 }
2019-09-22 16:00:38 +00:00
}
impl FromStr for KeyFormatVersions {
type Err = Infallible;
2019-09-22 16:00:38 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
let mut result = unquote(input)
2019-09-22 18:33:40 +00:00
.split('/')
2019-09-22 16:00:38 +00:00
.filter_map(|v| v.parse().ok())
.collect::<Vec<_>>();
if result.is_empty() {
result.push(1);
}
Ok(Self(result))
}
}
impl fmt::Display for KeyFormatVersions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_default() {
return write!(f, "{}", quote("1"));
}
write!(
f,
"{}",
quote(
// vec![1, 2, 3] -> "1/2/3"
self.0
.iter()
2020-02-10 12:21:48 +00:00
.map(ToString::to_string)
2019-09-22 16:00:38 +00:00
.collect::<Vec<String>>()
.join("/")
)
)
}
}
impl<T: Into<Vec<usize>>> From<T> for KeyFormatVersions {
2019-10-03 15:01:15 +00:00
fn from(value: T) -> Self { Self(value.into()) }
2019-09-22 16:00:38 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
2019-09-22 16:00:38 +00:00
#[test]
fn test_display() {
assert_eq!(
KeyFormatVersions::from(vec![1, 2, 3, 4, 5]).to_string(),
quote("1/2/3/4/5")
);
assert_eq!(KeyFormatVersions::from(vec![]).to_string(), quote("1"));
assert_eq!(KeyFormatVersions::new().to_string(), quote("1"));
}
#[test]
fn test_parser() {
assert_eq!(
KeyFormatVersions::from(vec![1, 2, 3, 4, 5]),
quote("1/2/3/4/5").parse().unwrap()
);
assert_eq!(KeyFormatVersions::from(vec![1]), "1".parse().unwrap());
assert_eq!(KeyFormatVersions::from(vec![1, 2]), "1/2".parse().unwrap());
}
#[test]
fn test_required_version() {
assert_eq!(
KeyFormatVersions::new().required_version(),
ProtocolVersion::V5
)
}
#[test]
fn test_is_default() {
assert!(KeyFormatVersions::new().is_default());
assert!(KeyFormatVersions::from(vec![]).is_default());
assert!(!KeyFormatVersions::from(vec![1, 2, 3]).is_default());
}
#[test]
fn test_push() {
let mut key_format_versions = KeyFormatVersions::from(vec![]);
key_format_versions.push(2);
assert_eq!(KeyFormatVersions::from(vec![2]), key_format_versions);
}
#[test]
fn test_deref() {
assert!(!KeyFormatVersions::new().is_empty());
}
#[test]
fn test_deref_mut() {
let mut key_format_versions = KeyFormatVersions::from(vec![1, 2, 3]);
key_format_versions.pop();
assert_eq!(key_format_versions, KeyFormatVersions::from(vec![1, 2]));
}
}