Sort casting functions by name

This commit is contained in:
Tobie Morgan Hitchcock 2023-03-18 20:08:06 +00:00
parent 735856e5b3
commit 9afb3499ae
2 changed files with 31 additions and 31 deletions

View file

@ -6,13 +6,13 @@ use crate::sql::value::Value;
pub fn run(_: &Context, name: &str, val: Value) -> Result<Value, Error> {
match name {
"bool" => bool(val),
"int" => int(val),
"float" => float(val),
"string" => string(val),
"number" => number(val),
"decimal" => decimal(val),
"datetime" => datetime(val),
"decimal" => decimal(val),
"duration" => duration(val),
"float" => float(val),
"int" => int(val),
"number" => number(val),
"string" => string(val),
_ => Ok(val),
}
}
@ -21,10 +21,24 @@ pub fn bool(val: Value) -> Result<Value, Error> {
Ok(val.is_truthy().into())
}
pub fn int(val: Value) -> Result<Value, Error> {
pub fn datetime(val: Value) -> Result<Value, Error> {
Ok(match val {
Value::Number(Number::Int(_)) => val,
_ => Value::Number(Number::Int(val.as_int())),
Value::Datetime(_) => val,
_ => Value::Datetime(val.as_datetime()),
})
}
pub fn decimal(val: Value) -> Result<Value, Error> {
Ok(match val {
Value::Number(Number::Decimal(_)) => val,
_ => Value::Number(Number::Decimal(val.as_decimal())),
})
}
pub fn duration(val: Value) -> Result<Value, Error> {
Ok(match val {
Value::Duration(_) => val,
_ => Value::Duration(val.as_duration()),
})
}
@ -35,14 +49,14 @@ pub fn float(val: Value) -> Result<Value, Error> {
})
}
pub fn number(val: Value) -> Result<Value, Error> {
pub fn int(val: Value) -> Result<Value, Error> {
Ok(match val {
Value::Number(Number::Decimal(_)) => val,
_ => Value::Number(Number::Decimal(val.as_decimal())),
Value::Number(Number::Int(_)) => val,
_ => Value::Number(Number::Int(val.as_int())),
})
}
pub fn decimal(val: Value) -> Result<Value, Error> {
pub fn number(val: Value) -> Result<Value, Error> {
Ok(match val {
Value::Number(Number::Decimal(_)) => val,
_ => Value::Number(Number::Decimal(val.as_decimal())),
@ -55,17 +69,3 @@ pub fn string(val: Value) -> Result<Value, Error> {
_ => Value::Strand(val.as_strand()),
})
}
pub fn datetime(val: Value) -> Result<Value, Error> {
Ok(match val {
Value::Datetime(_) => val,
_ => Value::Datetime(val.as_datetime()),
})
}
pub fn duration(val: Value) -> Result<Value, Error> {
Ok(match val {
Value::Duration(_) => val,
_ => Value::Duration(val.as_duration()),
})
}

View file

@ -202,13 +202,13 @@ fn cast(i: &str) -> IResult<&str, Function> {
fn function_casts(i: &str) -> IResult<&str, &str> {
alt((
tag("bool"),
tag("int"),
tag("float"),
tag("string"),
tag("number"),
tag("decimal"),
tag("datetime"),
tag("decimal"),
tag("duration"),
tag("float"),
tag("int"),
tag("number"),
tag("string"),
))(i)
}