cassandra_protocol/types/
vector.rs

1use crate::error::{Error, Result};
2use crate::frame::message_result::{ColType, ColTypeOption, ColTypeOptionValue};
3use crate::frame::Version;
4use crate::types::data_serialization_types::*;
5use crate::types::{AsRust, AsRustType, CBytes};
6use derive_more::Constructor;
7use itertools::Itertools;
8
9// TODO: consider using pointers to ColTypeOption and Vec<CBytes> instead of owning them.
10#[derive(Debug, Constructor)]
11pub struct Vector {
12    /// column spec of the list, i.e. id should be List as it's a list and value should contain
13    /// a type of list items.
14    metadata: ColTypeOption,
15    data: Vec<CBytes>,
16    protocol_version: Version,
17}
18
19impl Vector {
20    fn try_map<T, F>(&self, f: F) -> Result<Vec<T>>
21    where
22        F: FnMut(&CBytes) -> Result<T>,
23    {
24        self.data.iter().map(f).try_collect()
25    }
26}
27
28pub struct VectorInfo {
29    pub internal_type: String,
30    pub count: usize,
31}
32
33pub fn get_vector_type_info(option_value: &ColTypeOptionValue) -> Result<VectorInfo> {
34    let input = match option_value {
35        ColTypeOptionValue::CString(ref s) => s,
36        _ => return Err(Error::General("Option value must be a string!".into())),
37    };
38
39    let _custom_type = input.split('(').next().unwrap().rsplit('.').next().unwrap();
40
41    let vector_type = input
42        .split('(')
43        .nth(1)
44        .and_then(|s| s.split(',').next())
45        .and_then(|s| s.rsplit('.').next())
46        .map(|s| s.trim())
47        .ok_or_else(|| Error::General("Cannot parse vector type!".into()))?;
48
49    let count: usize = input
50        .split('(')
51        .nth(1)
52        .and_then(|s| s.rsplit(',').next())
53        .and_then(|s| s.split(')').next())
54        .map(|s| s.trim().parse())
55        .transpose()
56        .map_err(|_| Error::General("Cannot parse vector count!".to_string()))?
57        .ok_or_else(|| Error::General("Cannot parse vector count!".into()))?;
58
59    Ok(VectorInfo {
60        internal_type: vector_type.to_string(),
61        count,
62    })
63}
64
65impl AsRust for Vector {}
66
67vector_as_rust!(f32);
68
69vector_as_cassandra_type!();