1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-11-15 20:01:01 +00:00

Add value module

This commit is contained in:
Takeru Ohta 2018-02-13 00:44:07 +09:00
parent e933d8e013
commit 84aacd0020

38
src/value.rs Normal file
View file

@ -0,0 +1,38 @@
use std::fmt;
use std::str::FromStr;
use trackable::error::ErrorKindExt;
use {Error, ErrorKind, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ByteRange {
pub length: usize,
pub start: Option<usize>,
}
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;
fn from_str(s: &str) -> Result<Self> {
let mut tokens = s.splitn(2, '@');
let length = tokens.next().expect("Never fails");
let start = if let Some(start) = tokens.next() {
Some(track!(
start.parse().map_err(|e| ErrorKind::InvalidInput.cause(e))
)?)
} else {
None
};
Ok(ByteRange {
length: track!(length.parse().map_err(|e| ErrorKind::InvalidInput.cause(e)))?,
start,
})
}
}