scylla/utils/
pretty.rs

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use chrono::{LocalResult, TimeZone, Utc};
use scylla_cql::frame::{
    response::result::CqlValue,
    value::{CqlDate, CqlTime, CqlTimestamp},
};

use std::borrow::Borrow;
use std::fmt::{Display, LowerHex, UpperHex};

pub(crate) struct HexBytes<'a>(pub(crate) &'a [u8]);

impl<'a> LowerHex for HexBytes<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for b in self.0 {
            write!(f, "{:02x}", b)?;
        }
        Ok(())
    }
}

impl<'a> UpperHex for HexBytes<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for b in self.0 {
            write!(f, "{:02X}", b)?;
        }
        Ok(())
    }
}

// Displays a CqlValue. The syntax should resemble the CQL literal syntax
// (but no guarantee is given that it's always the same).
pub(crate) struct CqlValueDisplayer<C>(pub(crate) C);

impl<C> Display for CqlValueDisplayer<C>
where
    C: Borrow<CqlValue>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0.borrow() {
            // Scalar types
            CqlValue::Ascii(a) => write!(f, "{}", CqlStringLiteralDisplayer(a))?,
            CqlValue::Text(t) => write!(f, "{}", CqlStringLiteralDisplayer(t))?,
            CqlValue::Blob(b) => write!(f, "0x{:x}", HexBytes(b))?,
            CqlValue::Empty => write!(f, "0x")?,
            CqlValue::Decimal(d) => {
                let (bytes, scale) = d.as_signed_be_bytes_slice_and_exponent();
                write!(
                    f,
                    "blobAsDecimal(0x{:x}{:x})",
                    HexBytes(&scale.to_be_bytes()),
                    HexBytes(bytes)
                )?
            }
            CqlValue::Float(fl) => write!(f, "{}", fl)?,
            CqlValue::Double(d) => write!(f, "{}", d)?,
            CqlValue::Boolean(b) => write!(f, "{}", b)?,
            CqlValue::Int(i) => write!(f, "{}", i)?,
            CqlValue::BigInt(bi) => write!(f, "{}", bi)?,
            CqlValue::Inet(i) => write!(f, "'{}'", i)?,
            CqlValue::SmallInt(si) => write!(f, "{}", si)?,
            CqlValue::TinyInt(ti) => write!(f, "{}", ti)?,
            CqlValue::Varint(vi) => write!(
                f,
                "blobAsVarint(0x{:x})",
                HexBytes(vi.as_signed_bytes_be_slice())
            )?,
            CqlValue::Counter(c) => write!(f, "{}", c.0)?,
            CqlValue::Date(CqlDate(d)) => {
                let days_since_epoch = chrono::Duration::days(*d as i64 - (1 << 31));

                // TODO: chrono::NaiveDate does not handle the whole range
                // supported by the `date` datatype
                match chrono::NaiveDate::from_ymd_opt(1970, 1, 1)
                    .unwrap()
                    .checked_add_signed(days_since_epoch)
                {
                    Some(d) => write!(f, "'{}'", d)?,
                    None => f.write_str("<date out of representable range>")?,
                }
            }
            CqlValue::Duration(d) => write!(f, "{}mo{}d{}ns", d.months, d.days, d.nanoseconds)?,
            CqlValue::Time(CqlTime(t)) => {
                write!(
                    f,
                    "'{:02}:{:02}:{:02}.{:09}'",
                    t / 3_600_000_000_000,
                    t / 60_000_000_000 % 60,
                    t / 1_000_000_000 % 60,
                    t % 1_000_000_000,
                )?;
            }
            CqlValue::Timestamp(CqlTimestamp(t)) => {
                match Utc.timestamp_millis_opt(*t) {
                    LocalResult::Ambiguous(_, _) => unreachable!(), // not supposed to happen with timestamp_millis_opt
                    LocalResult::Single(d) => {
                        write!(f, "{}", d.format("'%Y-%m-%d %H:%M:%S%.3f%z'"))?
                    }
                    LocalResult::None => f.write_str("<timestamp out of representable range>")?,
                }
            }
            CqlValue::Timeuuid(t) => write!(f, "{}", t)?,
            CqlValue::Uuid(u) => write!(f, "{}", u)?,

            // Compound types
            CqlValue::Tuple(t) => {
                f.write_str("(")?;
                CommaSeparatedDisplayer(
                    t.iter()
                        .map(|x| MaybeNullDisplayer(x.as_ref().map(CqlValueDisplayer))),
                )
                .fmt(f)?;
                f.write_str(")")?;
            }
            CqlValue::List(v) => {
                f.write_str("[")?;
                CommaSeparatedDisplayer(v.iter().map(CqlValueDisplayer)).fmt(f)?;
                f.write_str("]")?;
            }
            CqlValue::Set(v) => {
                f.write_str("{")?;
                CommaSeparatedDisplayer(v.iter().map(CqlValueDisplayer)).fmt(f)?;
                f.write_str("}")?;
            }
            CqlValue::Map(m) => {
                f.write_str("{")?;
                CommaSeparatedDisplayer(
                    m.iter()
                        .map(|(k, v)| PairDisplayer(CqlValueDisplayer(k), CqlValueDisplayer(v))),
                )
                .fmt(f)?;
                f.write_str("}")?;
            }
            CqlValue::UserDefinedType {
                keyspace: _,
                type_name: _,
                fields,
            } => {
                f.write_str("{")?;
                CommaSeparatedDisplayer(fields.iter().map(|(k, v)| {
                    PairDisplayer(k, MaybeNullDisplayer(v.as_ref().map(CqlValueDisplayer)))
                }))
                .fmt(f)?;
                f.write_str("}")?;
            }
        }
        Ok(())
    }
}

