1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-17 07:52:58 +00:00
hls_m3u8/src/types/byte_range.rs

174 lines
3.9 KiB
Rust
Raw Normal View History

2019-09-06 11:20:40 +00:00
use std::fmt;
2019-09-13 14:06:52 +00:00
use std::str::FromStr;
2019-09-10 09:05:20 +00:00
2019-09-13 14:06:52 +00:00
use crate::Error;
2019-09-10 09:05:20 +00:00
2019-09-06 11:20:40 +00:00
/// Byte range.
///
/// See: [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
2019-09-21 09:53:34 +00:00
#[derive(Copy, Hash, Eq, Ord, Debug, PartialEq, Clone, PartialOrd)]
2019-09-06 11:20:40 +00:00
pub struct ByteRange {
2019-09-10 09:05:20 +00:00
length: usize,
start: Option<usize>,
}
impl ByteRange {
/// Creates a new [ByteRange].
2019-09-22 18:33:40 +00:00
///
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
/// ByteRange::new(22, Some(12));
/// ```
2019-09-10 09:05:20 +00:00
pub const fn new(length: usize, start: Option<usize>) -> Self {
Self { length, start }
}
2019-09-21 09:53:34 +00:00
/// Returns the length of the range.
2019-09-22 18:33:40 +00:00
///
2019-09-21 09:53:34 +00:00
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
/// #
/// assert_eq!(ByteRange::new(20, Some(3)).length(), 20);
/// ```
pub const fn length(&self) -> usize {
self.length
}
/// Sets the length of the range.
2019-09-22 18:33:40 +00:00
///
2019-09-21 09:53:34 +00:00
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
/// #
/// let mut range = ByteRange::new(20, Some(3));
///
/// # assert_eq!(range.length(), 20);
/// range.set_length(10);
/// assert_eq!(range.length(), 10);
/// ```
pub fn set_length(&mut self, value: usize) -> &mut Self {
self.length = value;
self
}
/// Returns the start of the range.
2019-09-22 18:33:40 +00:00
///
2019-09-21 09:53:34 +00:00
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
/// #
/// assert_eq!(ByteRange::new(20, Some(3)).start(), Some(3));
/// ```
pub const fn start(&self) -> Option<usize> {
self.start
}
/// Sets the start of the range.
2019-09-22 18:33:40 +00:00
///
2019-09-21 09:53:34 +00:00
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
/// #
/// let mut range = ByteRange::new(20, None);
///
/// # assert_eq!(range.start(), None);
/// range.set_start(Some(3));
/// assert_eq!(range.start(), Some(3));
/// ```
pub fn set_start(&mut self, value: Option<usize>) -> &mut Self {
self.start = value;
self
}
2019-09-06 11:20:40 +00:00
}
impl fmt::Display for ByteRange {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.length)?;
if let Some(x) = self.start {
write!(f, "@{}", x)?;
}
Ok(())
}
}
impl FromStr for ByteRange {
type Err = Error;
2019-09-10 09:05:20 +00:00
2019-09-13 14:06:52 +00:00
fn from_str(s: &str) -> Result<Self, Self::Err> {
2019-09-10 09:05:20 +00:00
let tokens = s.splitn(2, '@').collect::<Vec<_>>();
if tokens.is_empty() {
2019-09-13 14:06:52 +00:00
return Err(Error::invalid_input());
2019-09-10 09:05:20 +00:00
}
2019-09-13 14:06:52 +00:00
let length = tokens[0].parse()?;
2019-09-10 09:05:20 +00:00
let start = {
if tokens.len() == 2 {
2019-09-22 18:33:40 +00:00
Some(tokens[1].parse()?)
} else {
None
2019-09-10 09:05:20 +00:00
}
2019-09-06 11:20:40 +00:00
};
2019-09-22 18:33:40 +00:00
Ok(Self::new(length, start))
2019-09-06 11:20:40 +00:00
}
}
2019-09-06 11:46:21 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() {
let byte_range = ByteRange {
length: 0,
start: Some(5),
};
assert_eq!(byte_range.to_string(), "0@5".to_string());
let byte_range = ByteRange {
length: 99999,
start: Some(2),
};
assert_eq!(byte_range.to_string(), "99999@2".to_string());
let byte_range = ByteRange {
length: 99999,
start: None,
};
assert_eq!(byte_range.to_string(), "99999".to_string());
}
#[test]
2019-09-21 09:53:34 +00:00
fn test_parser() {
2019-09-22 18:33:40 +00:00
assert_eq!(
ByteRange {
length: 99999,
start: Some(2),
},
"99999@2".parse::<ByteRange>().unwrap()
);
assert_eq!(
ByteRange {
length: 99999,
start: Some(2),
},
"99999@2".parse::<ByteRange>().unwrap()
);
assert_eq!(
ByteRange {
length: 99999,
start: None,
},
"99999".parse::<ByteRange>().unwrap()
);
assert!("".parse::<ByteRange>().is_err());
2019-09-06 11:46:21 +00:00
}
}