mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 07:02:24 +00:00
Replace more anyhow errors with explicit error types
This commit is contained in:
@@ -3,6 +3,9 @@ pub enum Error {
|
||||
#[error("Not found")]
|
||||
NotFound,
|
||||
|
||||
#[error("Invalid ics input: {0}")]
|
||||
InvalidIcs(String),
|
||||
|
||||
#[error(transparent)]
|
||||
SqlxError(sqlx::Error),
|
||||
|
||||
|
||||
@@ -58,10 +58,14 @@ impl Event {
|
||||
let mut parser = ical::IcalParser::new(BufReader::new(ics.as_bytes()));
|
||||
let cal = parser.next().ok_or(Error::NotFound)??;
|
||||
if parser.next().is_some() {
|
||||
return Err(anyhow!("multiple calendars!").into());
|
||||
return Err(Error::InvalidIcs(
|
||||
"multiple calendars, only one allowed".to_owned(),
|
||||
));
|
||||
}
|
||||
if cal.events.len() != 1 {
|
||||
return Err(anyhow!("multiple or no events").into());
|
||||
return Err(Error::InvalidIcs(
|
||||
"ics calendar must contain exactly one event".to_owned(),
|
||||
));
|
||||
}
|
||||
let event = Self { uid, cal, ics };
|
||||
// Run getters now to validate the input and ensure that they'll work later on
|
||||
|
||||
@@ -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