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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use {Arg, Client, Connection, IntoArg, Result};
use ql2::proto::{Term, Term_AssocPair as TermPair};
use serde_json::value::Value;
use types::FromJson;

impl IntoArg for Arg {
    fn into_arg(self) -> Arg {
        self
    }
}

impl IntoArg for Client {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.query,
            term: self.term,
            pool: None,
        }
    }
}

impl<'a> IntoArg for &'a Client {
    fn into_arg(self) -> Arg {
        self.clone().into_arg()
    }
}

impl IntoArg for Term {
    fn into_arg(self) -> Arg {
        Arg {
            string: String::new(),
            term: Ok(self),
            pool: None,
        }
    }
}

impl IntoArg for String {
    fn into_arg(self) -> Arg {
        Arg {
            string: format!(r#""{}""#, self),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for char {
    fn into_arg(self) -> Arg {
        Arg {
            string: format!("'{}'", self),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl<'a> IntoArg for &'a String {
    fn into_arg(self) -> Arg {
        Arg {
            string: format!(r#""{}""#, self),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl<'a> IntoArg for &'a str {
    fn into_arg(self) -> Arg {
        Arg {
            string: format!(r#""{}""#, self),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for f32 {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.to_string(),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for i32 {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.to_string(),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for u32 {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.to_string(),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for f64 {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.to_string(),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for i64 {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.to_string(),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for u64 {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.to_string(),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for bool {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.to_string(),
            term: Term::from_json(self),
            pool: None,
        }
    }
}

impl IntoArg for Value {
    fn into_arg(self) -> Arg {
        Arg {
            string: self.to_string(),
            term: Term::from_json(self),
            pool: None,
        }
    }
}


impl IntoArg for Connection {
    fn into_arg(self) -> Arg {
        Arg {
            string: String::from("conn"),
            term: Ok(Term::new()),
            pool: Some(self),
        }
    }
}


impl Arg {
    /// Create a new command argument
    ///
    /// This is the return type of the `IntoArg` trait. You need to
    /// use `Arg::new` to create an argument when implementing that
    /// trait for any additional types that you want to pass to ReQL
    /// commands.
    ///
    /// ReQL commands are represented as `term`s so you must first
    /// convert your argument to a term and pass it as `term` to this method.
    /// For debugging and logging purposes, this method also requires that you
    /// pass the string representation of your argument i.e. `as_str`.
    pub fn new() -> Arg {
        Arg {
            string: String::new(),
            term: Ok(Term::new()),
            pool: None,
        }
    }

    #[doc(hidden)]
    pub fn set_string(&mut self, string: &str) {
        self.string = string.into();
    }

    #[doc(hidden)]
    pub fn set_term(&mut self, term: Result<Term>) {
        self.term = term;
    }

    #[doc(hidden)]
    pub fn add_arg(&mut self, arg: Arg) {
        if let Some(pool) = arg.pool {
            self.pool = Some(pool);
        }
        let mut error = None;
        if let Ok(ref mut term) = self.term {
            match arg.term {
                Ok(aterm) => term.mut_args().push(aterm),
                Err(e) => {
                    error = Some(e);
                }
            }
        }
        if let Some(e) = error {
            self.term = Err(e);
        }
    }

    #[doc(hidden)]
    pub fn add_opt(&mut self, temp_pair: TermPair) {
        if let Ok(ref mut term) = self.term {
            term.mut_optargs().push(temp_pair);
        }
    }

    #[doc(hidden)]
    pub fn create_term_pair<T: ::IntoArg>(key: &str, val: T) -> Result<TermPair> {
        let mut temp = Term::new();
        temp.mut_args().push(val.into_arg().term?);
        let mut temp_pair = TermPair::new();
        temp_pair.set_key(key.into());
        temp_pair.set_val(temp);
        Ok(temp_pair)
    }
}