1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-01 07:08:07 +00:00
hls_m3u8/src/tags/basic/version.rs

84 lines
1.9 KiB
Rust
Raw Normal View History

2018-02-14 03:00:19 +00:00
use std::fmt;
use std::str::FromStr;
2019-09-10 09:05:20 +00:00
use crate::types::ProtocolVersion;
use crate::utils::tag;
use crate::Error;
2018-02-14 03:00:19 +00:00
/// [4.3.1.2. EXT-X-VERSION]
///
/// [4.3.1.2. EXT-X-VERSION]: https://tools.ietf.org/html/rfc8216#section-4.3.1.2
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
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:";
2018-02-14 03:28:50 +00:00
/// Makes a new `ExtXVersion` tag.
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
}
/// Returns the protocol compatibility version of the playlist containing this tag.
2019-09-08 10:23:33 +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
}
/// Returns the protocol compatibility version that this tag requires.
2019-09-08 10:23:33 +00:00
pub const fn requires_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
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!(
"#EXT-X-VERSION:6".parse().ok(),
Some(ExtXVersion::new(ProtocolVersion::V6))
);
}
#[test]
fn test_requires_version() {
assert_eq!(
ExtXVersion::new(ProtocolVersion::V6).requires_version(),
ProtocolVersion::V1
);
}
#[test]
fn test_version() {
assert_eq!(
ExtXVersion::new(ProtocolVersion::V6).version(),
ProtocolVersion::V6
);
2018-02-14 03:00:19 +00:00
}
}