1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 21:49:35 +00:00
hls_m3u8/src/types/protocol_version.rs

114 lines
3.1 KiB
Rust
Raw Permalink Normal View History

2019-09-06 11:20:40 +00:00
use std::fmt;
2019-09-15 17:09:48 +00:00
use std::str::FromStr;
2019-09-06 11:20:40 +00:00
2019-09-13 14:06:52 +00:00
use crate::Error;
/// The [`ProtocolVersion`] specifies which `m3u8` revision is required, to
/// parse a certain tag correctly.
2020-02-10 12:21:48 +00:00
#[non_exhaustive]
2019-09-06 11:20:40 +00:00
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ProtocolVersion {
V1,
V2,
V3,
V4,
V5,
V6,
V7,
}
2019-09-17 13:40:10 +00:00
impl ProtocolVersion {
/// Returns the latest [`ProtocolVersion`] that is supported by
2019-10-03 14:23:27 +00:00
/// this library.
2019-09-22 18:33:40 +00:00
///
/// # Example
2020-02-02 12:38:11 +00:00
///
2019-09-22 18:33:40 +00:00
/// ```
/// # use hls_m3u8::types::ProtocolVersion;
/// assert_eq!(ProtocolVersion::latest(), ProtocolVersion::V7);
/// ```
2020-02-24 15:30:43 +00:00
#[must_use]
#[inline]
2019-10-03 15:01:15 +00:00
pub const fn latest() -> Self { Self::V7 }
2019-09-17 13:40:10 +00:00
}
2019-09-06 11:20:40 +00:00
impl fmt::Display for ProtocolVersion {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-09-22 18:33:40 +00:00
match &self {
Self::V1 => write!(f, "1"),
Self::V2 => write!(f, "2"),
Self::V3 => write!(f, "3"),
Self::V4 => write!(f, "4"),
Self::V5 => write!(f, "5"),
Self::V6 => write!(f, "6"),
Self::V7 => write!(f, "7"),
}
2019-09-06 11:20:40 +00:00
}
}
2019-09-17 13:40:10 +00:00
2019-09-06 11:20:40 +00:00
impl FromStr for ProtocolVersion {
type Err = Error;
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
Ok({
2019-09-22 08:57:28 +00:00
match input.trim() {
"1" => Self::V1,
"2" => Self::V2,
"3" => Self::V3,
"4" => Self::V4,
"5" => Self::V5,
"6" => Self::V6,
"7" => Self::V7,
_ => return Err(Error::unknown_protocol_version(input)),
}
2019-09-06 11:20:40 +00:00
})
}
}
2019-09-22 08:57:28 +00:00
2020-02-02 12:38:11 +00:00
/// The default is [`ProtocolVersion::V1`].
2019-09-22 08:57:28 +00:00
impl Default for ProtocolVersion {
2019-10-03 15:01:15 +00:00
fn default() -> Self { Self::V1 }
2019-09-22 08:57:28 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
2019-09-22 08:57:28 +00:00
#[test]
fn test_display() {
assert_eq!(ProtocolVersion::V1.to_string(), "1".to_string());
assert_eq!(ProtocolVersion::V2.to_string(), "2".to_string());
assert_eq!(ProtocolVersion::V3.to_string(), "3".to_string());
assert_eq!(ProtocolVersion::V4.to_string(), "4".to_string());
assert_eq!(ProtocolVersion::V5.to_string(), "5".to_string());
assert_eq!(ProtocolVersion::V6.to_string(), "6".to_string());
assert_eq!(ProtocolVersion::V7.to_string(), "7".to_string());
}
#[test]
fn test_parser() {
assert_eq!(ProtocolVersion::V1, "1".parse().unwrap());
assert_eq!(ProtocolVersion::V2, "2".parse().unwrap());
assert_eq!(ProtocolVersion::V3, "3".parse().unwrap());
assert_eq!(ProtocolVersion::V4, "4".parse().unwrap());
assert_eq!(ProtocolVersion::V5, "5".parse().unwrap());
assert_eq!(ProtocolVersion::V6, "6".parse().unwrap());
assert_eq!(ProtocolVersion::V7, "7".parse().unwrap());
2019-09-22 18:33:40 +00:00
assert_eq!(ProtocolVersion::V7, " 7 ".parse().unwrap());
assert!("garbage".parse::<ProtocolVersion>().is_err());
}
#[test]
fn test_default() {
assert_eq!(ProtocolVersion::default(), ProtocolVersion::V1);
}
#[test]
fn test_latest() {
assert_eq!(ProtocolVersion::latest(), ProtocolVersion::V7);
2019-09-22 08:57:28 +00:00
}
}