1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-12-23 20:40:29 +00:00

internalize signed_decimal_floating_point #9

This commit is contained in:
Luro02 2019-09-15 19:01:56 +02:00
parent 42469275d3
commit b932cef71a
6 changed files with 43 additions and 102 deletions

View file

@ -19,24 +19,36 @@ impl ExtXStart {
pub(crate) const PREFIX: &'static str = "#EXT-X-START:"; pub(crate) const PREFIX: &'static str = "#EXT-X-START:";
/// Makes a new `ExtXStart` tag. /// 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 { ExtXStart {
time_offset, time_offset: SignedDecimalFloatingPoint::new(time_offset).unwrap(),
precise: false, precise: false,
} }
} }
/// Makes a new `ExtXStart` tag with the given `precise` flag. /// 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 { ExtXStart {
time_offset, time_offset: SignedDecimalFloatingPoint::new(time_offset).unwrap(),
precise, precise,
} }
} }
/// Returns the time offset of the media segments in the playlist. /// Returns the time offset of the media segments in the playlist.
pub const fn time_offset(&self) -> SignedDecimalFloatingPoint { pub const fn time_offset(&self) -> f64 {
self.time_offset self.time_offset.as_f64()
} }
/// Returns whether clients should not render media stream whose presentation times are /// Returns whether clients should not render media stream whose presentation times are
@ -97,13 +109,13 @@ mod test {
#[test] #[test]
fn ext_x_start() { 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"; let text = "#EXT-X-START:TIME-OFFSET=-1.23";
assert_eq!(text.parse().ok(), Some(tag)); assert_eq!(text.parse().ok(), Some(tag));
assert_eq!(tag.to_string(), text); assert_eq!(tag.to_string(), text);
assert_eq!(tag.requires_version(), ProtocolVersion::V1); 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"; let text = "#EXT-X-START:TIME-OFFSET=1.23,PRECISE=YES";
assert_eq!(text.parse().ok(), Some(tag)); assert_eq!(text.parse().ok(), Some(tag));
assert_eq!(tag.to_string(), text); assert_eq!(tag.to_string(), text);

View file

@ -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<u8>);
impl HexadecimalSequence {
/// Makes a new `HexadecimalSequence` instance.
pub fn new<T: Into<Vec<u8>>>(v: T) -> Self {
HexadecimalSequence(v.into())
}
/// Converts into the underlying byte sequence.
pub fn into_bytes(self) -> Vec<u8> {
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<Self, Self::Err> {
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))
}
}

View file

@ -5,7 +5,6 @@ mod decimal_floating_point;
mod decimal_resolution; mod decimal_resolution;
mod encryption_method; mod encryption_method;
mod hdcp_level; mod hdcp_level;
mod hexadecimal_sequence;
mod in_stream_id; mod in_stream_id;
mod initialization_vector; mod initialization_vector;
mod media_type; mod media_type;
@ -18,9 +17,8 @@ pub(crate) use decimal_floating_point::*;
pub(crate) use decimal_resolution::*; pub(crate) use decimal_resolution::*;
pub use encryption_method::*; pub use encryption_method::*;
pub use hdcp_level::*; pub use hdcp_level::*;
pub use hexadecimal_sequence::*;
pub use in_stream_id::*; pub use in_stream_id::*;
pub(crate) use initialization_vector::*; pub(crate) use initialization_vector::*;
pub use media_type::*; pub use media_type::*;
pub use protocol_version::*; pub use protocol_version::*;
pub use signed_decimal_floating_point::*; pub(crate) use signed_decimal_floating_point::*;

View file

@ -1 +0,0 @@

View file

@ -19,14 +19,16 @@ pub enum ProtocolVersion {
} }
impl fmt::Display for ProtocolVersion { impl fmt::Display for ProtocolVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let n = match *self { let n = {
ProtocolVersion::V1 => 1, match &self {
ProtocolVersion::V2 => 2, ProtocolVersion::V1 => 1,
ProtocolVersion::V3 => 3, ProtocolVersion::V2 => 2,
ProtocolVersion::V4 => 4, ProtocolVersion::V3 => 3,
ProtocolVersion::V5 => 5, ProtocolVersion::V4 => 4,
ProtocolVersion::V6 => 6, ProtocolVersion::V5 => 5,
ProtocolVersion::V7 => 7, ProtocolVersion::V6 => 6,
ProtocolVersion::V7 => 7,
}
}; };
write!(f, "{}", n) write!(f, "{}", n)
} }
@ -35,15 +37,17 @@ impl FromStr for ProtocolVersion {
type Err = Error; type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> { fn from_str(input: &str) -> Result<Self, Self::Err> {
Ok(match input { Ok({
"1" => ProtocolVersion::V1, match input {
"2" => ProtocolVersion::V2, "1" => ProtocolVersion::V1,
"3" => ProtocolVersion::V3, "2" => ProtocolVersion::V2,
"4" => ProtocolVersion::V4, "3" => ProtocolVersion::V3,
"5" => ProtocolVersion::V5, "4" => ProtocolVersion::V4,
"6" => ProtocolVersion::V6, "5" => ProtocolVersion::V5,
"7" => ProtocolVersion::V7, "6" => ProtocolVersion::V6,
_ => return Err(Error::unknown_protocol_version(input)), "7" => ProtocolVersion::V7,
_ => return Err(Error::unknown_protocol_version(input)),
}
}) })
} }
} }

View file

@ -9,7 +9,7 @@ use crate::Error;
/// ///
/// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2 /// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct SignedDecimalFloatingPoint(f64); pub(crate) struct SignedDecimalFloatingPoint(f64);
impl SignedDecimalFloatingPoint { impl SignedDecimalFloatingPoint {
/// Makes a new `SignedDecimalFloatingPoint` instance. /// Makes a new `SignedDecimalFloatingPoint` instance.