1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-21 01:38:07 +00:00
hls_m3u8/src/tags/media_segment/map.rs

140 lines
3.7 KiB
Rust
Raw Normal View History

2019-09-06 10:55:00 +00:00
use std::fmt;
use std::str::FromStr;
2019-09-13 14:06:52 +00:00
use crate::attribute::AttributePairs;
2019-09-22 08:57:28 +00:00
use crate::types::{ByteRange, ProtocolVersion, RequiredVersion};
2019-09-13 14:06:52 +00:00
use crate::utils::{quote, tag, unquote};
use crate::Error;
2019-09-22 16:00:38 +00:00
/// # [4.4.2.5. EXT-X-MAP]
/// The [ExtXMap] tag specifies how to obtain the Media Initialization
/// Section, required to parse the applicable [Media Segment]s.
2019-09-06 10:55:00 +00:00
///
2019-09-22 16:00:38 +00:00
/// Its format is:
/// ```text
/// #EXT-X-MAP:<attribute-list>
/// ```
///
/// [Media Segment]: crate::MediaSegment
/// [4.4.2.5. EXT-X-MAP]:
/// https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-04#section-4.4.2.5
2019-09-06 10:55:00 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExtXMap {
2019-09-08 09:30:52 +00:00
uri: String,
2019-09-06 10:55:00 +00:00
range: Option<ByteRange>,
}
impl ExtXMap {
pub(crate) const PREFIX: &'static str = "#EXT-X-MAP:";
/// Makes a new `ExtXMap` tag.
2019-09-08 09:30:52 +00:00
pub fn new<T: ToString>(uri: T) -> Self {
ExtXMap {
uri: uri.to_string(),
range: None,
}
2019-09-06 10:55:00 +00:00
}
/// Makes a new `ExtXMap` tag with the given range.
2019-09-08 09:30:52 +00:00
pub fn with_range<T: ToString>(uri: T, range: ByteRange) -> Self {
2019-09-06 10:55:00 +00:00
ExtXMap {
2019-09-08 09:30:52 +00:00
uri: uri.to_string(),
2019-09-06 10:55:00 +00:00
range: Some(range),
}
}
/// Returns the URI that identifies a resource that contains the media initialization section.
2019-09-08 10:23:33 +00:00
pub const fn uri(&self) -> &String {
2019-09-06 10:55:00 +00:00
&self.uri
}
/// Returns the range of the media initialization section.
2019-09-08 10:23:33 +00:00
pub const fn range(&self) -> Option<ByteRange> {
2019-09-06 10:55:00 +00:00
self.range
}
2019-09-22 08:57:28 +00:00
}
2019-09-06 10:55:00 +00:00
2019-09-22 08:57:28 +00:00
impl RequiredVersion for ExtXMap {
fn required_version(&self) -> ProtocolVersion {
2019-09-06 10:55:00 +00:00
ProtocolVersion::V6
}
}
impl fmt::Display for ExtXMap {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Self::PREFIX)?;
2019-09-08 09:30:52 +00:00
write!(f, "URI={}", quote(&self.uri))?;
2019-09-14 19:42:06 +00:00
if let Some(value) = &self.range {
write!(f, ",BYTERANGE={}", quote(value))?;
2019-09-06 10:55:00 +00:00
}
Ok(())
}
}
impl FromStr for ExtXMap {
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)?;
2019-09-06 10:55:00 +00:00
let mut uri = None;
let mut range = None;
2019-09-14 09:31:16 +00:00
for (key, value) in input.parse::<AttributePairs>()? {
match key.as_str() {
2019-09-08 09:30:52 +00:00
"URI" => uri = Some(unquote(value)),
2019-09-06 10:55:00 +00:00
"BYTERANGE" => {
2019-09-13 14:06:52 +00:00
range = Some((unquote(value).parse())?);
2019-09-06 10:55:00 +00:00
}
_ => {
// [6.3.1. General Client Responsibilities]
// > ignore any attribute/value pair with an unrecognized AttributeName.
}
}
}
2019-09-22 18:33:40 +00:00
let uri = uri.ok_or_else(|| Error::missing_value("EXT-X-URI"))?;
2019-09-06 10:55:00 +00:00
Ok(ExtXMap { uri, range })
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
2019-09-22 08:57:28 +00:00
fn test_display() {
assert_eq!(
ExtXMap::new("foo").to_string(),
"#EXT-X-MAP:URI=\"foo\"".to_string(),
);
assert_eq!(
ExtXMap::with_range("foo", ByteRange::new(9, Some(2))).to_string(),
"#EXT-X-MAP:URI=\"foo\",BYTERANGE=\"9@2\"".to_string(),
);
}
#[test]
fn test_parser() {
assert_eq!(
ExtXMap::new("foo"),
"#EXT-X-MAP:URI=\"foo\"".parse().unwrap()
);
assert_eq!(
ExtXMap::with_range("foo", ByteRange::new(9, Some(2))),
"#EXT-X-MAP:URI=\"foo\",BYTERANGE=\"9@2\"".parse().unwrap()
);
}
#[test]
fn test_required_version() {
assert_eq!(ExtXMap::new("foo").required_version(), ProtocolVersion::V6);
assert_eq!(
ExtXMap::with_range("foo", ByteRange::new(9, Some(2))).required_version(),
ProtocolVersion::V6
);
2019-09-06 10:55:00 +00:00
}
}