Skip to content

Commit

Permalink
fix: str::from builtin function with String (#170)
Browse files Browse the repository at this point in the history
- Added `from_str` function to `Value` that is called by the `str::from`
builtin function.
- `from_str` does not add quotation marks around an input string.
`str::from("foo")` returns `foo` instead of `"foo"` as before.
- Choice has been made to keep the use of `Value::to_string` formatting
for `Value::Tuple` variant. Therefore, `str::from((42, false, "foo"))`
returns `(42, false, "foo")`
  • Loading branch information
ISibboI authored Oct 12, 2024
2 parents 6608b16 + 4548b6d commit 9701989
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/function/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
Ok(Value::from(subject.trim()))
})),
"str::from" => Some(Function::new(|argument| {
Ok(Value::String(argument.to_string()))
Ok(Value::String(argument.str_from()))
})),
"str::substring" => Some(Function::new(|argument| {
let args = argument.as_ranged_len_tuple(2..=3)?;
Expand Down
33 changes: 33 additions & 0 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ impl Value {
value => Err(EvalexprError::expected_empty(value.clone())),
}
}

/// Returns a string for the `str::from` built-in function.
pub fn str_from(&self) -> String {
match self {
Value::String(v) => v.to_string(),
Value::Float(v) => v.to_string(),
Value::Int(v) => v.to_string(),
Value::Boolean(v) => v.to_string(),
Value::Tuple(_) => self.to_string(),
Value::Empty => String::from("()"),
}
}
}

impl From<String> for Value {
Expand Down Expand Up @@ -310,4 +322,25 @@ mod tests {
assert!(Value::from(true).is_boolean());
assert!(Value::from(TupleType::new()).is_tuple());
}

#[test]
fn test_value_str_from() {
assert_eq!(Value::from("string").str_from(), "string");
assert_eq!(Value::from(3.3).str_from(), "3.3");
assert_eq!(Value::from(3).str_from(), "3");
assert_eq!(Value::from(true).str_from(), "true");
assert_eq!(Value::from(()).str_from(), "()");
assert_eq!(
Value::from(TupleType::from([
Value::from("string"),
Value::from(3.3),
Value::from(3),
Value::from(TupleType::from([Value::from(42), Value::from(4.2),])),
Value::from(()),
Value::from(true),
]))
.str_from(),
r#"("string", 3.3, 3, (42, 4.2), (), true)"#
);
}
}
14 changes: 13 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,26 @@ fn test_builtin_functions() {
);
assert_eq!(
eval("str::from(\"a\")"),
Ok(Value::String(String::from("\"a\"")))
Ok(Value::String(String::from("a")))
);
assert_eq!(eval("str::from(1.0)"), Ok(Value::String(String::from("1"))));
assert_eq!(
eval("str::from(4.2)"),
Ok(Value::String(String::from("4.2")))
);
assert_eq!(eval("str::from(1)"), Ok(Value::String(String::from("1"))));
assert_eq!(
eval("str::from(true)"),
Ok(Value::String(String::from("true")))
);
assert_eq!(
eval(r#"str::from((1, "foo", , false))"#),
Ok(Value::String(String::from(r#"(1, "foo", (), false)"#)))
);
assert_eq!(
eval("str::from(true)"),
Ok(Value::String(String::from("true")))
);
assert_eq!(
eval("str::from(1, 2, 3)"),
Ok(Value::String(String::from("(1, 2, 3)")))
Expand Down

0 comments on commit 9701989

Please sign in to comment.