aboutsummaryrefslogtreecommitdiff
path: root/src/commands/roll.rs
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2019-03-04 12:10:01 -0500
committerNathan Perry <avaglir@gmail.com>2019-03-04 12:10:01 -0500
commit47be7de23836aa97a9283c73bdf90d91c5f45485 (patch)
treeff2f20177f769e0f0f2fb2e625b55ec1602a3271 /src/commands/roll.rs
parent12ae9736b384d58d65e9c67186321241e2488489 (diff)
better logging for failed roll parse
Diffstat (limited to 'src/commands/roll.rs')
-rw-r--r--src/commands/roll.rs39
1 files changed, 34 insertions, 5 deletions
diff --git a/src/commands/roll.rs b/src/commands/roll.rs
index ae6c4c2..a6ea3d8 100644
--- a/src/commands/roll.rs
+++ b/src/commands/roll.rs
@@ -1,4 +1,4 @@
-use failure::err_msg;
+use failure::Error;
use nom::{
self,
double,
@@ -24,11 +24,30 @@ enum CalcExpr {
Term(f64),
}
+#[derive(Clone, Debug, PartialEq, Fail)]
+enum CalcParseError {
+ #[fail(display = "couldn't consume entire expression. remaining: '{}'.", remaining)]
+ NotReadToEnd {
+ remaining: String,
+ },
+ #[fail(display = "nom error: {}", _0)]
+ Nom(String),
+}
+
impl CalcExpr {
fn parse<S: AsRef<str>>(input: S) -> Result<Box<Self>> {
parse_expr(CompleteStr(input.as_ref()))
- .map(|(_, res)| res)
- .map_err(|e| err_msg(format!("couldn't parse: {}", e)))
+ .map_err(|e| CalcParseError::Nom(format!("{}", e)))
+ .and_then(|(s, res)| {
+ if s.len() != 0 {
+ Err(CalcParseError::NotReadToEnd {
+ remaining: s.as_ref().to_owned(),
+ })
+ } else {
+ Ok(res)
+ }
+ })
+ .map_err(Error::from)
}
fn compute(self: Box<Self>) -> f64 {
@@ -270,6 +289,16 @@ mod test {
}
pub fn roll(_ctx: &mut Context, msg: &Message, args: Args) -> Result<()> {
- let expr = CalcExpr::parse(args.rest())?;
- send(msg.channel_id, &format!("{}", expr.compute()), msg.tts)
+ match CalcExpr::parse(args.rest()) {
+ Ok(expr) => send(msg.channel_id, &format!("{}", expr.compute()), msg.tts),
+ Err(e) => {
+ let parse_err = e.downcast::<CalcParseError>().unwrap();
+ if let CalcParseError::NotReadToEnd { remaining } = parse_err {
+ error!("parsing {}: failed to consume '{}'", args.rest(), remaining);
+ send(msg.channel_id, "I COULDN'T READ THAT YOU FUCK", msg.tts)
+ } else {
+ Err(parse_err.into())
+ }
+ },
+ }
}