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
236
237
238
239
240
241
242
243
244
245
246
247
use serde;
use std;

#[derive(Clone, Debug)]
pub enum Dypes {
    Uint(u64),
    Int(i64),
    Float(f64),
    String(String),
    Bytes(Vec<u8>),
    Null,
}

impl From<String> for Dypes {
    fn from(val: String) -> Self {
        Dypes::String(val)
    }
}

impl From<Dypes> for Option<String> {
    fn from(val: Dypes) -> Self {
        match val {
            Dypes::String(v) => Some(v),
            Dypes::Bytes(v) => match String::from_utf8(v) {
                Ok(x) => Some(x),
                Err(_) => None,
            },
            _ => None,
        }
    }
}

impl From<Dypes> for Option<bool> {
    fn from(val: Dypes) -> Self {
        match val {
            Dypes::Int(x) => Some(x != 0),
            Dypes::Uint(x) => Some(x != 0),
            _ => None,
        }
    }
}

macro_rules! impl_from_dypes_for_opt_int {
    ($i:ty) => {
        impl From<Dypes> for Option<$i> {
            fn from(val: Dypes) -> Self {
                match val {
                    Dypes::Int(x) => Some(x as $i),
                    Dypes::Uint(x) => Some(x as $i),
                    Dypes::Float(x) => Some(x as $i),
                    _ => None,
                }
            }
        }
    };
}

macro_rules! impl_to_dypes_for_uint {
    ($i:ty) => {
        impl From<$i> for Dypes {
            fn from(val: $i) -> Self {
                Dypes::Uint(val as u64)
            }
        }
    };
}

macro_rules! impl_to_dypes_for_int {
    ($i:ty) => {
        impl From<$i> for Dypes {
            fn from(val: $i) -> Self {
                Dypes::Int(val as i64)
            }
        }
    };
}

macro_rules! impl_to_dypes_for_float {
    ($i:ty) => {
        impl From<$i> for Dypes {
            fn from(val: $i) -> Self {
                Dypes::Float(val as f64)
            }
        }
    };
}

impl_to_dypes_for_uint!(u64);
impl_to_dypes_for_uint!(u32);
impl_to_dypes_for_int!(i64);
impl_to_dypes_for_int!(i32);
impl_to_dypes_for_float!(f64);
impl_to_dypes_for_float!(f32);

impl_from_dypes_for_opt_int!(u64);
impl_from_dypes_for_opt_int!(i64);
impl_from_dypes_for_opt_int!(u32);
impl_from_dypes_for_opt_int!(i32);
impl_from_dypes_for_opt_int!(f64);
impl_from_dypes_for_opt_int!(f32);

#[derive(Debug)]
pub struct Rnd2(f64);

impl Rnd2 {
    pub fn new(f: f64) -> Self {
        Rnd2(round2(f))
    }
}

pub fn round2(a: f64) -> f64 {
    (a * 100_f64).round() / 100_f64
}

impl serde::Serialize for Rnd2 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_f64(self.0)
    }
}

impl From<Dypes> for Option<Rnd2> {
    fn from(val: Dypes) -> Self {
        match val {
            Dypes::Float(x) => Some(Rnd2::new(x)),
            _ => None,
        }
    }
}

pub struct Params(pub Vec<Dypes>);

impl Params {
    pub fn new(val: Vec<Dypes>) -> Self {
        Params(val)
    }

    pub fn values(self) -> Vec<Dypes> {
        self.0
    }
}

impl<T> From<T> for Params
where
    Dypes: std::convert::From<T>,
{
    fn from(x: T) -> Self {
        Params::new(vec![Dypes::from(x)])
    }
}

impl<T> From<Vec<T>> for Params
where
    Dypes: std::convert::From<T>,
{
    fn from(x: Vec<T>) -> Self {
        Params::new(x.into_iter().map(|v| Dypes::from(v)).collect())
    }
}

/**
 * Stolen from
 * https://github.com/blackbeam/rust-mysql-simple
 */

macro_rules! into_params_impl {
    ($([$A:ident,$a:ident]),*) => (
        impl<$($A,)*> From<($($A,)*)> for Params where $(Dypes: std::convert::From<$A>,)*{
            fn from(x: ($($A,)*)) -> Params {
                let ($($a,)*) = x;
                let mut params = Vec::new();
                $(params.push(Dypes::from($a));)*
                Params::new(params)
            }
        }
    );
}

into_params_impl!([A, a]);
into_params_impl!([A, a], [B, b]);
into_params_impl!([A, a], [B, b], [C, c]);
into_params_impl!([A, a], [B, b], [C, c], [D, d]);
into_params_impl!([A, a], [B, b], [C, c], [D, d], [E, e]);
into_params_impl!([A, a], [B, b], [C, c], [D, d], [E, e], [F, f]);
into_params_impl!([A, a], [B, b], [C, c], [D, d], [E, e], [F, f], [G, g]);
into_params_impl!(
    [A, a],
    [B, b],
    [C, c],
    [D, d],
    [E, e],
    [F, f],
    [G, g],
    [H, h]
);
into_params_impl!(
    [A, a],
    [B, b],
    [C, c],
    [D, d],
    [E, e],
    [F, f],
    [G, g],
    [H, h],
    [I, i]
);
into_params_impl!(
    [A, a],
    [B, b],
    [C, c],
    [D, d],
    [E, e],
    [F, f],
    [G, g],
    [H, h],
    [I, i],
    [J, j]
);
into_params_impl!(
    [A, a],
    [B, b],
    [C, c],
    [D, d],
    [E, e],
    [F, f],
    [G, g],
    [H, h],
    [I, i],
    [J, j],
    [K, k]
);
into_params_impl!(
    [A, a],
    [B, b],
    [C, c],
    [D, d],
    [E, e],
    [F, f],
    [G, g],
    [H, h],
    [I, i],
    [J, j],
    [K, k],
    [L, l]
);