mirror of
https://github.com/sile/hls_m3u8.git
synced 2024-12-23 12:30:29 +00:00
internalize signed_decimal_floating_point #9
This commit is contained in:
parent
42469275d3
commit
b932cef71a
6 changed files with 43 additions and 102 deletions
|
@ -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);
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
|
@ -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::*;
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
|
|
@ -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<Self, Self::Err> {
|
||||
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)),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue