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
//! # diesel-chrono-duration
//!
//! This crate implements storage functionality for the `chrono::Duration` type.
//! It could be included into the `diesel` itself but its policy does not allow that.
//!
//! ## Installation
//!
//! Add the following dependency to your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! diesel-chrono-duration = "0.1"
//! ```
//!
//! And add this to your root file:
//!
//! ```no_run
//! extern crate diesel_chrono_duration;
//! ```

extern crate chrono;
#[macro_use]
extern crate diesel;

use std::io::Write;

use diesel::backend::Backend;
use diesel::deserialize::{self, FromSql};
use diesel::serialize::{self, Output, ToSql};
use diesel::sql_types::BigInt;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromSqlRow, AsExpression)]
#[sql_type = "BigInt"]
pub struct ChronoDurationProxy(pub chrono::Duration);

impl From<chrono::Duration> for ChronoDurationProxy {
    fn from(duration: chrono::Duration) -> ChronoDurationProxy {
        ChronoDurationProxy(duration)
    }
}

impl AsRef<chrono::Duration> for ChronoDurationProxy {
    fn as_ref(&self) -> &chrono::Duration {
        &self.0
    }
}

impl std::ops::Deref for ChronoDurationProxy {
    type Target = chrono::Duration;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<DB> ToSql<BigInt, DB> for ChronoDurationProxy
where
    i64: ToSql<BigInt, DB>,
    DB: Backend,
{
    fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
        if let Some(num_nanoseconds) = self.0.num_nanoseconds() {
            ToSql::<BigInt, DB>::to_sql(&num_nanoseconds, out)
        } else {
            Err(format!("{:?} as nanoseconds is too large to fit in an i64", self).into())
        }
    }
}

impl<DB> FromSql<BigInt, DB> for ChronoDurationProxy
where
    i64: FromSql<BigInt, DB>,
    DB: Backend,
{
    fn from_sql(value: Option<&<DB as Backend>::RawValue>) -> deserialize::Result<Self> {
        let i64_value = <i64 as FromSql<BigInt, DB>>::from_sql(value)?;
        Ok(chrono::Duration::nanoseconds(i64_value).into())
    }
}