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
use crate::common::Identifier;
use itertools::Itertools;
use std::fmt::{Display, Formatter};

/// The data necessary to create a keyspace.
#[derive(PartialEq, Debug, Clone)]
pub struct CreateKeyspace {
    /// the name of the keyspace
    pub name: Identifier,
    /// replication strategy options.
    pub replication: Vec<(String, String)>,
    /// if specified the DURABLE WRITES option will be output.
    pub durable_writes: Option<bool>,
    /// only create if the keyspace does not exist.
    pub if_not_exists: bool,
}

impl Display for CreateKeyspace {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if let Some(durable_writes) = self.durable_writes {
            write!(
                f,
                "KEYSPACE {}{} WITH REPLICATION = {{{}}} AND DURABLE_WRITES = {}",
                if self.if_not_exists {
                    "IF NOT EXISTS "
                } else {
                    ""
                },
                self.name,
                self.replication
                    .iter()
                    .map(|(x, y)| format!("{}:{}", x, y))
                    .join(", "),
                if durable_writes { "TRUE" } else { "FALSE" }
            )
        } else {
            write!(
                f,
                "KEYSPACE {}{} WITH REPLICATION = {{{}}}",
                if self.if_not_exists {
                    "IF NOT EXISTS "
                } else {
                    ""
                },
                self.name,
                self.replication
                    .iter()
                    .map(|(x, y)| format!("{}:{}", x, y))
                    .join(", ")
            )
        }
    }
}