cdrs/types/
udt.rs

1use std::collections::HashMap;
2use std::net::IpAddr;
3use time::Timespec;
4use uuid::Uuid;
5
6use crate::error::{column_is_empty_err, Error, Result};
7use crate::frame::frame_result::{CUdt, ColType, ColTypeOption, ColTypeOptionValue};
8use crate::types::blob::Blob;
9use crate::types::data_serialization_types::*;
10use crate::types::decimal::Decimal;
11use crate::types::list::List;
12use crate::types::map::Map;
13use crate::types::tuple::Tuple;
14use crate::types::{ByName, CBytes, IntoRustByName};
15
16#[derive(Clone, Debug)]
17pub struct UDT {
18    data: HashMap<String, (ColTypeOption, CBytes)>,
19}
20
21impl UDT {
22    pub fn new<'a>(data: Vec<CBytes>, metadata: &'a CUdt) -> UDT {
23        let meta_iter = metadata.descriptions.iter();
24
25        let acc: HashMap<String, (ColTypeOption, CBytes)> =
26            HashMap::with_capacity(metadata.descriptions.len());
27        let d = meta_iter.zip(data.iter()).fold(acc, |mut a, v| {
28            let (m, val_b) = v;
29            let &(ref name_b, ref val_type) = m;
30            let name = name_b.as_plain();
31            a.insert(name, (val_type.clone(), val_b.clone()));
32            a
33        });
34
35        UDT { data: d }
36    }
37}
38
39impl ByName for UDT {}
40
41into_rust_by_name!(UDT, Blob);
42into_rust_by_name!(UDT, String);
43into_rust_by_name!(UDT, bool);
44into_rust_by_name!(UDT, i64);
45into_rust_by_name!(UDT, i32);
46into_rust_by_name!(UDT, i16);
47into_rust_by_name!(UDT, i8);
48into_rust_by_name!(UDT, f64);
49into_rust_by_name!(UDT, f32);
50into_rust_by_name!(UDT, IpAddr);
51into_rust_by_name!(UDT, Uuid);
52into_rust_by_name!(UDT, List);
53into_rust_by_name!(UDT, Map);
54into_rust_by_name!(UDT, UDT);
55into_rust_by_name!(UDT, Tuple);
56into_rust_by_name!(UDT, Timespec);
57into_rust_by_name!(UDT, Decimal);