[][src]Trait serde_db::de::DbValueInto

pub trait DbValueInto<T> {
    fn try_into(self) -> Result<T, ConversionError>;
}

Converts the given database value into a specific rust type.

We recommend to implement try_into as gracefully as possible, i.e., supporting as many conversions as possible. For the numeric types this requires some lines of code, but the effort pays off for the users.

Example:

This example is not tested
impl DbValueInto<u32> for MyDbValue {
    fn try_into(self) -> Result<u32, ConversionError> {
        match self {
            MyDbValue::TINYINT(u) => Ok(u as u32),

            MyDbValue::SMALLINT(i) => if i >= 0 {
                Ok(i as u32)
            } else {
                Err(ConversionError::NumberRange(...))
            }

            MyDbValue::INT(i) => if i >= 0 {
                Ok(i as u32)
            } else {
                Err(ConversionError::NumberRange(...))
            }

            MyDbValue::BIGINT(i) => if (i >= 0) && (i <= u32::MAX as i64) {
                Ok(i as u32)
            } else {
                Err(ConversionError::NumberRange(...))
            }

            _ => Err(ConversionError::ValueType(...)),
        }
    }
 }

Required methods

fn try_into(self) -> Result<T, ConversionError>

Converts the database value into the target rust type.

Errors

ConversionError if the value cannot be converted into the target type.

Loading content...

Implementors

Loading content...