1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-16 20:10:33 +00:00

improve readability

This commit is contained in:
Luro02 2020-03-28 10:46:07 +01:00
parent 20072c2695
commit e187c9dc7c
No known key found for this signature in database
GPG key ID: B66FD4F74501A9CF
3 changed files with 69 additions and 98 deletions

View file

@ -10,7 +10,7 @@ use crate::tags::{
ExtXVersion, VariantStream, ExtXVersion, VariantStream,
}; };
use crate::types::{ClosedCaptions, MediaType, ProtocolVersion}; use crate::types::{ClosedCaptions, MediaType, ProtocolVersion};
use crate::utils::tag; use crate::utils::{tag, BoolExt};
use crate::{Error, RequiredVersion}; use crate::{Error, RequiredVersion};
/// The master playlist describes all of the available variants for your /// The master playlist describes all of the available variants for your
@ -97,6 +97,7 @@ use crate::{Error, RequiredVersion};
#[derive(Builder, Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Builder, Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[builder(build_fn(validate = "Self::validate"))] #[builder(build_fn(validate = "Self::validate"))]
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option))]
#[non_exhaustive]
pub struct MasterPlaylist { pub struct MasterPlaylist {
/// Indicates that all media samples in a [`MediaSegment`] can be /// Indicates that all media samples in a [`MediaSegment`] can be
/// decoded without information from other segments. /// decoded without information from other segments.
@ -167,8 +168,6 @@ pub struct MasterPlaylist {
/// This field is optional. /// This field is optional.
#[builder(default)] #[builder(default)]
pub unknown_tags: Vec<String>, pub unknown_tags: Vec<String>,
#[builder(default, field(private))]
__non_exhaustive: (),
} }
impl MasterPlaylist { impl MasterPlaylist {
@ -277,13 +276,8 @@ impl MasterPlaylist {
impl RequiredVersion for MasterPlaylist { impl RequiredVersion for MasterPlaylist {
fn required_version(&self) -> ProtocolVersion { fn required_version(&self) -> ProtocolVersion {
required_version![ required_version![
{ self.has_independent_segments
if self.has_independent_segments { .athen_some(ExtXIndependentSegments),
Some(ExtXIndependentSegments)
} else {
None
}
},
self.start, self.start,
self.media, self.media,
self.variant_streams, self.variant_streams,
@ -403,13 +397,9 @@ impl RequiredVersion for MasterPlaylistBuilder {
// not for Option<Option<T>>) // not for Option<Option<T>>)
// https://github.com/rust-lang/chalk/issues/12 // https://github.com/rust-lang/chalk/issues/12
required_version![ required_version![
{ self.has_independent_segments
if self.has_independent_segments.unwrap_or(false) { .unwrap_or(false)
Some(ExtXIndependentSegments) .athen_some(ExtXIndependentSegments),
} else {
None
}
},
self.start.flatten(), self.start.flatten(),
self.media, self.media,
self.variant_streams, self.variant_streams,

View file

@ -11,8 +11,10 @@ use crate::tags::{
ExtM3u, ExtXDiscontinuitySequence, ExtXEndList, ExtXIFramesOnly, ExtXIndependentSegments, ExtM3u, ExtXDiscontinuitySequence, ExtXEndList, ExtXIFramesOnly, ExtXIndependentSegments,
ExtXKey, ExtXMediaSequence, ExtXStart, ExtXTargetDuration, ExtXVersion, ExtXKey, ExtXMediaSequence, ExtXStart, ExtXTargetDuration, ExtXVersion,
}; };
use crate::types::{EncryptionMethod, PlaylistType, ProtocolVersion}; use crate::types::{
use crate::utils::tag; DecryptionKey, EncryptionMethod, InitializationVector, KeyFormat, PlaylistType, ProtocolVersion,
};
use crate::utils::{tag, BoolExt};
use crate::{Error, RequiredVersion}; use crate::{Error, RequiredVersion};
/// Media playlist. /// Media playlist.
@ -274,13 +276,7 @@ impl MediaPlaylistBuilder {
// insert all explictly numbered segments into the result // insert all explictly numbered segments into the result
let mut result_segments = segments let mut result_segments = segments
.iter() .iter()
.filter_map(|(_, s)| { .filter_map(|(_, s)| s.explicit_number.athen(|| (s.number, s.clone())))
if s.explicit_number {
Some((s.number, s.clone()))
} else {
None
}
})
.collect::<BTreeMap<_, _>>(); .collect::<BTreeMap<_, _>>();
// no segment should exist before the sequence_number // no segment should exist before the sequence_number
@ -345,45 +341,19 @@ impl RequiredVersion for MediaPlaylistBuilder {
fn required_version(&self) -> ProtocolVersion { fn required_version(&self) -> ProtocolVersion {
required_version![ required_version![
self.target_duration.map(ExtXTargetDuration), self.target_duration.map(ExtXTargetDuration),
{ (self.media_sequence.unwrap_or(0) != 0)
if self.media_sequence.unwrap_or(0) != 0 { .athen(|| ExtXMediaSequence(self.media_sequence.unwrap_or(0))),
Some(ExtXMediaSequence(self.media_sequence.unwrap_or(0))) (self.discontinuity_sequence.unwrap_or(0) != 0)
} else { .athen(|| ExtXDiscontinuitySequence(self.discontinuity_sequence.unwrap_or(0))),
None
}
},
{
if self.discontinuity_sequence.unwrap_or(0) != 0 {
Some(ExtXDiscontinuitySequence(
self.discontinuity_sequence.unwrap_or(0),
))
} else {
None
}
},
self.playlist_type, self.playlist_type,
{ self.has_i_frames_only
if self.has_i_frames_only.unwrap_or(false) { .unwrap_or(false)
Some(ExtXIFramesOnly) .athen_some(ExtXIFramesOnly),
} else { self.has_independent_segments
None .unwrap_or(false)
} .athen_some(ExtXIndependentSegments),
},
{
if self.has_independent_segments.unwrap_or(false) {
Some(ExtXIndependentSegments)
} else {
None
}
},
self.start, self.start,
{ self.has_end_list.unwrap_or(false).athen_some(ExtXEndList),
if self.has_end_list.unwrap_or(false) {
Some(ExtXEndList)
} else {
None
}
},
self.segments self.segments
] ]
} }
@ -406,43 +376,15 @@ impl RequiredVersion for MediaPlaylist {
fn required_version(&self) -> ProtocolVersion { fn required_version(&self) -> ProtocolVersion {
required_version![ required_version![
ExtXTargetDuration(self.target_duration), ExtXTargetDuration(self.target_duration),
{ (self.media_sequence != 0).athen(|| ExtXMediaSequence(self.media_sequence)),
if self.media_sequence != 0 { (self.discontinuity_sequence != 0)
Some(ExtXMediaSequence(self.media_sequence)) .athen(|| ExtXDiscontinuitySequence(self.discontinuity_sequence)),
} else {
None
}
},
{
if self.discontinuity_sequence != 0 {
Some(ExtXDiscontinuitySequence(self.discontinuity_sequence))
} else {
None
}
},
self.playlist_type, self.playlist_type,
{ self.has_i_frames_only.athen_some(ExtXIFramesOnly),
if self.has_i_frames_only { self.has_independent_segments
Some(ExtXIFramesOnly) .athen_some(ExtXIndependentSegments),
} else {
None
}
},
{
if self.has_independent_segments {
Some(ExtXIndependentSegments)
} else {
None
}
},
self.start, self.start,
{ self.has_end_list.athen_some(ExtXEndList),
if self.has_end_list {
Some(ExtXEndList)
} else {
None
}
},
self.segments self.segments
] ]
} }

View file

@ -1,6 +1,45 @@
use crate::Error; use crate::Error;
use core::iter; use core::iter;
/// This is an extension trait that adds the below method to `bool`.
/// Those methods are already planned for the standard library, but are not
/// stable at the time of writing this comment.
///
/// The current status can be seen here:
/// <https://github.com/rust-lang/rust/issues/64260>
///
/// This trait exists to allow publishing a new version (requires stable
/// release) and the functions are prefixed with an `a` to prevent naming
/// conflicts with the coming std functions.
// TODO: replace this trait with std version as soon as it is stabilized
pub(crate) trait BoolExt {
#[must_use]
fn athen_some<T>(self, t: T) -> Option<T>;
#[must_use]
fn athen<T, F: FnOnce() -> T>(self, f: F) -> Option<T>;
}
impl BoolExt for bool {
#[inline]
fn athen_some<T>(self, t: T) -> Option<T> {
if self {
Some(t)
} else {
None
}
}
#[inline]
fn athen<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
if self {
Some(f())
} else {
None
}
}
}
macro_rules! required_version { macro_rules! required_version {
( $( $tag:expr ),* ) => { ( $( $tag:expr ),* ) => {
::core::iter::empty() ::core::iter::empty()