2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2021-03-29 15:43:37 +00:00
|
|
|
use crate::err::Error;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::sql::number::Number;
|
|
|
|
use crate::sql::value::Value;
|
2021-03-29 15:43:37 +00:00
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn run(_: &Context, name: &str, val: Value) -> Result<Value, Error> {
|
2022-03-04 16:01:32 +00:00
|
|
|
match name {
|
2022-08-17 17:10:09 +00:00
|
|
|
"bool" => bool(val),
|
|
|
|
"int" => int(val),
|
|
|
|
"float" => float(val),
|
|
|
|
"string" => string(val),
|
|
|
|
"number" => number(val),
|
|
|
|
"decimal" => decimal(val),
|
|
|
|
"datetime" => datetime(val),
|
|
|
|
"duration" => duration(val),
|
2021-03-29 15:43:37 +00:00
|
|
|
_ => Ok(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn bool(val: Value) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match val.is_truthy() {
|
|
|
|
true => Ok(Value::True),
|
|
|
|
false => Ok(Value::False),
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn int(val: Value) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match val {
|
|
|
|
Value::Number(Number::Int(_)) => Ok(val),
|
|
|
|
_ => Ok(Value::Number(Number::Int(val.as_int()))),
|
|
|
|
}
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn float(val: Value) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match val {
|
|
|
|
Value::Number(Number::Float(_)) => Ok(val),
|
|
|
|
_ => Ok(Value::Number(Number::Float(val.as_float()))),
|
|
|
|
}
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn number(val: Value) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match val {
|
|
|
|
Value::Number(Number::Decimal(_)) => Ok(val),
|
|
|
|
_ => Ok(Value::Number(Number::Decimal(val.as_decimal()))),
|
|
|
|
}
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn decimal(val: Value) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match val {
|
|
|
|
Value::Number(Number::Decimal(_)) => Ok(val),
|
|
|
|
_ => Ok(Value::Number(Number::Decimal(val.as_decimal()))),
|
|
|
|
}
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn string(val: Value) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match val {
|
|
|
|
Value::Strand(_) => Ok(val),
|
|
|
|
_ => Ok(Value::Strand(val.as_strand())),
|
|
|
|
}
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn datetime(val: Value) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match val {
|
|
|
|
Value::Datetime(_) => Ok(val),
|
|
|
|
_ => Ok(Value::Datetime(val.as_datetime())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:09 +00:00
|
|
|
pub fn duration(val: Value) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
match val {
|
|
|
|
Value::Duration(_) => Ok(val),
|
|
|
|
_ => Ok(Value::Duration(val.as_duration())),
|
|
|
|
}
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|