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/media_playlist/discontinuity_sequence.rs

77 lines
1.9 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
use std::str::FromStr;
2019-09-10 09:05:20 +00:00
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;
2019-09-06 10:55:00 +00:00
/// [4.3.3.3. EXT-X-DISCONTINUITY-SEQUENCE]
///
/// [4.3.3.3. EXT-X-DISCONTINUITY-SEQUENCE]: https://tools.ietf.org/html/rfc8216#section-4.3.3.3
2019-09-22 08:57:28 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ExtXDiscontinuitySequence(u64);
2019-09-06 10:55:00 +00:00
impl ExtXDiscontinuitySequence {
pub(crate) const PREFIX: &'static str = "#EXT-X-DISCONTINUITY-SEQUENCE:";
/// Makes a new `ExtXDiscontinuitySequence` tag.
2019-09-08 10:23:33 +00:00
pub const fn new(seq_num: u64) -> Self {
2019-09-22 08:57:28 +00:00
Self(seq_num)
2019-09-06 10:55:00 +00:00
}
/// Returns the discontinuity sequence number of
/// the first media segment that appears in the associated playlist.
2019-09-22 08:57:28 +00:00
pub const fn seq_num(&self) -> u64 {
self.0
2019-09-06 10:55:00 +00:00
}
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXDiscontinuitySequence {
fn required_version(&self) -> ProtocolVersion {
2019-09-06 10:55:00 +00:00
ProtocolVersion::V1
}
}
impl fmt::Display for ExtXDiscontinuitySequence {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2019-09-22 08:57:28 +00:00
write!(f, "{}{}", Self::PREFIX, self.0)
2019-09-06 10:55:00 +00:00
}
}
impl FromStr for ExtXDiscontinuitySequence {
2019-09-10 09:05:20 +00:00
type Err = crate::Error;
2019-09-08 10:23:33 +00:00
2019-09-10 09:05:20 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
2019-09-22 08:57:28 +00:00
let seq_num = tag(input, Self::PREFIX)?.parse()?;
2019-09-10 09:05:20 +00:00
Ok(Self::new(seq_num))
2019-09-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
2019-09-22 08:57:28 +00:00
fn test_display() {
assert_eq!(
ExtXDiscontinuitySequence::new(123).to_string(),
"#EXT-X-DISCONTINUITY-SEQUENCE:123".to_string()
);
}
#[test]
fn test_required_version() {
assert_eq!(
ExtXDiscontinuitySequence::new(123).required_version(),
ProtocolVersion::V1
)
}
#[test]
fn test_parser() {
assert_eq!(
ExtXDiscontinuitySequence::new(123),
"#EXT-X-DISCONTINUITY-SEQUENCE:123".parse().unwrap()
);
2019-09-06 10:55:00 +00:00
}
}