mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-19 16:58:55 +00:00
Prelude: args: use anyhow as Error/Result
This commit is contained in:
parent
82a3b5a548
commit
fe5cd8a58a
1 changed files with 10 additions and 7 deletions
|
@ -2,14 +2,17 @@ pub use duration::Duration;
|
||||||
pub use username_arg::UsernameArg;
|
pub use username_arg::UsernameArg;
|
||||||
|
|
||||||
mod duration {
|
mod duration {
|
||||||
|
use crate::{Error, Result};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::time::Duration as StdDuration;
|
use std::time::Duration as StdDuration;
|
||||||
use String as Error;
|
|
||||||
// Parse a single duration unit
|
const INVALID_DURATION: &str = "Not a valid duration";
|
||||||
fn parse_duration_string(s: &str) -> Result<StdDuration, Error> {
|
|
||||||
|
/// Parse a single duration unit
|
||||||
|
fn parse_duration_string(s: &str) -> Result<StdDuration> {
|
||||||
// We reject the empty case
|
// We reject the empty case
|
||||||
if s == "" {
|
if s == "" {
|
||||||
return Err(Error::from("empty strings are not valid durations"));
|
return Err(Error::msg("empty strings are not valid durations"));
|
||||||
}
|
}
|
||||||
struct ParseStep {
|
struct ParseStep {
|
||||||
current_value: Option<u64>,
|
current_value: Option<u64>,
|
||||||
|
@ -26,7 +29,7 @@ mod duration {
|
||||||
current_value: Some(v.unwrap_or(0) * 10 + ((item as u64) - ('0' as u64))),
|
current_value: Some(v.unwrap_or(0) * 10 + ((item as u64) - ('0' as u64))),
|
||||||
..s
|
..s
|
||||||
}),
|
}),
|
||||||
(_, None) => Err(Error::from("Not a valid duration")),
|
(_, None) => Err(Error::msg(INVALID_DURATION)),
|
||||||
(item, Some(v)) => Ok(ParseStep {
|
(item, Some(v)) => Ok(ParseStep {
|
||||||
current_value: None,
|
current_value: None,
|
||||||
current_duration: s.current_duration
|
current_duration: s.current_duration
|
||||||
|
@ -36,7 +39,7 @@ mod duration {
|
||||||
'h' => StdDuration::from_secs(60 * 60),
|
'h' => StdDuration::from_secs(60 * 60),
|
||||||
'd' => StdDuration::from_secs(60 * 60 * 24),
|
'd' => StdDuration::from_secs(60 * 60 * 24),
|
||||||
'w' => StdDuration::from_secs(60 * 60 * 24 * 7),
|
'w' => StdDuration::from_secs(60 * 60 * 24 * 7),
|
||||||
_ => return Err(Error::from("Not a valid duration")),
|
_ => return Err(Error::msg(INVALID_DURATION)),
|
||||||
} * (v as u32),
|
} * (v as u32),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
@ -44,7 +47,7 @@ mod duration {
|
||||||
.and_then(|v| match v.current_value {
|
.and_then(|v| match v.current_value {
|
||||||
// All values should be consumed
|
// All values should be consumed
|
||||||
None => Ok(v),
|
None => Ok(v),
|
||||||
_ => Err(Error::from("Not a valid duration")),
|
_ => Err(Error::msg(INVALID_DURATION)),
|
||||||
})
|
})
|
||||||
.map(|v| v.current_duration)
|
.map(|v| v.current_duration)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue