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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Edgedb-query crate aims to provide a bunch of traits or structs used
//! by Edgedb-query-derive crate

pub mod queries;
pub mod models;

pub use models::edge_query::EdgeQuery;
pub use models::edge_query::ToEdgeQuery;
pub use models::query_result::BasicResult;
pub use queries::filter::Filter;
pub use queries::select::Options;
pub use queries::select::SelectOptions;
pub use queries::select::OrderDir;
pub use queries::select::OrderOptions;
pub use queries::select::PageOptions;

use edgedb_protocol::model::Uuid;
use edgedb_protocol::value::Value;



macro_rules! _to_edgeql_and_to_edge_scalar_impls {
    ($($ty: ty => { scalar: $scalar: expr }),* $(,)?) => {
        $(
            impl ToEdgeQl for $ty {
                fn to_edgeql(&self) -> String {
                    self.to_string()
                }
            }
            impl ToEdgeScalar for $ty {
                fn scalar() -> String {
                    $scalar.to_owned()
                }
            }

            impl ToEdgeShape for $ty {
                fn shape() -> String {
                    String::default()
                }
            }
        )*
    }
}


pub trait ToEdgeQl {
    /// Transform a struct into a edgeDB query language statment
    fn to_edgeql(&self) -> String;
}

pub trait ToEdgeScalar {
    /// returns the cast expression corresponding to the self struct
    fn scalar() -> String;
}

pub trait ToEdgeShape {
    fn shape() -> String;
}

pub trait ToEdgeValue {
    /// Transform a struct data into a edgedb_protocol::value::Value
    fn to_edge_value(&self) -> Value;
}

_to_edgeql_and_to_edge_scalar_impls!(
    String => { scalar: "<str>" },
    i8 => { scalar: "<int16>" },
    i16 => { scalar: "<int16>" },
    i32 => { scalar: "<int32>" },
    i64 => { scalar: "<int64>" },
    f32 => { scalar: "<float32>" },
    f64 => { scalar: "<float64>" },
    bool => { scalar: "<bool>" },
    serde_json::Value => { scalar: "<json>" },
    uuid::Uuid => { scalar:"<uuid>"},
    chrono::DateTime<chrono::Utc> => { scalar: "<datetime>"},
    chrono::DateTime<chrono::Local> => { scalar: "<cal::local_datetime>"},
    chrono::Duration => { scalar : "<duration>"},
    chrono::Date<chrono::Local> => { scalar: "<cal::local_date>"},
    chrono::NaiveTime => { scalar: "<cal::local_time>"},
    chrono::NaiveDate => { scalar: "<cal::local_date>"},
);

impl ToEdgeScalar for () {
    fn scalar() -> String {
        "".to_owned()
    }
}

impl<T: ToEdgeQl> ToEdgeQl for Vec<T> {
    fn to_edgeql(&self) -> String {
        let s = self
            .iter()
            .map(|s| s.to_edgeql())
            .collect::<Vec<String>>()
            .join(",");
        format!("[{}]", s)
    }
}

impl<T: ToEdgeScalar + Default> ToEdgeScalar for Vec<T> {
    fn scalar() -> String {
        format!("<array{}>", T::scalar())
    }
}

impl<T> ToEdgeShape for Vec<T> {
    fn shape() -> String {
        String::default()
    }
}

impl ToEdgeValue for () {
    fn to_edge_value(&self) -> Value {
        Value::Str("".to_string())
    }
}

impl ToEdgeValue for String {
    fn to_edge_value(&self) -> Value {
        Value::Str(self.to_string())
    }
}

impl ToEdgeValue for i8 {
    fn to_edge_value(&self) -> Value {
        Value::Int16(*self as i16)
    }
}

impl ToEdgeValue for i16 {
    fn to_edge_value(&self) -> Value {
        Value::Int16(*self)
    }
}

impl ToEdgeValue for i32 {
    fn to_edge_value(&self) -> Value {
        Value::Int32(*self)
    }
}

impl ToEdgeValue for i64 {
    fn to_edge_value(&self) -> Value {
        Value::Int64(*self)
    }
}

impl ToEdgeValue for f32 {
    fn to_edge_value(&self) -> Value {
        Value::Float32(*self)
    }
}

impl ToEdgeValue for f64 {
    fn to_edge_value(&self) -> Value {
        Value::Float64(*self)
    }
}

impl ToEdgeValue for bool {
    fn to_edge_value(&self) -> Value {
        Value::Bool(*self)
    }
}

impl<T: ToEdgeValue> ToEdgeValue for Vec<T> {
    fn to_edge_value(&self) -> Value {
        Value::Array(
            self.iter()
                .map(|t| t.to_edge_value())
                .collect::<Vec<Value>>(),
        )
    }
}

impl ToEdgeValue for serde_json::Value {
    fn to_edge_value(&self) -> Value {
        Value::Json(self.to_string())
    }
}

impl ToEdgeValue for Uuid {
    fn to_edge_value(&self) -> Value {
        Value::Uuid(Uuid::from_u128(self.as_u128()))
    }
}