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;
|
|
|
|
|
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-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-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
|
|
|
}
|
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!(
|
|
|
|
"#EXT-X-VERSION:6".parse().ok(),
|
|
|
|
Some(ExtXVersion::new(ProtocolVersion::V6))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
}
|