cassandra_proto/types/
rows.rs

1use std::net::IpAddr;
2use time::Timespec;
3use uuid::Uuid;
4
5use crate::error::{column_is_empty_err, Error, Result};
6use crate::frame::frame_result::{
7    BodyResResultRows, ColSpec, ColType, ColTypeOption, ColTypeOptionValue, RowsMetadata,
8};
9use crate::types::blob::Blob;
10use crate::types::data_serialization_types::*;
11use crate::types::decimal::Decimal;
12use crate::types::list::List;
13use crate::types::map::Map;
14use crate::types::tuple::Tuple;
15use crate::types::udt::UDT;
16use crate::types::{ByIndex, ByName, CBytes, IntoRustByIndex, IntoRustByName};
17
18#[derive(Clone, Debug)]
19pub struct Row {
20    metadata: RowsMetadata,
21    row_content: Vec<CBytes>,
22}
23
24impl Row {
25    pub fn from_frame_body(body: BodyResResultRows) -> Vec<Row> {
26        body.rows_content
27            .iter()
28            .map(|row| Row {
29                metadata: body.metadata.clone(),
30                row_content: row.clone(),
31            }).collect()
32    }
33
34    fn get_col_spec_by_name(&self, name: &str) -> Option<(&ColSpec, &CBytes)> {
35        self.metadata
36            .col_specs
37            .iter()
38            .position(|spec| spec.name.as_str() == name)
39            .map(|i| {
40                let ref col_spec = self.metadata.col_specs[i];
41                let ref data = self.row_content[i];
42                (col_spec, data)
43            })
44    }
45
46    fn get_col_spec_by_index(&self, index: usize) -> Option<(&ColSpec, &CBytes)> {
47        let specs = self.metadata.col_specs.iter();
48        let values = self.row_content.iter();
49        specs.zip(values).nth(index)
50    }
51}
52
53impl ByName for Row {}
54
55into_rust_by_name!(Row, Blob);
56into_rust_by_name!(Row, String);
57into_rust_by_name!(Row, bool);
58into_rust_by_name!(Row, i64);
59into_rust_by_name!(Row, i32);
60into_rust_by_name!(Row, i16);
61into_rust_by_name!(Row, i8);
62into_rust_by_name!(Row, f64);
63into_rust_by_name!(Row, f32);
64into_rust_by_name!(Row, IpAddr);
65into_rust_by_name!(Row, Uuid);
66into_rust_by_name!(Row, List);
67into_rust_by_name!(Row, Map);
68into_rust_by_name!(Row, UDT);
69into_rust_by_name!(Row, Tuple);
70into_rust_by_name!(Row, Timespec);
71into_rust_by_name!(Row, Decimal);
72
73impl ByIndex for Row {}
74
75into_rust_by_index!(Row, Blob);
76into_rust_by_index!(Row, String);
77into_rust_by_index!(Row, bool);
78into_rust_by_index!(Row, i64);
79into_rust_by_index!(Row, i32);
80into_rust_by_index!(Row, i16);
81into_rust_by_index!(Row, i8);
82into_rust_by_index!(Row, f64);
83into_rust_by_index!(Row, f32);
84into_rust_by_index!(Row, IpAddr);
85into_rust_by_index!(Row, Uuid);
86into_rust_by_index!(Row, List);
87into_rust_by_index!(Row, Map);
88into_rust_by_index!(Row, UDT);
89into_rust_by_index!(Row, Tuple);
90into_rust_by_index!(Row, Timespec);
91into_rust_by_index!(Row, Decimal);