2020-04-22 07:54:48 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-09-06 10:55:00 +00:00
|
|
|
use std::fmt;
|
2019-09-10 09:05:20 +00:00
|
|
|
|
2019-10-04 09:02:21 +00:00
|
|
|
use crate::types::ProtocolVersion;
|
2019-09-10 09:05:20 +00:00
|
|
|
use crate::utils::tag;
|
2020-03-25 12:21:11 +00:00
|
|
|
use crate::{Error, RequiredVersion};
|
2019-09-06 10:55:00 +00:00
|
|
|
|
2020-03-25 11:27:36 +00:00
|
|
|
/// Allows synchronization between different renditions of the same
|
|
|
|
/// [`VariantStream`].
|
2019-09-22 16:00:38 +00:00
|
|
|
///
|
2020-02-14 12:05:18 +00:00
|
|
|
/// [`VariantStream`]: crate::tags::VariantStream
|
2020-03-25 12:21:11 +00:00
|
|
|
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
|
|
|
|
pub(crate) struct ExtXDiscontinuitySequence(pub usize);
|
2020-02-10 12:21:48 +00:00
|
|
|
|
|
|
|
impl ExtXDiscontinuitySequence {
|
|
|
|
pub(crate) const PREFIX: &'static str = "#EXT-X-DISCONTINUITY-SEQUENCE:";
|
2019-09-22 08:57:28 +00:00
|
|
|
}
|
2019-09-06 10:55:00 +00:00
|
|
|
|
2020-02-02 12:38:11 +00:00
|
|
|
/// This tag requires [`ProtocolVersion::V1`].
|
2019-09-22 08:57:28 +00:00
|
|
|
impl RequiredVersion for ExtXDiscontinuitySequence {
|
2019-10-03 15:01:15 +00:00
|
|
|
fn required_version(&self) -> ProtocolVersion { ProtocolVersion::V1 }
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ExtXDiscontinuitySequence {
|
2020-04-09 06:43:13 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-02-06 11:28:54 +00:00
|
|
|
//
|
2020-03-25 12:21:11 +00:00
|
|
|
write!(f, "{}{}", Self::PREFIX, self.0)
|
2020-02-06 11:28:54 +00:00
|
|
|
}
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 07:54:48 +00:00
|
|
|
impl TryFrom<&str> for ExtXDiscontinuitySequence {
|
|
|
|
type Error = Error;
|
2019-09-08 10:23:33 +00:00
|
|
|
|
2020-04-22 07:54:48 +00:00
|
|
|
fn try_from(input: &str) -> Result<Self, Self::Error> {
|
2020-02-14 12:01:42 +00:00
|
|
|
let input = tag(input, Self::PREFIX)?;
|
|
|
|
let seq_num = input.parse().map_err(|e| Error::parse_int(input, e))?;
|
|
|
|
|
2020-03-25 12:21:11 +00:00
|
|
|
Ok(Self(seq_num))
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2019-10-08 13:42:33 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2019-09-06 10:55:00 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-09-22 08:57:28 +00:00
|
|
|
fn test_display() {
|
|
|
|
assert_eq!(
|
2020-03-25 12:21:11 +00:00
|
|
|
ExtXDiscontinuitySequence(123).to_string(),
|
2019-09-22 08:57:28 +00:00
|
|
|
"#EXT-X-DISCONTINUITY-SEQUENCE:123".to_string()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_required_version() {
|
|
|
|
assert_eq!(
|
2020-03-25 12:21:11 +00:00
|
|
|
ExtXDiscontinuitySequence(123).required_version(),
|
2019-09-22 08:57:28 +00:00
|
|
|
ProtocolVersion::V1
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parser() {
|
|
|
|
assert_eq!(
|
2020-03-25 12:21:11 +00:00
|
|
|
ExtXDiscontinuitySequence(123),
|
2020-04-22 07:54:48 +00:00
|
|
|
ExtXDiscontinuitySequence::try_from("#EXT-X-DISCONTINUITY-SEQUENCE:123").unwrap()
|
2019-09-22 08:57:28 +00:00
|
|
|
);
|
2020-02-14 12:01:42 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-04-22 07:54:48 +00:00
|
|
|
ExtXDiscontinuitySequence::try_from("#EXT-X-DISCONTINUITY-SEQUENCE:12A"),
|
2020-02-14 12:01:42 +00:00
|
|
|
Err(Error::parse_int("12A", "12A".parse::<u64>().expect_err("")))
|
|
|
|
);
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|