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
use std::net::IpAddr;
use std::collections::HashMap;
use uuid::Uuid;
use time::Timespec;

use frame::frame_result::{ColTypeOption, CUdt, ColType, ColTypeOptionValue};
use types::{CBytes, IntoRustByName, ByName};
use types::data_serialization_types::*;
use types::list::List;
use types::map::Map;
use types::tuple::Tuple;
use error::{Result, Error, column_is_empty_err};

#[derive(Debug)]
pub struct UDT {
    data: HashMap<String, (ColTypeOption, CBytes)>,
}

impl UDT {
    pub fn new<'a>(data: Vec<CBytes>, metadata: &'a CUdt) -> UDT {
        let meta_iter = metadata.descriptions.iter();

        let acc: HashMap<String, (ColTypeOption, CBytes)> =
            HashMap::with_capacity(metadata.descriptions.len());
        let d = meta_iter.zip(data.iter()).fold(acc, |mut a, v| {
            let (m, val_b) = v;
            let &(ref name_b, ref val_type) = m;
            let name = name_b.as_plain();
            a.insert(name, (val_type.clone(), val_b.clone()));
            a
        });

        UDT { data: d }
    }
}

impl IntoRustByName<Vec<u8>> for UDT {
    fn get_by_name(&self, name: &str) -> Result<Option<Vec<u8>>> {
        self.data
            .get(name)
            .ok_or(column_is_empty_err())
            .and_then(|v| {
                let &(ref col_type, ref bytes) = v;

                match col_type.id {
                    // XXX: unwrap Option
                    ColType::Blob => {
                        decode_blob(&bytes.as_plain().unwrap())
                            .map(Some)
                            .map_err(Into::into)
                    }
                    _ => Err(Error::General(format!("Cannot parse  {:?} into UDT ", col_type.id))),
                }
            })
    }
}

impl ByName for UDT {}

into_rust_by_name!(UDT, String);
into_rust_by_name!(UDT, bool);
into_rust_by_name!(UDT, i64);
into_rust_by_name!(UDT, i32);
into_rust_by_name!(UDT, i16);
into_rust_by_name!(UDT, i8);
into_rust_by_name!(UDT, f64);
into_rust_by_name!(UDT, f32);
into_rust_by_name!(UDT, IpAddr);
into_rust_by_name!(UDT, Uuid);
into_rust_by_name!(UDT, List);
into_rust_by_name!(UDT, Map);
into_rust_by_name!(UDT, UDT);
into_rust_by_name!(UDT, Tuple);
into_rust_by_name!(UDT, Timespec);