From 429f3f8c3d3d24433c77b003857499e9fa9ed344 Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Wed, 25 Mar 2020 13:37:47 +0100 Subject: [PATCH] internalize ExtXTargetDuration --- src/tags/media_playlist/mod.rs | 14 ++--- src/tags/media_playlist/target_duration.rs | 69 ++++------------------ src/tags/mod.rs | 2 +- tests/media_playlist.rs | 6 +- tests/rfc8216.rs | 8 +-- 5 files changed, 24 insertions(+), 75 deletions(-) diff --git a/src/tags/media_playlist/mod.rs b/src/tags/media_playlist/mod.rs index f896a93..bb4e53a 100644 --- a/src/tags/media_playlist/mod.rs +++ b/src/tags/media_playlist/mod.rs @@ -1,13 +1,11 @@ -mod discontinuity_sequence; -mod end_list; -mod i_frames_only; -mod media_sequence; -mod playlist_type; -mod target_duration; +pub(crate) mod discontinuity_sequence; +pub(crate) mod end_list; +pub(crate) mod i_frames_only; +pub(crate) mod media_sequence; +pub(crate) mod target_duration; pub(crate) use discontinuity_sequence::*; pub(crate) use end_list::*; pub(crate) use i_frames_only::*; -pub use playlist_type::*; pub(crate) use media_sequence::*; -pub use target_duration::*; +pub(crate) use target_duration::*; diff --git a/src/tags/media_playlist/target_duration.rs b/src/tags/media_playlist/target_duration.rs index 9ef6e3c..4d97561 100644 --- a/src/tags/media_playlist/target_duration.rs +++ b/src/tags/media_playlist/target_duration.rs @@ -2,57 +2,16 @@ use std::fmt; use std::str::FromStr; use std::time::Duration; -use derive_more::Deref; - use crate::types::ProtocolVersion; use crate::utils::tag; use crate::{Error, RequiredVersion}; -/// # [4.3.3.1. EXT-X-TARGETDURATION] -/// -/// The [`ExtXTargetDuration`] tag specifies the maximum [`MediaSegment`] -/// duration. -/// -/// [`MediaSegment`]: crate::MediaSegment -/// [4.3.3.1. EXT-X-TARGETDURATION]: -/// https://tools.ietf.org/html/rfc8216#section-4.3.3.1 -#[derive(Deref, Debug, Clone, Copy, PartialEq, Eq, Hash, Default, PartialOrd, Ord)] -pub struct ExtXTargetDuration(Duration); +/// Specifies the maximum `MediaSegment` duration. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, PartialOrd, Ord)] +pub(crate) struct ExtXTargetDuration(pub Duration); impl ExtXTargetDuration { pub(crate) const PREFIX: &'static str = "#EXT-X-TARGETDURATION:"; - - /// Makes a new [`ExtXTargetDuration`] tag. - /// - /// # Example - /// - /// ``` - /// # use hls_m3u8::tags::ExtXTargetDuration; - /// use std::time::Duration; - /// - /// let target_duration = ExtXTargetDuration::new(Duration::from_secs(20)); - /// ``` - /// - /// # Note - /// - /// The nanoseconds part of the [`Duration`] will be discarded. - #[must_use] - pub const fn new(duration: Duration) -> Self { Self(Duration::from_secs(duration.as_secs())) } - - /// Returns the maximum media segment duration. - /// - /// # Example - /// - /// ``` - /// # use hls_m3u8::tags::ExtXTargetDuration; - /// use std::time::Duration; - /// - /// let target_duration = ExtXTargetDuration::new(Duration::from_nanos(2_000_000_000)); - /// - /// assert_eq!(target_duration.duration(), Duration::from_secs(2)); - /// ``` - #[must_use] - pub const fn duration(&self) -> Duration { self.0 } } /// This tag requires [`ProtocolVersion::V1`]. @@ -70,17 +29,14 @@ impl FromStr for ExtXTargetDuration { type Err = Error; fn from_str(input: &str) -> Result { - let input = tag(input, Self::PREFIX)?; - let input = input.parse().map_err(|e| Error::parse_int(input, e))?; + let input = tag(input, Self::PREFIX)? + .parse() + .map_err(|e| Error::parse_int(input, e))?; - Ok(Self::new(Duration::from_secs(input))) + Ok(Self(Duration::from_secs(input))) } } -impl From for ExtXTargetDuration { - fn from(value: Duration) -> Self { Self::new(value) } -} - #[cfg(test)] mod test { use super::*; @@ -89,7 +45,7 @@ mod test { #[test] fn test_display() { assert_eq!( - ExtXTargetDuration::new(Duration::from_secs(5)).to_string(), + ExtXTargetDuration(Duration::from_secs(5)).to_string(), "#EXT-X-TARGETDURATION:5".to_string() ); } @@ -97,7 +53,7 @@ mod test { #[test] fn test_required_version() { assert_eq!( - ExtXTargetDuration::new(Duration::from_secs(5)).required_version(), + ExtXTargetDuration(Duration::from_secs(5)).required_version(), ProtocolVersion::V1 ); } @@ -105,13 +61,8 @@ mod test { #[test] fn test_parser() { assert_eq!( - ExtXTargetDuration::new(Duration::from_secs(5)), + ExtXTargetDuration(Duration::from_secs(5)), "#EXT-X-TARGETDURATION:5".parse().unwrap() ); } - - #[test] - fn test_deref() { - assert_eq!(ExtXTargetDuration::new(Duration::from_secs(5)).as_secs(), 5); - } } diff --git a/src/tags/mod.rs b/src/tags/mod.rs index 35ae4c4..ae45d74 100644 --- a/src/tags/mod.rs +++ b/src/tags/mod.rs @@ -10,6 +10,6 @@ mod shared; pub use basic::*; pub use master_playlist::*; -pub use media_playlist::*; +pub(crate) use media_playlist::*; pub use media_segment::*; pub use shared::*; diff --git a/tests/media_playlist.rs b/tests/media_playlist.rs index b359a10..ae8a955 100644 --- a/tests/media_playlist.rs +++ b/tests/media_playlist.rs @@ -5,7 +5,7 @@ use std::time::Duration; -use hls_m3u8::tags::{ExtInf, ExtXByteRange, ExtXTargetDuration}; +use hls_m3u8::tags::{ExtInf, ExtXByteRange}; use hls_m3u8::types::PlaylistType; use hls_m3u8::{MediaPlaylist, MediaSegment}; use pretty_assertions::assert_eq; @@ -27,7 +27,7 @@ generate_tests! { test_media_playlist_with_byterange => { MediaPlaylist::builder() .media_sequence(1) - .target_duration(ExtXTargetDuration::new(Duration::from_secs(10))) + .target_duration(Duration::from_secs(10)) .segments(vec![ MediaSegment::builder() .inf(ExtInf::new(Duration::from_secs_f64(10.0))) @@ -72,7 +72,7 @@ generate_tests! { test_absolute_uris => { MediaPlaylist::builder() .playlist_type(PlaylistType::Vod) - .target_duration(ExtXTargetDuration::new(Duration::from_secs(10))) + .target_duration(Duration::from_secs(10)) .segments(vec![ MediaSegment::builder() .inf(ExtInf::new(Duration::from_secs(10))) diff --git a/tests/rfc8216.rs b/tests/rfc8216.rs index 41a19bd..174c5a8 100644 --- a/tests/rfc8216.rs +++ b/tests/rfc8216.rs @@ -1,7 +1,7 @@ // https://tools.ietf.org/html/rfc8216#section-8 use std::time::Duration; -use hls_m3u8::tags::{ExtInf, ExtXKey, ExtXMedia, ExtXTargetDuration, VariantStream}; +use hls_m3u8::tags::{ExtInf, ExtXKey, ExtXMedia, VariantStream}; use hls_m3u8::types::{DecryptionKey, EncryptionMethod, MediaType, StreamData}; use hls_m3u8::{MasterPlaylist, MediaPlaylist, MediaSegment}; use pretty_assertions::assert_eq; @@ -22,7 +22,7 @@ macro_rules! generate_tests { generate_tests! { test_simple_playlist => { MediaPlaylist::builder() - .target_duration(ExtXTargetDuration::new(Duration::from_secs(10))) + .target_duration(Duration::from_secs(10)) .segments(vec![ MediaSegment::builder() .inf(ExtInf::new(Duration::from_secs_f64(9.009))) @@ -58,7 +58,7 @@ generate_tests! { }, test_live_media_playlist_using_https => { MediaPlaylist::builder() - .target_duration(ExtXTargetDuration::new(Duration::from_secs(8))) + .target_duration(Duration::from_secs(8)) .media_sequence(2680) .segments(vec![ MediaSegment::builder() @@ -94,7 +94,7 @@ generate_tests! { }, test_media_playlist_with_encrypted_segments => { MediaPlaylist::builder() - .target_duration(ExtXTargetDuration::new(Duration::from_secs(15))) + .target_duration(Duration::from_secs(15)) .media_sequence(7794) .segments(vec![ MediaSegment::builder()