1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-10 01:09:27 +00:00
hls_m3u8/src/types/playlist_type.rs

102 lines
2.7 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
use std::str::FromStr;
2019-09-13 14:06:52 +00:00
2019-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
2019-09-13 14:06:52 +00:00
use crate::utils::tag;
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-06 10:55:00 +00:00
2020-04-09 06:26:50 +00:00
/// Provides mutability information about the [`MediaPlaylist`].
2019-09-06 10:55:00 +00:00
///
2020-04-09 06:26:50 +00:00
/// It applies to the entire [`MediaPlaylist`].
2019-09-15 08:40:45 +00:00
///
2020-02-14 12:05:18 +00:00
/// [`MediaPlaylist`]: crate::MediaPlaylist
2019-10-12 09:38:28 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum PlaylistType {
/// If the [`PlaylistType`] is Event, [`MediaSegment`]s
2020-02-14 12:05:18 +00:00
/// can only be added to the end of the [`MediaPlaylist`].
2019-10-05 10:49:08 +00:00
///
2020-02-14 12:05:18 +00:00
/// [`MediaSegment`]: crate::MediaSegment
/// [`MediaPlaylist`]: crate::MediaPlaylist
2019-09-15 08:40:45 +00:00
Event,
/// If the [`PlaylistType`] is Video On Demand (Vod),
2020-02-14 12:05:18 +00:00
/// the [`MediaPlaylist`] cannot change.
2019-10-05 10:49:08 +00:00
///
2020-02-14 12:05:18 +00:00
/// [`MediaPlaylist`]: crate::MediaPlaylist
2019-09-15 08:40:45 +00:00
Vod,
2019-09-06 10:55:00 +00:00
}
impl PlaylistType {
2019-09-06 10:55:00 +00:00
pub(crate) const PREFIX: &'static str = "#EXT-X-PLAYLIST-TYPE:";
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2019-10-05 10:49:08 +00:00
/// This tag requires [`ProtocolVersion::V1`].
impl RequiredVersion for PlaylistType {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
2019-09-06 10:55:00 +00:00
}
impl fmt::Display for PlaylistType {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-09-15 08:40:45 +00:00
match &self {
Self::Event => write!(f, "{}EVENT", Self::PREFIX),
Self::Vod => write!(f, "{}VOD", Self::PREFIX),
}
2019-09-06 10:55:00 +00:00
}
}
impl FromStr for PlaylistType {
2019-09-06 10:55:00 +00:00
type Err = Error;
2019-09-08 10:23:33 +00:00
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
2019-09-15 08:40:45 +00:00
let input = tag(input, Self::PREFIX)?;
match input {
"EVENT" => Ok(Self::Event),
"VOD" => Ok(Self::Vod),
_ => Err(Error::custom(format!("unknown playlist type: {:?}", input))),
2019-09-15 08:40:45 +00:00
}
2019-09-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
2019-09-06 10:55:00 +00:00
#[test]
2019-09-15 08:40:45 +00:00
fn test_parser() {
assert_eq!(
"#EXT-X-PLAYLIST-TYPE:VOD".parse::<PlaylistType>().unwrap(),
PlaylistType::Vod,
2019-09-15 08:40:45 +00:00
);
assert_eq!(
"#EXT-X-PLAYLIST-TYPE:EVENT"
.parse::<PlaylistType>()
2019-09-15 08:40:45 +00:00
.unwrap(),
PlaylistType::Event,
2019-09-15 08:40:45 +00:00
);
assert!("#EXT-X-PLAYLIST-TYPE:H".parse::<PlaylistType>().is_err());
2019-10-05 10:49:08 +00:00
assert!("garbage".parse::<PlaylistType>().is_err());
2019-09-15 08:40:45 +00:00
}
#[test]
fn test_display() {
assert_eq!(
"#EXT-X-PLAYLIST-TYPE:VOD".to_string(),
PlaylistType::Vod.to_string(),
2019-09-15 08:40:45 +00:00
);
assert_eq!(
"#EXT-X-PLAYLIST-TYPE:EVENT".to_string(),
PlaylistType::Event.to_string(),
2019-09-15 08:40:45 +00:00
);
}
#[test]
2019-09-22 08:57:28 +00:00
fn test_required_version() {
assert_eq!(PlaylistType::Vod.required_version(), ProtocolVersion::V1);
assert_eq!(PlaylistType::Event.required_version(), ProtocolVersion::V1);
2019-09-06 10:55:00 +00:00
}
}