1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#![cfg(feature = "bigdecimal")]

use bigdecimal::{BigDecimal, FromPrimitive};

use crate::deserialize::{self, FromSql};
use crate::sql_types::{Double, Numeric};
use crate::sqlite::connection::SqliteValue;
use crate::sqlite::Sqlite;

impl FromSql<Numeric, Sqlite> for BigDecimal {
    fn from_sql(bytes: SqliteValue<'_>) -> deserialize::Result<Self> {
        let x = <f64 as FromSql<Double, Sqlite>>::from_sql(bytes)?;
        BigDecimal::from_f64(x).ok_or_else(|| format!("{} is not valid decimal number ", x).into())
    }
}