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
use std::{collections::BTreeMap, slice};

use serde::{Deserialize, Serialize};

use crate::cql::schema::Column;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Table {
    pub keyspace: String,
    pub name: String,
    pub schema: TableSchema,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TableSchema {
    pub columns: BTreeMap<String, Column>,
    pub partition_key: PrimaryKey,
    pub clustering_key: PrimaryKey,
    pub partitioner: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PrimaryKey {
    Empty,
    Simple(String),
    Composite(Vec<String>),
}

impl PrimaryKey {
    pub fn count(&self) -> usize {
        match self {
            PrimaryKey::Empty => 0,
            PrimaryKey::Simple(_) => 1,
            PrimaryKey::Composite(v) => v.len(),
        }
    }
    pub fn from_definition(mut names: Vec<String>) -> PrimaryKey {
        match names.len() {
            0 => PrimaryKey::Empty,
            1 => PrimaryKey::Simple(names.pop().unwrap()),
            _ => PrimaryKey::Composite(names),
        }
    }
}

impl<'a> IntoIterator for &'a PrimaryKey {
    type Item = &'a String;
    type IntoIter = slice::Iter<'a, String>;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            PrimaryKey::Empty => slice::Iter::default(),
            PrimaryKey::Simple(v) => slice::from_ref(v).iter(),
            PrimaryKey::Composite(v) => v.iter(),
        }
    }
}