2019-09-06 10:55:00 +00:00
|
|
|
use std::fmt;
|
2019-09-10 09:05:20 +00:00
|
|
|
use std::ops::Deref;
|
2019-09-06 10:55:00 +00:00
|
|
|
use std::str::FromStr;
|
2019-09-10 09:05:20 +00:00
|
|
|
|
2019-09-06 10:55:00 +00:00
|
|
|
use trackable::error::ErrorKindExt;
|
|
|
|
|
2019-09-10 09:05:20 +00:00
|
|
|
use crate::types::{ByteRange, ProtocolVersion};
|
|
|
|
use crate::{Error, ErrorKind, Result};
|
|
|
|
|
2019-09-06 10:55:00 +00:00
|
|
|
/// [4.3.2.2. EXT-X-BYTERANGE]
|
|
|
|
///
|
|
|
|
/// [4.3.2.2. EXT-X-BYTERANGE]: https://tools.ietf.org/html/rfc8216#section-4.3.2.2
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2019-09-10 09:05:20 +00:00
|
|
|
pub struct ExtXByteRange(ByteRange);
|
2019-09-06 10:55:00 +00:00
|
|
|
|
|
|
|
impl ExtXByteRange {
|
|
|
|
pub(crate) const PREFIX: &'static str = "#EXT-X-BYTERANGE:";
|
|
|
|
|
|
|
|
/// Makes a new `ExtXByteRange` tag.
|
2019-09-10 09:05:20 +00:00
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use hls_m3u8::tags::ExtXByteRange;
|
|
|
|
///
|
|
|
|
/// let byte_range = ExtXByteRange::new(20, Some(5));
|
|
|
|
/// ```
|
|
|
|
pub const fn new(length: usize, start: Option<usize>) -> Self {
|
|
|
|
Self(ByteRange::new(length, start))
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 09:05:20 +00:00
|
|
|
/// Converts the [ExtXByteRange] to a [ByteRange].
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use hls_m3u8::tags::ExtXByteRange;
|
|
|
|
/// use hls_m3u8::types::ByteRange;
|
|
|
|
///
|
|
|
|
/// let byte_range = ExtXByteRange::new(20, Some(5));
|
|
|
|
/// let range: ByteRange = byte_range.to_range();
|
|
|
|
/// ```
|
|
|
|
pub const fn to_range(&self) -> ByteRange {
|
|
|
|
self.0
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the protocol compatibility version that this tag requires.
|
2019-09-10 09:05:20 +00:00
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// use hls_m3u8::tags::ExtXByteRange;
|
|
|
|
/// use hls_m3u8::types::ProtocolVersion;
|
|
|
|
///
|
|
|
|
/// let byte_range = ExtXByteRange::new(20, Some(5));
|
|
|
|
/// assert_eq!(byte_range.requires_version(), ProtocolVersion::V4);
|
|
|
|
/// ```
|
2019-09-08 10:23:33 +00:00
|
|
|
pub const fn requires_version(&self) -> ProtocolVersion {
|
2019-09-06 10:55:00 +00:00
|
|
|
ProtocolVersion::V4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:05:20 +00:00
|
|
|
impl Deref for ExtXByteRange {
|
|
|
|
type Target = ByteRange;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 10:55:00 +00:00
|
|
|
impl fmt::Display for ExtXByteRange {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-09-10 09:05:20 +00:00
|
|
|
write!(f, "{}", Self::PREFIX)?;
|
|
|
|
write!(f, "{}", self.0)?;
|
|
|
|
Ok(())
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for ExtXByteRange {
|
|
|
|
type Err = Error;
|
2019-09-08 10:23:33 +00:00
|
|
|
|
2019-09-06 10:55:00 +00:00
|
|
|
fn from_str(s: &str) -> Result<Self> {
|
2019-09-10 09:05:20 +00:00
|
|
|
// check if the string starts with the PREFIX
|
2019-09-06 10:55:00 +00:00
|
|
|
track_assert!(s.starts_with(Self::PREFIX), ErrorKind::InvalidInput);
|
2019-09-10 09:05:20 +00:00
|
|
|
let byte_range = s.split_at(Self::PREFIX.len()).1;
|
|
|
|
let tokens = byte_range.splitn(2, '@').collect::<Vec<_>>();
|
|
|
|
if tokens.is_empty() {
|
|
|
|
Err(ErrorKind::InvalidInput)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let length = tokens[0]
|
|
|
|
.parse()
|
|
|
|
.map_err(|e| ErrorKind::InvalidInput.cause(e))?;
|
|
|
|
let start = {
|
|
|
|
let mut result = None;
|
|
|
|
if tokens.len() == 2 {
|
|
|
|
result = Some(
|
|
|
|
tokens[1]
|
|
|
|
.parse()
|
|
|
|
.map_err(|e| ErrorKind::InvalidInput.cause(e))?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
result
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ExtXByteRange::new(length, start))
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2019-09-10 09:05:20 +00:00
|
|
|
fn test_display() {
|
|
|
|
let byte_range = ExtXByteRange::new(0, Some(5));
|
|
|
|
assert_eq!(byte_range.to_string(), "#EXT-X-BYTERANGE:0@5".to_string());
|
|
|
|
|
|
|
|
let byte_range = ExtXByteRange::new(99999, Some(2));
|
|
|
|
assert_eq!(
|
|
|
|
byte_range.to_string(),
|
|
|
|
"#EXT-X-BYTERANGE:99999@2".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
let byte_range = ExtXByteRange::new(99999, None);
|
|
|
|
assert_eq!(byte_range.to_string(), "#EXT-X-BYTERANGE:99999".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse() {
|
|
|
|
let byte_range = ExtXByteRange::new(99999, Some(2));
|
|
|
|
assert_eq!(
|
|
|
|
byte_range,
|
|
|
|
"#EXT-X-BYTERANGE:99999@2".parse::<ExtXByteRange>().unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let byte_range = ExtXByteRange::new(99999, None);
|
|
|
|
assert_eq!(
|
|
|
|
byte_range,
|
|
|
|
"#EXT-X-BYTERANGE:99999".parse::<ExtXByteRange>().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deref() {
|
|
|
|
let byte_range = ExtXByteRange::new(0, Some(22));
|
|
|
|
|
|
|
|
assert_eq!(*byte_range.length(), 0);
|
|
|
|
assert_eq!(*byte_range.start(), Some(22));
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|