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:
parent
e933d8e013
commit
84aacd0020
1 changed files with 38 additions and 0 deletions
38
src/value.rs
Normal file
38
src/value.rs
Normal 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,
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue