1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-15 03:20:35 +00:00
hls_m3u8/src/tags/media_playlist/playlist_type.rs

113 lines
2.8 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-09-22 08:57:28 +00:00
use crate::types::{ProtocolVersion, RequiredVersion};
2019-09-13 14:06:52 +00:00
use crate::utils::tag;
use crate::Error;
2019-09-06 10:55:00 +00:00
2019-09-22 16:00:38 +00:00
/// # [4.4.3.5. EXT-X-PLAYLIST-TYPE]
2019-09-06 10:55:00 +00:00
///
2019-09-22 16:00:38 +00:00
/// The [ExtXPlaylistType] tag provides mutability information about the
/// [Media Playlist]. It applies to the entire [Media Playlist].
2019-09-15 08:40:45 +00:00
///
2019-09-22 16:00:38 +00:00
/// Its format is:
2019-09-15 08:40:45 +00:00
/// ```text
/// #EXT-X-PLAYLIST-TYPE:<type-enum>
/// ```
///
2019-09-22 16:00:38 +00:00
/// [Media Playlist]: crate::MediaPlaylist
/// [4.4.3.5. EXT-X-PLAYLIST-TYPE]:
/// https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-04#section-4.4.3.5
2019-09-06 10:55:00 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2019-09-15 08:40:45 +00:00
pub enum ExtXPlaylistType {
2019-09-22 16:00:38 +00:00
/// If the [ExtXPlaylistType] is Event, Media Segments can only be added to
2019-09-15 08:40:45 +00:00
/// the end of the Media Playlist.
Event,
2019-09-22 16:00:38 +00:00
/// If the [ExtXPlaylistType] is Video On Demand (Vod),
2019-09-15 08:40:45 +00:00
/// the Media Playlist cannot change.
Vod,
2019-09-06 10:55:00 +00:00
}
impl ExtXPlaylistType {
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-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXPlaylistType {
fn required_version(&self) -> ProtocolVersion {
2019-09-06 10:55:00 +00:00
ProtocolVersion::V1
}
}
impl fmt::Display for ExtXPlaylistType {
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 ExtXPlaylistType {
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-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
2019-09-15 08:40:45 +00:00
fn test_parser() {
assert_eq!(
"#EXT-X-PLAYLIST-TYPE:VOD"
.parse::<ExtXPlaylistType>()
.unwrap(),
ExtXPlaylistType::Vod,
);
assert_eq!(
"#EXT-X-PLAYLIST-TYPE:EVENT"
.parse::<ExtXPlaylistType>()
.unwrap(),
ExtXPlaylistType::Event,
);
assert!("#EXT-X-PLAYLIST-TYPE:H"
.parse::<ExtXPlaylistType>()
.is_err());
}
#[test]
fn test_display() {
assert_eq!(
"#EXT-X-PLAYLIST-TYPE:VOD".to_string(),
ExtXPlaylistType::Vod.to_string(),
);
assert_eq!(
"#EXT-X-PLAYLIST-TYPE:EVENT".to_string(),
ExtXPlaylistType::Event.to_string(),
);
}
#[test]
2019-09-22 08:57:28 +00:00
fn test_required_version() {
2019-09-15 08:40:45 +00:00
assert_eq!(
2019-09-22 08:57:28 +00:00
ExtXPlaylistType::Vod.required_version(),
2019-09-15 08:40:45 +00:00
ProtocolVersion::V1
);
assert_eq!(
2019-09-22 08:57:28 +00:00
ExtXPlaylistType::Event.required_version(),
2019-09-15 08:40:45 +00:00
ProtocolVersion::V1
);
2019-09-06 10:55:00 +00:00
}
}