From b932cef71a0adb84589d3ed0b5a50a02824f4054 Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Sun, 15 Sep 2019 19:01:56 +0200 Subject: [PATCH] internalize signed_decimal_floating_point #9 --- src/tags/shared/start.rs | 28 ++++++--- src/types/hexadecimal_sequence.rs | 72 ---------------------- src/types/mod.rs | 4 +- src/types/playlist_type.rs | 1 - src/types/protocol_version.rs | 38 +++++++----- src/types/signed_decimal_floating_point.rs | 2 +- 6 files changed, 43 insertions(+), 102 deletions(-) delete mode 100644 src/types/hexadecimal_sequence.rs delete mode 100644 src/types/playlist_type.rs diff --git a/src/tags/shared/start.rs b/src/tags/shared/start.rs index 06b6d90..a4c594a 100644 --- a/src/tags/shared/start.rs +++ b/src/tags/shared/start.rs @@ -19,24 +19,36 @@ impl ExtXStart { pub(crate) const PREFIX: &'static str = "#EXT-X-START:"; /// Makes a new `ExtXStart` tag. - pub const fn new(time_offset: SignedDecimalFloatingPoint) -> Self { + /// # Panic + /// Panics if the time_offset value is infinite. + pub fn new(time_offset: f64) -> Self { + if time_offset.is_infinite() { + panic!("EXT-X-START: Floating point value must be finite!"); + } + ExtXStart { - time_offset, + time_offset: SignedDecimalFloatingPoint::new(time_offset).unwrap(), precise: false, } } /// Makes a new `ExtXStart` tag with the given `precise` flag. - pub const fn with_precise(time_offset: SignedDecimalFloatingPoint, precise: bool) -> Self { + /// # Panic + /// Panics if the time_offset value is infinite. + pub fn with_precise(time_offset: f64, precise: bool) -> Self { + if time_offset.is_infinite() { + panic!("EXT-X-START: Floating point value must be finite!"); + } + ExtXStart { - time_offset, + time_offset: SignedDecimalFloatingPoint::new(time_offset).unwrap(), precise, } } /// Returns the time offset of the media segments in the playlist. - pub const fn time_offset(&self) -> SignedDecimalFloatingPoint { - self.time_offset + pub const fn time_offset(&self) -> f64 { + self.time_offset.as_f64() } /// Returns whether clients should not render media stream whose presentation times are @@ -97,13 +109,13 @@ mod test { #[test] fn ext_x_start() { - let tag = ExtXStart::new(SignedDecimalFloatingPoint::new(-1.23).unwrap()); + let tag = ExtXStart::new(-1.23); let text = "#EXT-X-START:TIME-OFFSET=-1.23"; assert_eq!(text.parse().ok(), Some(tag)); assert_eq!(tag.to_string(), text); assert_eq!(tag.requires_version(), ProtocolVersion::V1); - let tag = ExtXStart::with_precise(SignedDecimalFloatingPoint::new(1.23).unwrap(), true); + let tag = ExtXStart::with_precise(1.23, true); let text = "#EXT-X-START:TIME-OFFSET=1.23,PRECISE=YES"; assert_eq!(text.parse().ok(), Some(tag)); assert_eq!(tag.to_string(), text); diff --git a/src/types/hexadecimal_sequence.rs b/src/types/hexadecimal_sequence.rs deleted file mode 100644 index fd6d69e..0000000 --- a/src/types/hexadecimal_sequence.rs +++ /dev/null @@ -1,72 +0,0 @@ -use std::fmt; -use std::ops::Deref; -use std::str::FromStr; - -use crate::Error; - -/// Hexadecimal sequence. -/// -/// See: [4.2. Attribute Lists] -/// -/// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2 -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct HexadecimalSequence(Vec); - -impl HexadecimalSequence { - /// Makes a new `HexadecimalSequence` instance. - pub fn new>>(v: T) -> Self { - HexadecimalSequence(v.into()) - } - - /// Converts into the underlying byte sequence. - pub fn into_bytes(self) -> Vec { - self.0 - } -} - -impl Deref for HexadecimalSequence { - type Target = [u8]; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl AsRef<[u8]> for HexadecimalSequence { - fn as_ref(&self) -> &[u8] { - &self.0 - } -} - -impl fmt::Display for HexadecimalSequence { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "0x")?; - for b in &self.0 { - write!(f, "{:02x}", b)?; - } - Ok(()) - } -} - -impl FromStr for HexadecimalSequence { - type Err = Error; - - fn from_str(input: &str) -> Result { - if !(input.starts_with("0x") || input.starts_with("0X")) { - return Err(Error::invalid_input()); - } - - if input.len() % 2 != 0 { - return Err(Error::invalid_input()); - } - - let mut result = Vec::with_capacity(input.len() / 2 - 1); - - for c in input.as_bytes().chunks(2).skip(1) { - let d = String::from_utf8(c.to_vec()).map_err(|e| Error::custom(e))?; - let b = u8::from_str_radix(d.as_str(), 16)?; - result.push(b); - } - - Ok(HexadecimalSequence(result)) - } -} diff --git a/src/types/mod.rs b/src/types/mod.rs index 10641d3..ea47eaa 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -5,7 +5,6 @@ mod decimal_floating_point; mod decimal_resolution; mod encryption_method; mod hdcp_level; -mod hexadecimal_sequence; mod in_stream_id; mod initialization_vector; mod media_type; @@ -18,9 +17,8 @@ pub(crate) use decimal_floating_point::*; pub(crate) use decimal_resolution::*; pub use encryption_method::*; pub use hdcp_level::*; -pub use hexadecimal_sequence::*; pub use in_stream_id::*; pub(crate) use initialization_vector::*; pub use media_type::*; pub use protocol_version::*; -pub use signed_decimal_floating_point::*; +pub(crate) use signed_decimal_floating_point::*; diff --git a/src/types/playlist_type.rs b/src/types/playlist_type.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/types/playlist_type.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/types/protocol_version.rs b/src/types/protocol_version.rs index 626e13e..4211a0a 100644 --- a/src/types/protocol_version.rs +++ b/src/types/protocol_version.rs @@ -19,14 +19,16 @@ pub enum ProtocolVersion { } impl fmt::Display for ProtocolVersion { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let n = match *self { - ProtocolVersion::V1 => 1, - ProtocolVersion::V2 => 2, - ProtocolVersion::V3 => 3, - ProtocolVersion::V4 => 4, - ProtocolVersion::V5 => 5, - ProtocolVersion::V6 => 6, - ProtocolVersion::V7 => 7, + let n = { + match &self { + ProtocolVersion::V1 => 1, + ProtocolVersion::V2 => 2, + ProtocolVersion::V3 => 3, + ProtocolVersion::V4 => 4, + ProtocolVersion::V5 => 5, + ProtocolVersion::V6 => 6, + ProtocolVersion::V7 => 7, + } }; write!(f, "{}", n) } @@ -35,15 +37,17 @@ impl FromStr for ProtocolVersion { type Err = Error; fn from_str(input: &str) -> Result { - Ok(match input { - "1" => ProtocolVersion::V1, - "2" => ProtocolVersion::V2, - "3" => ProtocolVersion::V3, - "4" => ProtocolVersion::V4, - "5" => ProtocolVersion::V5, - "6" => ProtocolVersion::V6, - "7" => ProtocolVersion::V7, - _ => return Err(Error::unknown_protocol_version(input)), + Ok({ + match input { + "1" => ProtocolVersion::V1, + "2" => ProtocolVersion::V2, + "3" => ProtocolVersion::V3, + "4" => ProtocolVersion::V4, + "5" => ProtocolVersion::V5, + "6" => ProtocolVersion::V6, + "7" => ProtocolVersion::V7, + _ => return Err(Error::unknown_protocol_version(input)), + } }) } } diff --git a/src/types/signed_decimal_floating_point.rs b/src/types/signed_decimal_floating_point.rs index 0c3691c..4495c93 100644 --- a/src/types/signed_decimal_floating_point.rs +++ b/src/types/signed_decimal_floating_point.rs @@ -9,7 +9,7 @@ use crate::Error; /// /// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2 #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] -pub struct SignedDecimalFloatingPoint(f64); +pub(crate) struct SignedDecimalFloatingPoint(f64); impl SignedDecimalFloatingPoint { /// Makes a new `SignedDecimalFloatingPoint` instance.