surrealpatch/lib/src/fnc/cast.rs

75 lines
1.8 KiB
Rust
Raw Normal View History

use crate::dbs::Runtime;
2021-03-29 15:43:37 +00:00
use crate::err::Error;
use crate::sql::number::Number;
use crate::sql::value::Value;
2021-03-29 15:43:37 +00:00
pub fn run(ctx: &Runtime, name: &String, val: Value) -> Result<Value, Error> {
2021-03-29 15:43:37 +00:00
match name.as_str() {
"bool" => bool(ctx, val),
"int" => int(ctx, val),
"float" => float(ctx, val),
"string" => string(ctx, val),
"number" => number(ctx, val),
"decimal" => decimal(ctx, val),
2021-03-29 15:43:37 +00:00
"datetime" => datetime(ctx, val),
"duration" => duration(ctx, val),
_ => Ok(val),
}
}
pub fn bool(_: &Runtime, val: Value) -> Result<Value, Error> {
match val.is_truthy() {
true => Ok(Value::True),
false => Ok(Value::False),
2021-03-29 15:43:37 +00:00
}
}
pub fn int(_: &Runtime, val: Value) -> Result<Value, Error> {
match val {
Value::Number(Number::Int(_)) => Ok(val),
_ => Ok(Value::Number(Number::Int(val.as_int()))),
}
2021-03-29 15:43:37 +00:00
}
pub fn float(_: &Runtime, val: Value) -> Result<Value, Error> {
match val {
Value::Number(Number::Float(_)) => Ok(val),
_ => Ok(Value::Number(Number::Float(val.as_float()))),
}
2021-03-29 15:43:37 +00:00
}
pub fn number(_: &Runtime, val: Value) -> Result<Value, Error> {
match val {
Value::Number(Number::Decimal(_)) => Ok(val),
_ => Ok(Value::Number(Number::Decimal(val.as_decimal()))),
}
2021-03-29 15:43:37 +00:00
}
pub fn decimal(_: &Runtime, val: Value) -> Result<Value, Error> {
match val {
Value::Number(Number::Decimal(_)) => Ok(val),
_ => Ok(Value::Number(Number::Decimal(val.as_decimal()))),
}
2021-03-29 15:43:37 +00:00
}
pub fn string(_: &Runtime, val: Value) -> Result<Value, Error> {
match val {
Value::Strand(_) => Ok(val),
_ => Ok(Value::Strand(val.as_strand())),
}
2021-03-29 15:43:37 +00:00
}
pub fn datetime(_: &Runtime, val: Value) -> Result<Value, Error> {
match val {
Value::Datetime(_) => Ok(val),
_ => Ok(Value::Datetime(val.as_datetime())),
}
}
pub fn duration(_: &Runtime, val: Value) -> Result<Value, Error> {
match val {
Value::Duration(_) => Ok(val),
_ => Ok(Value::Duration(val.as_duration())),
}
2021-03-29 15:43:37 +00:00
}