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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::io::Write;

use seaplane::api::metadata::v1::KeyValue as KeyValueModel;
use serde::Serialize;
use tabwriter::TabWriter;
use unicode_segmentation::UnicodeSegmentation;

use crate::{
    context::Ctx,
    error::{CliError, Result},
    ops::EncodedString,
    printer::{printer, Output},
};

/// We use our own KeyValue instead of the models because we need to *not* enforce base64 encoding,
/// and implement a bunch of additional methods and traits that wouldn't make sense for the models
///
/// We also need to keep track if the values are encoded or not
#[derive(Debug, Default, Clone, Serialize)]
pub struct KeyValue {
    pub key: EncodedString,
    pub value: EncodedString,
}

impl KeyValue {
    /// Creates a new KeyValue from an encoded key and value. You must pinky promise the key and
    /// value are URL safe base64 encoded or Bad Things may happen.
    pub fn new<S: Into<String>>(key: S, value: S) -> Self {
        Self { key: EncodedString::new(key.into()), value: EncodedString::new(value.into()) }
    }

    /// Creates a new KeyValue from an un-encoded key and value, encoding them along the way
    pub fn new_unencoded<S: AsRef<str>>(key: S, value: S) -> Self {
        Self::new(
            base64::encode_config(key.as_ref(), base64::URL_SAFE_NO_PAD),
            base64::encode_config(value.as_ref(), base64::URL_SAFE_NO_PAD),
        )
    }

    /// Creates a new KeyValue from an un-encoded string ref, encoding it along the way
    pub fn from_key_unencoded<S: AsRef<str>>(key: S) -> Self {
        Self::from_key(base64::encode_config(key.as_ref(), base64::URL_SAFE_NO_PAD))
    }

    /// Creates a new KeyValue from an already encoded string ref. You must pinky promise the key
    /// is URL safe base64 encoded or Bad Things may happen.
    pub fn from_key<S: Into<String>>(key: S) -> Self {
        Self { key: EncodedString::new(key.into()), ..Self::default() }
    }

    /// Sets the value to some base64 encoded value
    pub fn set_value<S: Into<String>>(&mut self, value: S) {
        self.value = EncodedString::new(value.into())
    }
}

#[derive(Debug, Default, Clone, Serialize)]
#[serde(transparent)]
pub struct KeyValues {
    inner: Vec<KeyValue>,
}

impl KeyValues {
    pub fn from_model(model: Vec<KeyValueModel>) -> Self {
        Self {
            inner: model
                .iter()
                .map(|kv| KeyValue::new(kv.key.as_ref(), kv.value.as_ref()))
                .collect(),
        }
    }

    /// Inserts an already base64 encoded key and value
    pub fn insert<S: Into<String>>(&mut self, key: S, value: S) {
        self.inner.push(KeyValue::new(key, value));
    }

    pub fn iter(&self) -> impl Iterator<Item = &KeyValue> { self.inner.iter() }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut KeyValue> { self.inner.iter_mut() }

    pub fn push(&mut self, kv: KeyValue) { self.inner.push(kv) }

    pub fn keys(&self) -> impl Iterator<Item = EncodedString> + '_ {
        self.inner.iter().map(|kv| kv.key.clone())
    }
}

impl Output for KeyValues {
    fn print_json(&self, _ctx: &Ctx) -> Result<()> {
        cli_println!("{}", serde_json::to_string(self)?);
        Ok(())
    }

    fn print_table(&self, ctx: &Ctx) -> Result<()> {
        let mdctx = ctx.md_ctx.get_or_init();
        let headers = !mdctx.no_header;
        let decode = mdctx.decode;
        let decode_safe = mdctx.decode_safe;
        let only_keys = mdctx.no_values;
        let only_values = mdctx.no_keys;
        let keys_width_limit = mdctx.keys_width_limit;
        let values_width_limit = mdctx.values_width_limit;

        let mut tw = TabWriter::new(Vec::new());

        if headers {
            match [only_keys, only_values] {
                [true, false] => writeln!(tw, "KEY")?,
                [false, true] => writeln!(tw, "VALUE")?,
                [..] => writeln!(tw, "KEY\tVALUE")?,
            }
        }

        // Truncate a binary vector to a specific length in bytes.
        // Appends ellipsis `…` if the vector was truncated
        fn truncate_vec(vec: Vec<u8>, max: usize) -> Vec<u8> {
            if max == 0 || vec.len() <= max {
                vec
            } else {
                let mut vec = vec;
                vec.truncate(max);
                vec.extend_from_slice("…".as_bytes());
                vec
            }
        }

        // Truncate a unicode string to a specific number of grapheme clusters.
        // Appends ellipsis `…` if the string was truncated
        fn truncate_string(str: String, max: usize) -> String {
            if max == 0 || str.graphemes(true).count() <= max {
                str
            } else {
                let mut s: String = str.graphemes(true).take(max).collect();
                s.push('…');
                s
            }
        }

        for kv in self.iter() {
            if !only_values {
                if decode {
                    tw.write_all(&truncate_vec(kv.key.decoded()?, keys_width_limit))?;
                } else if decode_safe {
                    write!(tw, "{}", truncate_string(kv.key.decoded_safe()?, keys_width_limit))?;
                } else {
                    write!(tw, "{}", truncate_string(kv.key.to_string(), keys_width_limit))?;
                }

                if !only_keys {
                    tw.write_all(b"\t")?
                };
            };

            if !only_keys {
                if decode {
                    tw.write_all(&truncate_vec(kv.value.decoded()?, values_width_limit))?;
                } else if decode_safe {
                    write!(
                        tw,
                        "{}",
                        truncate_string(kv.value.decoded_safe()?, values_width_limit)
                    )?;
                } else {
                    write!(tw, "{}", truncate_string(kv.value.to_string(), values_width_limit))?;
                }
            };
            writeln!(tw)?;
        }
        tw.flush()?;

        let mut ptr = printer();
        let page = tw
            .into_inner()
            .map_err(|_| CliError::bail("IO flush error writing key-values"))?;
        ptr.write_all(&page)?;
        ptr.flush()?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn build_kvs() -> KeyValues {
        KeyValues {
            inner: vec![
                KeyValue {
                    key: EncodedString::new("a2V5MQ".into()),
                    value: EncodedString::new("dmFsdWUx".into()),
                },
                KeyValue {
                    key: EncodedString::new("a2V5Mg".into()),
                    value: EncodedString::new("dmFsdWUy".into()),
                },
                KeyValue {
                    key: EncodedString::new("a2V5Mw".into()),
                    value: EncodedString::new("dmFsdWUz".into()),
                },
            ],
        }
    }

    #[test]
    fn serialize_keyvalues_base64() {
        let kvs = build_kvs();

        assert_eq!(
            serde_json::to_string(&kvs).unwrap(),
            json!([{"key": "a2V5MQ", "value": "dmFsdWUx"}, {"key": "a2V5Mg", "value": "dmFsdWUy"}, {"key": "a2V5Mw", "value": "dmFsdWUz"}]).to_string()
        );
    }
}