partiql-common 0.15.0

PartiQL Core
Documentation
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use indexmap::map::Entry;
use indexmap::IndexMap;
use rust_decimal::Decimal;
use std::borrow::Borrow;
use std::fmt::Result;
use std::fmt::{Display, Formatter};
use std::hash::Hash;

/// Provides a mean to store meta-data for PartiQL objects.
///
/// # Examples
/// ```
/// use partiql_common::metadata::{PartiqlMetadata, PartiqlMetaValue};
///
/// let foo_val = PartiqlMetaValue::String("foo".to_string());
/// let i64_val = PartiqlMetaValue::Int64(2);///
/// let expected_vec_val = vec![foo_val, i64_val];
///
/// let expected_bool_val = true;///
/// let expected_int_val = 2;///
/// let expected_float_val = 2.5;///
/// let expected_str_val = "foo";///
///
/// let mut expected_map = PartiqlMetadata::new();///
/// expected_map.insert("bool value", expected_bool_val.into());///
/// expected_map.insert("integer value", expected_int_val.into());///
///
/// let mut metas = PartiqlMetadata::new();///
/// metas.insert("vec value", expected_vec_val.clone().into());
/// metas.insert("bool value", expected_bool_val.into());///
/// metas.insert("integer value", expected_int_val.into());///
/// metas.insert("float value", expected_float_val.into());///
/// metas.insert("string value", expected_str_val.into());///
/// metas.insert("map value", expected_map.clone().into());///
///
/// let vec_val = metas.vec_value("vec value").expect("vec meta value");///
/// let bool_val = metas.bool_value("bool value").expect("bool meta value");///
/// let int_val = metas.i32_value("integer value").expect("i32 meta value");///
/// let float_val = metas.f64_value("float value").expect("f64 meta value");///
/// let string_val = metas.string_value("string value").expect("string meta value");///
/// let map_val = metas.map_value("map value").expect("map meta value");///
///
/// assert_eq!(vec_val, expected_vec_val.clone());///
/// assert_eq!(bool_val, expected_bool_val.clone());///
/// assert_eq!(int_val, expected_int_val.clone());///
/// assert_eq!(float_val, expected_float_val.clone());///
/// assert_eq!(string_val, expected_str_val);///
/// assert_eq!(map_val, expected_map.clone());
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct PartiqlMetadata<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    inner: IndexMap<T, PartiqlMetaValue<T>>,
}

#[allow(dead_code)]
impl<T> PartiqlMetadata<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    pub fn new() -> Self {
        Self {
            inner: IndexMap::new(),
        }
    }

    pub fn insert(&mut self, key: T, value: PartiqlMetaValue<T>) {
        self.inner.insert(key, value);
    }

    pub fn get(&self, key: &T) -> Option<&PartiqlMetaValue<T>> {
        self.inner.get(key)
    }

    pub fn get_mut(&mut self, key: &T) -> Option<&mut PartiqlMetaValue<T>> {
        self.inner.get_mut(key)
    }

    pub fn contains_key(&self, key: &T) -> bool {
        self.inner.contains_key(key)
    }

    pub fn keys(&self) -> impl Iterator<Item = &T> {
        self.inner.keys()
    }

    pub fn values(&self) -> impl Iterator<Item = &PartiqlMetaValue<T>> {
        self.inner.values()
    }

    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut PartiqlMetaValue<T>> {
        self.inner.values_mut()
    }

    pub fn entry(&mut self, key: T) -> Entry<'_, T, PartiqlMetaValue<T>> {
        self.inner.entry(key)
    }

    pub fn clear(&mut self) {
        self.inner.clear();
    }

    pub fn remove(&mut self, key: &T) -> Option<PartiqlMetaValue<T>> {
        self.inner.swap_remove(key)
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn iter(&self) -> impl Iterator<Item = (&T, &PartiqlMetaValue<T>)> {
        self.inner.iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&T, &mut PartiqlMetaValue<T>)> {
        self.inner.iter_mut()
    }

    pub fn vec_value(&self, key: &str) -> Option<Vec<PartiqlMetaValue<T>>> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::Array(v)) = value {
            Some(v.clone())
        } else {
            None
        }
    }

    pub fn bool_value(&self, key: &str) -> Option<bool> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::Bool(v)) = value {
            Some(*v)
        } else {
            None
        }
    }

    pub fn f32_value(&self, key: &str) -> Option<f32> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::Float32(v)) = value {
            Some(*v)
        } else {
            None
        }
    }

    pub fn f64_value(&self, key: &str) -> Option<f64> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::Float64(v)) = value {
            Some(*v)
        } else {
            None
        }
    }

    pub fn decimal_value(&self, key: &str) -> Option<Decimal> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::Decimal(v)) = value {
            Some(*v)
        } else {
            None
        }
    }

    pub fn i32_value(&self, key: &str) -> Option<i32> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::Int32(v)) = value {
            Some(*v)
        } else {
            None
        }
    }

    pub fn i64_value(&self, key: &str) -> Option<i64> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::Int64(v)) = value {
            Some(*v)
        } else {
            None
        }
    }

    pub fn map_value(&self, key: &str) -> Option<PartiqlMetadata<T>> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::Map(v)) = value {
            Some(v.clone())
        } else {
            None
        }
    }

    pub fn string_value(&self, key: &str) -> Option<String> {
        let value = self.inner.get(key);
        if let Some(PartiqlMetaValue::String(v)) = value {
            Some(v.clone())
        } else {
            None
        }
    }
}

