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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#![allow(clippy::assign_op_pattern)]

use fluvio_protocol::{Encoder, Decoder};

#[derive(Encoder, Decoder, Default, Debug, Eq, PartialEq, Clone)]
#[cfg_attr(
    feature = "use_serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct TableFormatSpec {
    pub name: String,
    pub input_format: Option<DataFormat>,
    pub columns: Option<Vec<TableFormatColumnConfig>>,
    #[cfg_attr(feature = "use_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub smartmodule: Option<String>,
}

impl TableFormatSpec {
    pub fn get_primary_keys(&self) -> Vec<String> {
        if let Some(columns) = &self.columns {
            let mut primary_keys = Vec::new();

            for c in columns {
                if let Some(is_primary_key) = c.primary_key {
                    if is_primary_key {
                        primary_keys.push(c.key_path.clone());
                    }
                }
            }

            primary_keys
        } else {
            Vec::new()
        }
    }
}

#[derive(Encoder, Decoder, Debug, Eq, PartialEq, Clone)]
#[cfg_attr(
    feature = "use_serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "UPPERCASE")
)]
pub enum DataFormat {
    #[fluvio(tag = 0)]
    JSON,
    //YAML,
    //TOML,
}

impl Default for DataFormat {
    fn default() -> Self {
        Self::JSON
    }
}
#[derive(Encoder, Decoder, Default, Debug, Eq, PartialEq, Clone)]
#[cfg_attr(
    feature = "use_serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "camelCase")
)]
pub struct TableFormatColumnConfig {
    pub header_label: Option<String>,
    pub width: Option<String>,
    pub alignment: Option<TableFormatAlignment>,
    pub key_path: String,
    pub format: Option<String>,
    pub display: Option<bool>,
    pub primary_key: Option<bool>,
    pub header_bg_color: Option<Color>,
    pub header_text_color: Option<Color>,
}

#[cfg_attr(
    feature = "use_serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "UPPERCASE")
)]
#[derive(Encoder, Decoder, Debug, Eq, PartialEq, Clone)]
pub enum TableFormatAlignment {
    #[fluvio(tag = 0)]
    Left,
    #[fluvio(tag = 1)]
    Right,
    #[fluvio(tag = 2)]
    Center,
}

impl Default for TableFormatAlignment {
    fn default() -> Self {
        Self::Center
    }
}

impl TableFormatColumnConfig {
    pub fn new(key_path: String) -> Self {
        Self {
            key_path,
            ..Default::default()
        }
    }

    pub fn with_primary_key(mut self, is_primary_key: Option<bool>) -> Self {
        self.primary_key = is_primary_key;
        self
    }

    pub fn with_display(mut self, do_display_column: Option<bool>) -> Self {
        self.display = do_display_column;
        self
    }

    pub fn with_header_label(mut self, header_label: Option<String>) -> Self {
        self.header_label = header_label;
        self
    }

    pub fn with_alignment(mut self, alignment: Option<TableFormatAlignment>) -> Self {
        self.alignment = alignment;
        self
    }

    pub fn with_header_text_color(mut self, header_text_color: Option<Color>) -> Self {
        self.header_text_color = header_text_color;
        self
    }

    pub fn with_header_bg_color(mut self, header_bg_color: Option<Color>) -> Self {
        self.header_bg_color = header_bg_color;
        self
    }
}

#[cfg_attr(
    feature = "use_serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "UPPERCASE")
)]
#[derive(Encoder, Decoder, Debug, Eq, PartialEq, Clone)]
pub enum Color {
    #[fluvio(tag = 0)]
    Blue,
    #[fluvio(tag = 1)]
    Yellow,
    #[fluvio(tag = 2)]
    Green,
}

impl Default for Color {
    fn default() -> Self {
        Self::Blue
    }
}