1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-11-15 20:01:01 +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,
};
use crate::types::{ClosedCaptions, MediaType, ProtocolVersion};
use crate::utils::tag;
use crate::utils::{tag, BoolExt};
use crate::{Error, RequiredVersion};
/// 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)]
#[builder(build_fn(validate = "Self::validate"))]
#[builder(setter(into, strip_option))]
#[non_exhaustive]
pub struct MasterPlaylist {
/// Indicates that all media samples in a [`MediaSegment`] can be
/// decoded without information from other segments.
@ -167,8 +168,6 @@ pub struct MasterPlaylist {
/// This field is optional.
#[builder(default)]
pub unknown_tags: Vec<String>,
#[builder(default, field(private))]
__non_exhaustive: (),
}
impl MasterPlaylist {
@ -277,13 +276,8 @@ impl MasterPlaylist {
impl RequiredVersion for MasterPlaylist {
fn required_version(&self) -> ProtocolVersion {
required_version![
{
if self.has_independent_segments {
Some(ExtXIndependentSegments)
} else {
None
}
},
self.has_independent_segments
.athen_some(ExtXIndependentSegments),
self.start,
self.media,
self.variant_streams,
@ -403,13 +397,9 @@ impl RequiredVersion for MasterPlaylistBuilder {
// not for Option<Option<T>>)
// https://github.com/rust-lang/chalk/issues/12
required_version![
{
if self.has_independent_segments.unwrap_or(false) {
Some(ExtXIndependentSegments)
} else {
None
}
},
self.has_independent_segments
.unwrap_or(false)
.athen_some(ExtXIndependentSegments),
self.start.flatten(),
self.media,
self.variant_streams,

View file

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

View file

@ -1,6 +1,45 @@
use crate::Error;
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 {
( $( $tag:expr ),* ) => {
::core::iter::empty()