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
use query_builder::{QueryBuilder, BuildQueryResult};
use query_source::QuerySource;

/// Types which can be passed to
/// [`update.set`](struct.IncompleteUpdateStatement.html#method.set). This can
/// be automatically generated for structs by
/// [`#[changeset_for]`](https://github.com/sgrif/diesel/tree/master/diesel_codegen#changeset_fortable_name).
pub trait AsChangeset {
    type Changeset: Changeset;

    fn as_changeset(self) -> Self::Changeset;
}

/// Apps should not need to concern themselves with this trait.
pub trait Changeset {
    type Target: QuerySource;

    fn is_noop(&self) -> bool;
    fn to_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult;
}

impl<T> AsChangeset for T where
    T: Changeset,
{
    type Changeset = Self;

    fn as_changeset(self) -> Self::Changeset {
        self
    }
}

impl<T: Changeset + ?Sized> Changeset for Box<T> {
    type Target = T::Target;

    fn is_noop(&self) -> bool {
        (&**self).is_noop()
    }

    fn to_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult {
        (&**self).to_sql(out)
    }
}

impl<T: Changeset> Changeset for Option<T> {
    type Target = T::Target;

    fn is_noop(&self) -> bool {
        self.is_none()
    }

    fn to_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult {
        match self {
            &Some(ref c) => c.to_sql(out),
            &None => Ok(()),
        }
    }
}