From b625a71711cc2226e3443b6ec2a914c2d6d9f82a Mon Sep 17 00:00:00 2001 From: Alexander Bethel Date: Fri, 21 May 2021 12:25:37 -0500 Subject: [PATCH] Allow abool -> bool coercion The expression `sometimes & true` now evaluates to `true` 50% of the time and false 50% of the time, rather than throwing a type error because `sometimes` is not a bool. --- src/variables.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/variables.rs b/src/variables.rs index 2ea96e4..3150b44 100644 --- a/src/variables.rs +++ b/src/variables.rs @@ -56,10 +56,9 @@ impl TryFrom for i32 { type Error = Error; fn try_from(value: Value) -> Result { - if let Value::Int(i) = value { - Ok(i) - } else { - Err(Error { + match value { + Value::Int(i) => Ok(i), + _ => Err(Error { kind: ErrorKind::TypeError(format!("Expected int, got {}", value)), // TODO: either add some kind of metadata to `Value` // so we can tell where the value came from and assign @@ -67,7 +66,7 @@ impl TryFrom for i32 { // `error::Error` struct so we can omit the `position` // when using some error kinds. position: 0..0, - }) + }), } } } @@ -76,13 +75,13 @@ impl TryFrom for bool { type Error = Error; fn try_from(value: Value) -> Result { - if let Value::Bool(b) = value { - Ok(b) - } else { - Err(Error { + match value { + Value::Bool(b) => Ok(b), + Value::Abool(b) => Ok(b.into()), + _ => Err(Error { kind: ErrorKind::TypeError(format!("Expected bool, got {}", value)), position: 0..0, - }) + }), } } }