mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-21 14:19:22 +00:00
Replace more anyhow errors with explicit error types
This commit is contained in:
@@ -1,30 +1,32 @@
|
||||
use crate::Error;
|
||||
use anyhow::Result;
|
||||
use chrono::{Duration, NaiveDateTime};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
static ref RE_DURATION: regex::Regex = regex::Regex::new(r"^(?<sign>[+-])?P((?P<W>\d+)W)?((?P<D>\d+)D)?(T((?P<H>\d+)H)?((?P<M>\d+)M)?((?P<S>\d+)S)?)?$").unwrap();
|
||||
}
|
||||
use anyhow::{anyhow, Result};
|
||||
use chrono::{Duration, NaiveDateTime};
|
||||
|
||||
pub fn parse_duration(string: &str) -> Result<Duration> {
|
||||
pub fn parse_duration(string: &str) -> Result<Duration, Error> {
|
||||
let captures = RE_DURATION
|
||||
.captures(string)
|
||||
.ok_or(anyhow!("invalid duration format"))?;
|
||||
.ok_or(Error::InvalidIcs("Invalid duration format".to_owned()))?;
|
||||
|
||||
let mut duration = Duration::zero();
|
||||
if let Some(weeks) = captures.name("W") {
|
||||
duration += Duration::weeks(weeks.as_str().parse()?);
|
||||
duration += Duration::weeks(weeks.as_str().parse().unwrap());
|
||||
}
|
||||
if let Some(days) = captures.name("D") {
|
||||
duration += Duration::days(days.as_str().parse()?);
|
||||
duration += Duration::days(days.as_str().parse().unwrap());
|
||||
}
|
||||
if let Some(hours) = captures.name("H") {
|
||||
duration += Duration::hours(hours.as_str().parse()?);
|
||||
duration += Duration::hours(hours.as_str().parse().unwrap());
|
||||
}
|
||||
if let Some(minutes) = captures.name("M") {
|
||||
duration += Duration::minutes(minutes.as_str().parse()?);
|
||||
duration += Duration::minutes(minutes.as_str().parse().unwrap());
|
||||
}
|
||||
if let Some(seconds) = captures.name("S") {
|
||||
duration += Duration::seconds(seconds.as_str().parse()?);
|
||||
duration += Duration::seconds(seconds.as_str().parse().unwrap());
|
||||
}
|
||||
if let Some(sign) = captures.name("sign") {
|
||||
if sign.as_str() == "-" {
|
||||
|
||||
Reference in New Issue
Block a user