pub(crate) struct CqlStringLiteralDisplayer<'a>(&'a str);

impl<'a> Display for CqlStringLiteralDisplayer<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // CQL string literals use single quotes. The only character that
        // needs escaping is singular quote, and escaping is done by repeating
        // the quote character.
        f.write_str("'")?;
        let mut first = true;
        for part in self.0.split('\'') {
            if first {
                first = false;
            } else {
                f.write_str("''")?;
            }
            f.write_str(part)?;
        }
        f.write_str("'")?;
        Ok(())
    }
}

pub(crate) struct CommaSeparatedDisplayer<I>(pub(crate) I);

impl<I, T> Display for CommaSeparatedDisplayer<I>
where
    I: Iterator<Item = T> + Clone,
    T: Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut first = true;
        for t in self.0.clone() {
            if first {
                first = false;
            } else {
                f.write_str(",")?;
            }
            write!(f, "{}", t)?;
        }
        Ok(())
    }
}

pub(crate) struct PairDisplayer<K, V>(K, V);

impl<K, V> Display for PairDisplayer<K, V>
where
    K: Display,
    V: Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.0, self.1)
    }
}

pub(crate) struct MaybeNullDisplayer<T>(Option<T>);

impl<T> Display for MaybeNullDisplayer<T>
where
    T: Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.0 {
            None => write!(f, "null")?,
            Some(v) => write!(f, "{}", v)?,
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use chrono::{NaiveDate, NaiveDateTime, NaiveTime};

    use scylla_cql::frame::response::result::CqlValue;
    use scylla_cql::frame::value::{CqlDate, CqlDecimal, CqlDuration, CqlTime, CqlTimestamp};

    use crate::utils::pretty::CqlValueDisplayer;

    #[test]
    fn test_cql_value_displayer() {
        assert_eq!(
            format!("{}", CqlValueDisplayer(CqlValue::Boolean(true))),
            "true"
        );
        assert_eq!(format!("{}", CqlValueDisplayer(CqlValue::Int(123))), "123");
        assert_eq!(
            format!(
                "{}",
                // 123.456
                CqlValueDisplayer(CqlValue::Decimal(
                    CqlDecimal::from_signed_be_bytes_and_exponent(vec![0x01, 0xE2, 0x40], 3)
                ))
            ),
            "blobAsDecimal(0x0000000301e240)"
        );
        assert_eq!(
            format!("{}", CqlValueDisplayer(CqlValue::Float(12.75))),
            "12.75"
        );
        assert_eq!(
            format!(
                "{}",
                CqlValueDisplayer(CqlValue::Text("Ala ma kota".to_owned()))
            ),
            "'Ala ma kota'"
        );
        assert_eq!(
            format!("{}", CqlValueDisplayer(CqlValue::Text("Foo's".to_owned()))),
            "'Foo''s'"
        );

        // Time types are the most tricky
        assert_eq!(
            format!(
                "{}",
                CqlValueDisplayer(CqlValue::Date(CqlDate(40 + (1 << 31))))
            ),
            "'1970-02-10'"
        );
        assert_eq!(
            format!(
                "{}",
                CqlValueDisplayer(CqlValue::Duration(CqlDuration {
                    months: 1,
                    days: 2,
                    nanoseconds: 3,
                }))
            ),
            "1mo2d3ns"
        );
        let t = chrono::NaiveTime::from_hms_nano_opt(6, 5, 4, 123)
            .unwrap()
            .signed_duration_since(chrono::NaiveTime::MIN);
        let t = t.num_nanoseconds().unwrap();
        assert_eq!(
            format!("{}", CqlValueDisplayer(CqlValue::Time(CqlTime(t)))),
            "'06:05:04.000000123'"
        );

        let t = NaiveDate::from_ymd_opt(2005, 4, 2)
            .unwrap()
            .and_time(NaiveTime::from_hms_opt(19, 37, 42).unwrap());
        assert_eq!(
            format!(
                "{}",
                CqlValueDisplayer(CqlValue::Timestamp(CqlTimestamp(
                    t.signed_duration_since(NaiveDateTime::default())
                        .num_milliseconds()
                )))
            ),
            "'2005-04-02 19:37:42.000+0000'"
        );

        // Compound types
        let list_or_set = vec![CqlValue::Int(1), CqlValue::Int(3), CqlValue::Int(2)];
        assert_eq!(
            format!("{}", CqlValueDisplayer(CqlValue::List(list_or_set.clone()))),
            "[1,3,2]"
        );
        assert_eq!(
            format!("{}", CqlValueDisplayer(CqlValue::Set(list_or_set.clone()))),
            "{1,3,2}"
        );

        let tuple: Vec<_> = list_or_set
            .into_iter()
            .map(Some)
            .chain(std::iter::once(None))
            .collect();
        assert_eq!(
            format!("{}", CqlValueDisplayer(CqlValue::Tuple(tuple))),
            "(1,3,2,null)"
        );

        let map = vec![
            (CqlValue::Text("foo".to_owned()), CqlValue::Int(123)),
            (CqlValue::Text("bar".to_owned()), CqlValue::Int(321)),
        ];
        assert_eq!(
            format!("{}", CqlValueDisplayer(CqlValue::Map(map))),
            "{'foo':123,'bar':321}"
        );

        let fields = vec![
            ("foo".to_owned(), Some(CqlValue::Int(123))),
            ("bar".to_owned(), Some(CqlValue::Int(321))),
        ];
        assert_eq!(
            format!(
                "{}",
                CqlValueDisplayer(CqlValue::UserDefinedType {
                    keyspace: "ks".to_owned(),
                    type_name: "typ".to_owned(),
                    fields,
                })
            ),
            "{foo:123,bar:321}"
        );
    }
}