summaryrefslogtreecommitdiff
path: root/rust/src/lambda.rs
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud.bailly@iohk.io>2024-09-23 15:35:01 +0200
committerArnaud Bailly <arnaud.bailly@iohk.io>2024-09-23 15:35:01 +0200
commit098968d1a8f39ab5eb670b7f5370817343a0f88f (patch)
treeab79f32ad295328ae819c4d953c8ae628c4dacb9 /rust/src/lambda.rs
parent3035c35f3de47a132d2d760c041a723236ced91c (diff)
downloadlambda-nantes-098968d1a8f39ab5eb670b7f5370817343a0f88f.tar.gz
Can parse and evaluate boolean atoms
Diffstat (limited to 'rust/src/lambda.rs')
-rw-r--r--rust/src/lambda.rs26
1 files changed, 18 insertions, 8 deletions
diff --git a/rust/src/lambda.rs b/rust/src/lambda.rs
index 3c1c8b6..7601640 100644
--- a/rust/src/lambda.rs
+++ b/rust/src/lambda.rs
@@ -13,32 +13,42 @@ pub fn run(arg: &str) -> String {
#[derive(Debug, PartialEq)]
pub enum Value {
Num(i32),
+ Bool(bool),
}
impl Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Value::Num(i) => write!(f, "{}", i),
+ Value::Bool(b) => write!(f, "{}", b),
}
}
}
fn interpret(arg: &str) -> Value {
- Value::Num(arg.parse().unwrap())
+ arg.parse::<i32>()
+ .map(Value::Num)
+ .or(arg.parse::<bool>().map(Value::Bool))
+ .unwrap()
}
#[cfg(test)]
mod tests {
use super::interpret;
- use super::Value::Num;
- // Bring the macros and other important things into scope.
+ use super::Value::*;
use proptest::prelude::*;
proptest! {
- #[test]
- fn interpret_integer_as_number(i in -1000i32..1000) {
- let result = interpret(&i.to_string());
- assert_eq!(Num(i), result);
- }
+ #[test]
+ fn interpret_integer_as_number(i in -1000i32..1000) {
+ let result = interpret(&i.to_string());
+ assert_eq!(Num(i), result);
+ }
+
+ }
+ #[test]
+ fn interpret_truth_values_as_booleans() {
+ assert_eq!(Bool(true), interpret("true"));
+ assert_eq!(Bool(false), interpret("false"));
}
}