1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-09 16:59:34 +00:00
hls_m3u8/src/tags/media_playlist/target_duration.rs

69 lines
1.7 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
use std::str::FromStr;
use std::time::Duration;
2019-09-13 14:06:52 +00:00
2019-10-04 09:02:21 +00:00
use crate::types::ProtocolVersion;
2019-09-13 14:06:52 +00:00
use crate::utils::tag;
2019-10-04 09:02:21 +00:00
use crate::{Error, RequiredVersion};
2019-09-06 10:55:00 +00:00
2020-03-25 12:37:47 +00:00
/// Specifies the maximum `MediaSegment` duration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
pub(crate) struct ExtXTargetDuration(pub Duration);
2019-09-06 10:55:00 +00:00
impl ExtXTargetDuration {
pub(crate) const PREFIX: &'static str = "#EXT-X-TARGETDURATION:";
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2019-10-03 14:23:27 +00:00
/// This tag requires [`ProtocolVersion::V1`].
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXTargetDuration {
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 ExtXTargetDuration {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-09-22 08:57:28 +00:00
write!(f, "{}{}", Self::PREFIX, self.0.as_secs())
2019-09-06 10:55:00 +00:00
}
}
impl FromStr for ExtXTargetDuration {
type Err = Error;
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
2020-03-25 12:37:47 +00:00
let input = tag(input, Self::PREFIX)?
.parse()
.map_err(|e| Error::parse_int(input, e))?;
2020-02-14 12:01:42 +00:00
2020-03-25 12:37:47 +00:00
Ok(Self(Duration::from_secs(input)))
2019-09-06 10:55:00 +00:00
}
}
#[cfg(test)]
mod test {
use super::*;
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:37:47 +00:00
ExtXTargetDuration(Duration::from_secs(5)).to_string(),
2019-09-22 08:57:28 +00:00
"#EXT-X-TARGETDURATION:5".to_string()
);
}
#[test]
fn test_required_version() {
assert_eq!(
2020-03-25 12:37:47 +00:00
ExtXTargetDuration(Duration::from_secs(5)).required_version(),
2019-09-22 08:57:28 +00:00
ProtocolVersion::V1
);
}
#[test]
fn test_parser() {
assert_eq!(
2020-03-25 12:37:47 +00:00
ExtXTargetDuration(Duration::from_secs(5)),
2019-09-22 08:57:28 +00:00
"#EXT-X-TARGETDURATION:5".parse().unwrap()
);
2019-09-06 10:55:00 +00:00
}
}