impl `TryFrom<Value>` for common `Option<T>` types
gitmalong opened this issue · comments
gitmalong commented
This works:
let fee: Decimal = fee.try_into().unwrap();
this doesn't
let fee: Option<Decimal> = fee.try_into().unwrap();
the trait bound `std::option::Option<rust_decimal::Decimal>: std::convert::From<gluesql_core::data::Value>` is not satisfied
Workaround
let fee: Option<Decimal> = match !fee.is_null() {
true => Some(Decimal::try_from(fee).unwrap()),
false => None,
};
Jiseok CHOI commented
The current implementation of try_from
in Value
looks like this
- Returns
ImpossibleCast
error forNull
variant - returns an
ImpossibleCast
error if the data cannot be automatically cast (dynamic type)
If your definition is,
- If
NULL
, returnOption::None
. - returns a type of
T
if the data can be cast.
Currently, you can use it as follows
https://docs.rs/gluesql/latest/gluesql/core/data/enum.Value.html#method.is_null
Use try_from
after checking Value::is_null
separately.