1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 12:21:31 +00:00
hls_m3u8/src/tags/basic/version.rs

116 lines
2.8 KiB
Rust
Raw Normal View History

use std::convert::TryFrom;
2018-02-14 03:00:19 +00:00
use std::fmt;
2019-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
2019-09-10 09:05:20 +00:00
use crate::utils::tag;
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-10 09:05:20 +00:00
2020-02-21 20:11:51 +00:00
/// The compatibility version of a playlist.
2019-10-05 10:49:08 +00:00
///
2020-02-21 20:11:51 +00:00
/// It applies to the entire [`MasterPlaylist`] or [`MediaPlaylist`].
2019-10-03 14:23:27 +00:00
///
2020-02-14 12:05:18 +00:00
/// [`MediaPlaylist`]: crate::MediaPlaylist
/// [`MasterPlaylist`]: crate::MasterPlaylist
2019-09-22 16:00:38 +00:00
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
2019-09-08 10:49:22 +00:00
pub struct ExtXVersion(ProtocolVersion);
2019-09-06 10:55:00 +00:00
2018-02-14 03:00:19 +00:00
impl ExtXVersion {
pub(crate) const PREFIX: &'static str = "#EXT-X-VERSION:";
2019-10-03 14:23:27 +00:00
/// Makes a new [`ExtXVersion`] tag.
2019-09-22 16:00:38 +00:00
///
/// # Example
2020-02-06 11:28:54 +00:00
///
2019-09-22 16:00:38 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXVersion;
/// use hls_m3u8::types::ProtocolVersion;
///
2019-10-03 14:23:27 +00:00
/// let version = ExtXVersion::new(ProtocolVersion::V2);
2019-09-22 16:00:38 +00:00
/// ```
2020-02-24 15:30:43 +00:00
#[must_use]
2019-10-03 15:01:15 +00:00
pub const fn new(version: ProtocolVersion) -> Self { Self(version) }
2018-02-14 03:00:19 +00:00
2020-02-21 20:11:51 +00:00
/// Returns the underlying [`ProtocolVersion`].
2019-09-22 08:57:28 +00:00
///
/// # Example
2020-02-06 11:28:54 +00:00
///
2019-09-22 08:57:28 +00:00
/// ```
/// # use hls_m3u8::tags::ExtXVersion;
/// use hls_m3u8::types::ProtocolVersion;
///
/// assert_eq!(
/// ExtXVersion::new(ProtocolVersion::V6).version(),
/// ProtocolVersion::V6
/// );
/// ```
2020-02-24 15:30:43 +00:00
#[must_use]
2019-10-03 15:01:15 +00:00
pub const fn version(self) -> ProtocolVersion { self.0 }
2019-09-22 08:57:28 +00:00
}
2018-02-14 03:00:19 +00:00
2019-10-03 14:23:27 +00:00
/// This tag requires [`ProtocolVersion::V1`].
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXVersion {
2019-10-03 15:01:15 +00:00
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
2018-02-14 03:00:19 +00:00
}
2019-09-06 10:55:00 +00:00
2018-02-14 03:00:19 +00:00
impl fmt::Display for ExtXVersion {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2020-02-06 11:28:54 +00:00
//
write!(f, "{}{}", Self::PREFIX, self.0)
}
2018-02-14 03:00:19 +00:00
}
2019-09-06 10:55:00 +00:00
2019-09-14 11:26:16 +00:00
impl Default for ExtXVersion {
2020-02-06 11:28:54 +00:00
fn default() -> Self { Self(ProtocolVersion::default()) }
2019-09-14 11:26:16 +00:00
}
impl From<ProtocolVersion> for ExtXVersion {
2019-10-03 15:01:15 +00:00
fn from(value: ProtocolVersion) -> Self { Self(value) }
2019-09-14 11:26:16 +00:00
}
impl TryFrom<&str> for ExtXVersion {
type Error = Error;
2019-09-08 10:49:22 +00:00
fn try_from(input: &str) -> Result<Self, Self::Error> {
2019-09-10 09:05:20 +00:00
let version = tag(input, Self::PREFIX)?.parse()?;
2019-10-03 14:23:27 +00:00
Ok(Self::new(version))
2018-02-14 03:00:19 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
2018-02-14 03:00:19 +00:00
#[test]
2019-09-08 10:49:22 +00:00
fn test_display() {
assert_eq!(
ExtXVersion::new(ProtocolVersion::V6).to_string(),
"#EXT-X-VERSION:6"
);
}
#[test]
fn test_parser() {
assert_eq!(
ExtXVersion::try_from("#EXT-X-VERSION:6").unwrap(),
2019-09-22 16:00:38 +00:00
ExtXVersion::new(ProtocolVersion::V6)
2019-09-08 10:49:22 +00:00
);
}
#[test]
2019-09-22 08:57:28 +00:00
fn test_required_version() {
2019-09-08 10:49:22 +00:00
assert_eq!(
2019-09-22 08:57:28 +00:00
ExtXVersion::new(ProtocolVersion::V6).required_version(),
2019-09-08 10:49:22 +00:00
ProtocolVersion::V1
);
}
2019-10-03 14:23:27 +00:00
#[test]
fn test_default_and_from() {
assert_eq!(
ExtXVersion::default(),
ExtXVersion::from(ProtocolVersion::V1)
);
}
2018-02-14 03:00:19 +00:00
}