1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-17 07:52:58 +00:00
hls_m3u8/src/tags/basic/version.rs

119 lines
2.8 KiB
Rust
Raw Normal View History

2018-02-14 03:00:19 +00:00
use std::fmt;
use std::str::FromStr;
2019-09-22 08:57:28 +00:00
use crate::types::{ProtocolVersion, RequiredVersion};
2019-09-10 09:05:20 +00:00
use crate::utils::tag;
use crate::Error;
2019-09-22 16:00:38 +00:00
/// # [4.3.1.2. EXT-X-VERSION]
/// The [ExtXVersion] tag indicates the compatibility version of the
/// Playlist file, its associated media, and its server.
///
/// The [ExtXVersion] tag applies to the entire Playlist file. Its
/// format is:
///
/// ```text
/// #EXT-X-VERSION:<n>
/// ```
/// where `n` is an integer indicating the protocol compatibility version
/// number.
2018-02-14 03:00:19 +00:00
///
/// [4.3.1.2. EXT-X-VERSION]: https://tools.ietf.org/html/rfc8216#section-4.3.1.2
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-09-22 16:00:38 +00:00
/// Makes a new [ExtXVersion] tag.
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXVersion;
/// use hls_m3u8::types::ProtocolVersion;
///
/// let version_tag = ExtXVersion::new(ProtocolVersion::V2);
/// ```
2019-09-08 10:23:33 +00:00
pub const fn new(version: ProtocolVersion) -> Self {
2019-09-08 10:49:22 +00:00
Self(version)
2018-02-14 03:00:19 +00:00
}
2019-09-22 16:00:38 +00:00
/// Returns the protocol compatibility version of the playlist, containing this tag.
2019-09-22 08:57:28 +00:00
///
/// # Example
/// ```
/// # use hls_m3u8::tags::ExtXVersion;
/// use hls_m3u8::types::ProtocolVersion;
///
/// assert_eq!(
/// ExtXVersion::new(ProtocolVersion::V6).version(),
/// ProtocolVersion::V6
/// );
/// ```
2019-09-22 18:33:40 +00:00
pub const fn version(self) -> ProtocolVersion {
2019-09-08 10:49:22 +00:00
self.0
2018-02-14 03:00:19 +00:00
}
2019-09-22 08:57:28 +00:00
}
2018-02-14 03:00:19 +00:00
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXVersion {
fn required_version(&self) -> ProtocolVersion {
2018-02-14 03:00:19 +00:00
ProtocolVersion::V1
}
}
2019-09-06 10:55:00 +00:00
2018-02-14 03:00:19 +00:00
impl fmt::Display for ExtXVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-08 10:49:22 +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 {
fn default() -> Self {
Self(ProtocolVersion::V1)
}
}
impl From<ProtocolVersion> for ExtXVersion {
fn from(value: ProtocolVersion) -> Self {
Self(value)
}
}
2018-02-14 03:00:19 +00:00
impl FromStr for ExtXVersion {
type Err = Error;
2019-09-08 10:49:22 +00:00
2019-09-10 09:05:20 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
let version = tag(input, Self::PREFIX)?.parse()?;
2019-09-08 10:49:22 +00:00
Ok(ExtXVersion::new(version))
2018-02-14 03:00:19 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
#[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!(
2019-09-22 16:00:38 +00:00
"#EXT-X-VERSION:6".parse::<ExtXVersion>().unwrap(),
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
);
}
2018-02-14 03:00:19 +00:00
}