diesel/mysql/types/
numeric.rs

1#[cfg(feature = "numeric")]
2mod bigdecimal {
3    use bigdecimal::{BigDecimal, FromPrimitive};
4    use std::io::prelude::*;
5
6    use crate::deserialize::{self, FromSql};
7    use crate::mysql::{Mysql, MysqlValue, NumericRepresentation};
8    use crate::serialize::{self, IsNull, Output, ToSql};
9    use crate::sql_types::Numeric;
10
11    #[cfg(all(feature = "mysql_backend", feature = "numeric"))]
12    impl ToSql<Numeric, Mysql> for BigDecimal {
13        fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
14            write!(out, "{}", *self)
15                .map(|_| IsNull::No)
16                .map_err(Into::into)
17        }
18    }
19
20    #[cfg(all(feature = "mysql_backend", feature = "numeric"))]
21    impl FromSql<Numeric, Mysql> for BigDecimal {
22        fn from_sql(value: MysqlValue<'_>) -> deserialize::Result<Self> {
23            match value.numeric_value()? {
24                NumericRepresentation::Tiny(x) => Ok(x.into()),
25                NumericRepresentation::Small(x) => Ok(x.into()),
26                NumericRepresentation::Medium(x) => Ok(x.into()),
27                NumericRepresentation::Big(x) => Ok(x.into()),
28                NumericRepresentation::Float(x) => BigDecimal::from_f32(x)
29                    .ok_or_else(|| format!("{x} is not valid decimal number ").into()),
30                NumericRepresentation::Double(x) => BigDecimal::from_f64(x)
31                    .ok_or_else(|| format!("{x} is not valid decimal number ").into()),
32                NumericRepresentation::Decimal(bytes) => BigDecimal::parse_bytes(bytes, 10)
33                    .ok_or_else(|| format!("{bytes:?} is not valid decimal number ").into()),
34            }
35        }
36    }
37}