1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 13:39:27 +00:00
hls_m3u8/src/tags/media_playlist/playlist_type.rs

114 lines
3 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
2019-10-05 10:49:08 +00:00
/// # [4.3.3.5. EXT-X-PLAYLIST-TYPE]
2019-09-06 10:55:00 +00:00
///
2019-10-03 14:23:27 +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-10-05 10:49:08 +00:00
/// [`Media Playlist`]: crate::MediaPlaylist
/// [4.3.3.5. EXT-X-PLAYLIST-TYPE]: https://tools.ietf.org/html/rfc8216#section-4.3.3.5
2019-10-12 09:38:28 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
2019-09-15 08:40:45 +00:00
pub enum ExtXPlaylistType {
2019-10-05 10:49:08 +00:00
/// If the [`ExtXPlaylistType`] is Event, [`Media Segment`]s
/// can only be added to the end of the [`Media Playlist`].
///
/// [`Media Segment`]: crate::MediaSegment
/// [`Media Playlist`]: crate::MediaPlaylist
2019-09-15 08:40:45 +00:00
Event,
2019-10-03 14:23:27 +00:00
/// If the [`ExtXPlaylistType`] is Video On Demand (Vod),
2019-10-05 10:49:08 +00:00
/// the [`Media Playlist`] cannot change.
///
/// [`Media Playlist`]: crate::MediaPlaylist
2019-09-15 08:40:45 +00:00
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-10-05 10:49:08 +00:00
/// This tag requires [`ProtocolVersion::V1`].
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXPlaylistType {
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 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::*;
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::<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());
2019-10-05 10:49:08 +00:00
assert!("garbage".parse::<ExtXPlaylistType>().is_err());
2019-09-15 08:40:45 +00:00
}
#[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
}
}