1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-03 05:59:22 +00:00
hls_m3u8/src/tags/shared/start.rs

116 lines
3.3 KiB
Rust
Raw Normal View History

use std::fmt;
use std::str::FromStr;
2019-09-13 14:06:52 +00:00
use crate::attribute::AttributePairs;
use crate::types::{ProtocolVersion, SignedDecimalFloatingPoint};
use crate::utils::{parse_yes_or_no, tag};
use crate::Error;
/// [4.3.5.2. EXT-X-START]
///
/// [4.3.5.2. EXT-X-START]: https://tools.ietf.org/html/rfc8216#section-4.3.5.2
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExtXStart {
time_offset: SignedDecimalFloatingPoint,
2018-02-14 14:11:25 +00:00
precise: bool,
}
2019-09-06 10:55:00 +00:00
impl ExtXStart {
pub(crate) const PREFIX: &'static str = "#EXT-X-START:";
/// Makes a new `ExtXStart` tag.
2019-09-08 10:23:33 +00:00
pub const fn new(time_offset: SignedDecimalFloatingPoint) -> Self {
ExtXStart {
time_offset,
2018-02-14 14:11:25 +00:00
precise: false,
}
}
/// Makes a new `ExtXStart` tag with the given `precise` flag.
2019-09-08 10:23:33 +00:00
pub const fn with_precise(time_offset: SignedDecimalFloatingPoint, precise: bool) -> Self {
ExtXStart {
time_offset,
precise,
}
}
/// Returns the time offset of the media segments in the playlist.
2019-09-08 10:23:33 +00:00
pub const fn time_offset(&self) -> SignedDecimalFloatingPoint {
self.time_offset
}
/// Returns whether clients should not render media stream whose presentation times are
/// prior to the specified time offset.
2019-09-08 10:23:33 +00:00
pub const fn precise(&self) -> bool {
self.precise
}
/// Returns the protocol compatibility version that this tag requires.
2019-09-08 10:23:33 +00:00
pub const fn requires_version(&self) -> ProtocolVersion {
ProtocolVersion::V1
}
}
2019-09-06 10:55:00 +00:00
impl fmt::Display for ExtXStart {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Self::PREFIX)?;
write!(f, "TIME-OFFSET={}", self.time_offset)?;
2018-02-14 14:11:25 +00:00
if self.precise {
write!(f, ",PRECISE=YES")?;
}
Ok(())
}
}
2019-09-06 10:55:00 +00:00
impl FromStr for ExtXStart {
type Err = Error;
2019-09-08 10:23:33 +00:00
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
let input = tag(input, Self::PREFIX)?;
let mut time_offset = None;
2018-02-14 14:11:25 +00:00
let mut precise = false;
2019-09-13 14:06:52 +00:00
let attrs = AttributePairs::parse(input);
for attr in attrs {
2019-09-13 14:06:52 +00:00
let (key, value) = (attr)?;
match key {
2019-09-13 14:06:52 +00:00
"TIME-OFFSET" => time_offset = Some((value.parse())?),
"PRECISE" => precise = (parse_yes_or_no(value))?,
_ => {
// [6.3.1. General Client Responsibilities]
// > ignore any attribute/value pair with an unrecognized AttributeName.
}
}
}
2019-09-13 14:06:52 +00:00
let time_offset = time_offset.ok_or(Error::missing_value("EXT-X-TIME-OFFSET"))?;
Ok(ExtXStart {
time_offset,
2018-02-14 14:11:25 +00:00
precise,
})
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn ext_x_start() {
2018-02-14 15:24:35 +00:00
let tag = ExtXStart::new(SignedDecimalFloatingPoint::new(-1.23).unwrap());
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);
2018-02-14 15:24:35 +00:00
let tag = ExtXStart::with_precise(SignedDecimalFloatingPoint::new(1.23).unwrap(), 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);
assert_eq!(tag.requires_version(), ProtocolVersion::V1);
}
}