impl<T> Default for PartiqlMetadata<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn default() -> Self {
        Self {
            inner: IndexMap::new(),
        }
    }
}

impl<T> FromIterator<(T, PartiqlMetaValue<T>)> for PartiqlMetadata<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from_iter<I: IntoIterator<Item = (T, PartiqlMetaValue<T>)>>(iter: I) -> Self {
        let inner = iter.into_iter().collect();
        Self { inner }
    }
}

impl<T> IntoIterator for PartiqlMetadata<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    type Item = (T, PartiqlMetaValue<T>);
    type IntoIter = indexmap::map::IntoIter<T, PartiqlMetaValue<T>>;

    fn into_iter(self) -> Self::IntoIter {
        self.inner.into_iter()
    }
}

#[derive(Clone, Debug, PartialEq)]
#[allow(dead_code)]
#[non_exhaustive]
pub enum PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    Array(Vec<PartiqlMetaValue<T>>),
    Bool(bool),
    Float32(f32),
    Float64(f64),
    Decimal(Decimal),
    Int32(i32),
    Int64(i64),
    Map(PartiqlMetadata<T>),
    String(String),
}

impl<T> From<bool> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(value: bool) -> Self {
        PartiqlMetaValue::Bool(value)
    }
}

impl<T> From<i32> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(value: i32) -> Self {
        PartiqlMetaValue::Int32(value)
    }
}
impl<T> From<i64> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(value: i64) -> Self {
        PartiqlMetaValue::Int64(value)
    }
}

impl<T> From<f64> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(value: f64) -> Self {
        PartiqlMetaValue::Float64(value)
    }
}

impl<T> From<String> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(value: String) -> Self {
        PartiqlMetaValue::String(value)
    }
}
impl<T> From<&'static str> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(value: &'static str) -> Self {
        PartiqlMetaValue::String(value.to_owned())
    }
}

impl<T> From<Vec<PartiqlMetaValue<T>>> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(value: Vec<PartiqlMetaValue<T>>) -> Self {
        PartiqlMetaValue::Array(value)
    }
}

impl<T> From<&[PartiqlMetaValue<T>]> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(slice: &[PartiqlMetaValue<T>]) -> Self {
        PartiqlMetaValue::Array(slice.to_vec())
    }
}

impl<T> From<PartiqlMetadata<T>> for PartiqlMetaValue<T>
where
    T: Eq + Clone + Hash + Borrow<str>,
{
    fn from(value: PartiqlMetadata<T>) -> Self {
        PartiqlMetaValue::Map(value)
    }
}

impl<T> Display for PartiqlMetaValue<T>
where
    T: Eq + Hash + Display + Clone + Borrow<str>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        match self {
            PartiqlMetaValue::Array(arr) => {
                write!(f, "[")?;
                for (idx, item) in arr.iter().enumerate() {
                    if idx > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{item}")?;
                }
                write!(f, "]")
            }
            PartiqlMetaValue::Bool(v) => write!(f, "{v}"),
            PartiqlMetaValue::Decimal(v) => write!(f, "{v}"),
            PartiqlMetaValue::Float64(v) => write!(f, "{v}"),
            PartiqlMetaValue::Float32(v) => write!(f, "{v}"),
            PartiqlMetaValue::Int32(v) => write!(f, "{v}"),
            PartiqlMetaValue::Int64(v) => write!(f, "{v}"),
            PartiqlMetaValue::Map(map) => {
                write!(f, "{{")?;
                for (t, v) in map.iter() {
                    write!(f, "{t}: {v} , ")?;
                }
                write!(f, "}}")
            }
            PartiqlMetaValue::String(v) => write!(f, "{v}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::metadata::{PartiqlMetaValue, PartiqlMetadata};

    #[test]
    fn test_metadata() {
        let foo_val = PartiqlMetaValue::String("foo".to_string());
        let i64_val = PartiqlMetaValue::Int64(2);

        let expected_vec_val = vec![foo_val, i64_val];
        let expected_bool_val = true;
        let expected_int_val = 2;
        let expected_float_val = 2.5;
        let expected_str_val = "foo";

        let mut expected_map = PartiqlMetadata::new();
        expected_map.insert("bool value", expected_bool_val.into());
        expected_map.insert("integer value", expected_int_val.into());

        let mut metas = PartiqlMetadata::new();
        metas.insert("vec value", expected_vec_val.clone().into());
        metas.insert("bool value", expected_bool_val.into());
        metas.insert("integer value", expected_int_val.into());
        metas.insert("float value", expected_float_val.into());
        metas.insert("string value", expected_str_val.into());
        metas.insert("map value", expected_map.clone().into());

        let vec_val = metas.vec_value("vec value").expect("vec meta value");
        let bool_val = metas.bool_value("bool value").expect("bool meta value");
        let int_val = metas.i32_value("integer value").expect("i32 meta value");
        let float_val = metas.f64_value("float value").expect("f64 meta value");
        let string_val = metas
            .string_value("string value")
            .expect("string meta value");
        let map_val = metas.map_value("map value").expect("map meta value");

        assert_eq!(vec_val, expected_vec_val.clone());
        assert_eq!(bool_val, expected_bool_val.clone());
        assert_eq!(int_val, expected_int_val.clone());
        assert_eq!(float_val, expected_float_val.clone());
        assert_eq!(string_val, expected_str_val);
        assert_eq!(map_val, expected_map.clone());
    }
}