From c87e50d9b4859ad16034437e812a84cbe59db6a7 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Tue, 24 Sep 2024 08:48:55 +0200 Subject: Ignore whitespaces in input --- rust/src/lambda.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'rust/src/lambda.rs') diff --git a/rust/src/lambda.rs b/rust/src/lambda.rs index ccb8c28..8c4c640 100644 --- a/rust/src/lambda.rs +++ b/rust/src/lambda.rs @@ -28,10 +28,12 @@ impl Display for Value { } fn interpret(arg: &str) -> Value { - arg.parse::() + let token = arg.trim(); + token + .parse::() .map(Value::Num) - .or(arg.parse::().map(Value::Bool)) - .unwrap_or(Value::Sym(arg.to_string())) + .or(token.parse::().map(Value::Bool)) + .unwrap_or(Value::Sym(token.to_string())) } #[cfg(test)] @@ -59,4 +61,10 @@ mod tests { fn interpret_identifiers_values_as_symbols() { assert_eq!(Sym("foo".to_string()), interpret("foo")); } + + #[test] + fn ignores_whitespace() { + assert_eq!(Sym("foo".to_string()), interpret(" foo \n\r")); + assert_eq!(Num(-42), interpret("\n-42")); + } } -- cgit v1.2.3