surrealpatch/src/fnc/cast.rs

49 lines
1.3 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::literal::Literal;
pub fn run(ctx: &Runtime, name: &String, val: Literal) -> Result<Literal, 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" => number(ctx, val),
"datetime" => datetime(ctx, val),
"duration" => duration(ctx, val),
_ => Ok(val),
}
}
pub fn bool(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
2021-03-29 15:43:37 +00:00
match val.as_bool() {
true => Ok(Literal::True),
false => Ok(Literal::False),
}
}
pub fn int(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
2021-03-29 15:43:37 +00:00
Ok(Literal::Int(val.as_int()))
}
pub fn float(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
2021-03-29 15:43:37 +00:00
Ok(Literal::Float(val.as_float()))
}
pub fn string(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
2021-03-29 15:43:37 +00:00
Ok(Literal::Strand(val.as_strand()))
}
pub fn number(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
2021-03-29 15:43:37 +00:00
Ok(Literal::Number(val.as_number()))
}
pub fn datetime(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
2021-03-29 15:43:37 +00:00
Ok(Literal::Datetime(val.as_datetime()))
}
pub fn duration(ctx: &Runtime, val: Literal) -> Result<Literal, Error> {
2021-03-29 15:43:37 +00:00
Ok(Literal::Duration(val.as_duration()))
}