1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-18 12:50:31 +00:00
hls_m3u8/src/tags/media_playlist/playlist_type.rs

114 lines
2.9 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-15 08:40:45 +00:00
/// [4.3.3.5. EXT-X-PLAYLIST-TYPE](https://tools.ietf.org/html/rfc8216#section-4.3.3.5)
2019-09-06 10:55:00 +00:00
///
2019-09-15 08:40:45 +00:00
/// The EXT-X-PLAYLIST-TYPE tag provides mutability information about the
/// Media Playlist. It applies to the entire Media Playlist.
/// It is OPTIONAL. Its format is:
///
/// ```text
/// #EXT-X-PLAYLIST-TYPE:<type-enum>
/// ```
///
/// # Note
/// If the EXT-X-PLAYLIST-TYPE tag is omitted from a Media Playlist, the
/// Playlist can be updated according to the rules in Section 6.2.1 with
/// no additional restrictions.
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 {
/// If the ExtXPlaylistType is Event, Media Segments can only be added to
/// the end of the Media Playlist.
Event,
/// If the ExtXPlaylistType is Video On Demand (Vod),
/// 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
}
}