mailang_stdlib/stdlib/
convert.rs1use super::value_to_string;
4use mailang_bytecode::Value;
5
6pub fn builtin_to_string(args: &[Value]) -> Result<Value, String> {
7 Ok(Value::Str(value_to_string(&args[0]).into()))
8}
9
10pub fn builtin_parse_int(args: &[Value]) -> Result<Value, String> {
11 match &args[0] {
12 Value::Str(s) => s.parse::<i64>().map(Value::Int).map_err(|e| e.to_string()),
13 Value::Float(n) => Ok(Value::Int(*n as i64)),
14 Value::Int(n) => Ok(Value::Int(*n)),
15 _ => Err("parse_int expects a string or number".to_string()),
16 }
17}
18
19pub fn builtin_parse_float(args: &[Value]) -> Result<Value, String> {
20 match &args[0] {
21 Value::Str(s) => s
22 .parse::<f64>()
23 .map(Value::Float)
24 .map_err(|e| e.to_string()),
25 Value::Int(n) => Ok(Value::Float(*n as f64)),
26 Value::Float(n) => Ok(Value::Float(*n)),
27 _ => Err("parse_float expects a string or number".to_string()),
28 }
29}