use std;
use std::time::Duration;
use itertools::Itertools;
#[derive(Debug, error_chain)]
pub enum ErrorKind {
Msg(String),
#[error_chain(custom)]
#[error_chain(description = r#"|_| "Can't parse duration, wrong number of colons (expected 2)""#)]
WrongNumberOfColons(()),
#[error_chain(custom)]
#[error_chain(description = r#"|_| "Can't parse duration, wrong number of commas (expected 1)""#)]
WrongNumberOfCommas(()),
#[error_chain(foreign)]
ParseIntError(std::num::ParseIntError),
}
pub fn parse_duration(in_dur: &str) -> Result<Duration> {
let bits = in_dur.split(':').collect_vec();
if bits.len() != 3 {
bail!(ErrorKind::WrongNumberOfColons(()));
}
let sec_bits = bits[2].split(',').collect_vec();
if sec_bits.len() != 2 {
bail!(ErrorKind::WrongNumberOfCommas(()));
}
let secs = bits[0].parse::<u64>()? * 60 * 60 +
bits[1].parse::<u64>()? * 60 +
sec_bits[0].parse::<u64>()?;
let millis = sec_bits[1].parse::<u64>()?;
Ok(Duration::new(secs, (millis * 1_000_000) as u32))
}