wires

summary refs log tree commit diff
path: root/wyrd_sqlite/src/from_sql.rs
blob: 3b149e864616e5875d9bee2730ce4129ae49568f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::num::{NonZero, TryFromIntError};

pub enum Value<'a> {
    Int(i64),
    Float(f64),
    Text(&'a str),
    Blob(&'a [u8]),
    Null,
}

// this is an owned trait to get around some problems with blanket implementations
pub trait FromSql: Sized {
    type Error: std::error::Error;

    fn try_from_sql(value: Value<'_>) -> Result<Self, Self::Error>;
}

#[derive(Debug, thiserror::Error)]
#[error("invalid type")]
pub struct InvalidTypeError;

#[derive(Debug, thiserror::Error)]
pub enum FromSqlIntError {
    #[error(transparent)]
    InvalidType(#[from] InvalidTypeError),
    #[error(transparent)]
    TryFromInt(#[from] TryFromIntError),
}

impl FromSql for i64 {
    type Error = FromSqlIntError;

    fn try_from_sql(value: Value<'_>) -> Result<Self, Self::Error> {
        if let Value::Int(i) = value {
            Ok(i)
        } else {
            Err(InvalidTypeError.into())
        }
    }
}

impl FromSql for NonZero<i64> {
    type Error = FromSqlIntError;

    fn try_from_sql(value: Value) -> Result<Self, Self::Error> {
        let i: i64 = FromSql::try_from_sql(value)?;
        Ok(i.try_into()?)
    }
}

macro_rules! int_impl {
    ($($source:ty),+) => {$(
        impl FromSql for $source {
            type Error = FromSqlIntError;

            fn try_from_sql(value: Value<'_>) -> Result<Self, Self::Error> {
                let i: i64 = FromSql::try_from_sql(value)?;
                Ok(i.try_into()?)
            }
        }

        impl FromSql for NonZero<$source> {
            type Error = FromSqlIntError;

            fn try_from_sql(value: Value<'_>) -> Result<Self, Self::Error> {
                let i: NonZero<i64> = FromSql::try_from_sql(value)?;
                Ok(i.try_into()?)
            }
        }
    )*}
}

int_impl!(i8, i16, i32, u8, u16, u32, u64);

impl<'a, T: FromSql> FromSql for Option<T> {
    type Error = T::Error;

    fn try_from_sql(value: Value<'_>) -> Result<Self, Self::Error> {
        if let Value::Null = value {
            Ok(None)
        } else {
            FromSql::try_from_sql(value).map(Some)
        }
    }
}