From e187c9dc7cc96a2937d1059bc88b5da97c09df34 Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Sat, 28 Mar 2020 10:46:07 +0100 Subject: [PATCH] improve readability --- src/master_playlist.rs | 24 +++------- src/media_playlist.rs | 104 +++++++++-------------------------------- src/utils.rs | 39 ++++++++++++++++ 3 files changed, 69 insertions(+), 98 deletions(-) diff --git a/src/master_playlist.rs b/src/master_playlist.rs index 9ba81b0..9ea96e0 100644 --- a/src/master_playlist.rs +++ b/src/master_playlist.rs @@ -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, - #[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>) // 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, diff --git a/src/media_playlist.rs b/src/media_playlist.rs index 86a585e..e4c9fc1 100644 --- a/src/media_playlist.rs +++ b/src/media_playlist.rs @@ -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::>(); // 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 ] } diff --git a/src/utils.rs b/src/utils.rs index b210675..b7e628f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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: +/// +/// +/// 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(self, t: T) -> Option; + + #[must_use] + fn athen T>(self, f: F) -> Option; +} + +impl BoolExt for bool { + #[inline] + fn athen_some(self, t: T) -> Option { + if self { + Some(t) + } else { + None + } + } + + #[inline] + fn athen T>(self, f: F) -> Option { + if self { + Some(f()) + } else { + None + } + } +} + macro_rules! required_version { ( $( $tag:expr ),* ) => { ::core::iter::empty()