partiql_value/
pretty.rs

1use crate::{Bag, DateTime, Graph, List, Tuple, Value};
2use partiql_common::pretty::{
3    pretty_prefixed_doc, pretty_seq, pretty_seq_doc, pretty_surrounded, PrettyDoc,
4    PRETTY_INDENT_MINOR_NEST,
5};
6use pretty::{DocAllocator, DocBuilder};
7
8impl PrettyDoc for Value {
9    #[inline]
10    fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
11    where
12        D: DocAllocator<'b, A>,
13        D::Doc: Clone,
14        A: Clone,
15    {
16        match self {
17            Value::Null => arena.text("NULL"),
18            Value::Missing => arena.text("MISSING"),
19            Value::Boolean(inner) => arena.text(inner.to_string()),
20            Value::Integer(inner) => arena.text(inner.to_string()),
21            Value::Real(inner) => arena.text(inner.0.to_string()),
22            Value::Decimal(inner) => inner.pretty_doc(arena),
23            Value::String(inner) => inner.pretty_doc(arena),
24            Value::Blob(inner) => pretty_string(inner, arena),
25            Value::DateTime(inner) => inner.pretty_doc(arena),
26            Value::List(inner) => inner.pretty_doc(arena),
27            Value::Bag(inner) => inner.pretty_doc(arena),
28            Value::Tuple(inner) => inner.as_ref().pretty_doc(arena),
29            Value::Graph(inner) => inner.pretty_doc(arena),
30            Value::Variant(inner) => inner.pretty_doc(arena),
31        }
32    }
33}
34
35impl PrettyDoc for DateTime {
36    #[inline]
37    fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
38    where
39        D: DocAllocator<'b, A>,
40        D::Doc: Clone,
41        A: Clone,
42    {
43        match self {
44            DateTime::Date(d) => pretty_prefixed_doc("DATE", format!("'{d:?}'"), arena),
45
46            DateTime::Time(t) => pretty_prefixed_doc("TIME", format!("'{t:?}'"), arena),
47            DateTime::TimeWithTz(t, tz) => {
48                pretty_prefixed_doc("TIME WITH TIME ZONE", format!("'{t:?} {tz:?}'"), arena)
49            }
50            DateTime::Timestamp(dt) => pretty_prefixed_doc("TIMESTAMP", format!("'{dt:?}'"), arena),
51            DateTime::TimestampWithTz(dt) => {
52                pretty_prefixed_doc("TIMESTAMP WITH TIME ZONE", format!("'{dt:?}'"), arena)
53            }
54        }
55    }
56}
57
58impl PrettyDoc for List {
59    #[inline]
60    fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
61    where
62        D: DocAllocator<'b, A>,
63        D::Doc: Clone,
64        A: Clone,
65    {
66        pretty_seq(self.iter(), "[", "]", ",", PRETTY_INDENT_MINOR_NEST, arena)
67    }
68}
69
70impl PrettyDoc for Bag {
71    #[inline]
72    fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
73    where
74        D: DocAllocator<'b, A>,
75        D::Doc: Clone,
76        A: Clone,
77    {
78        pretty_seq(
79            self.iter(),
80            "<<",
81            ">>",
82            ",",
83            PRETTY_INDENT_MINOR_NEST,
84            arena,
85        )
86    }
87}
88
89impl PrettyDoc for Tuple {
90    fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
91    where
92        D: DocAllocator<'b, A>,
93        D::Doc: Clone,
94        A: Clone,
95    {
96        let seq = self.pairs().map(|(k, v)| {
97            let k = k.pretty_doc(arena);
98            let v = v.pretty_doc(arena);
99            let sep = arena.text(": ");
100
101            k.append(sep).group().append(v).group()
102        });
103        pretty_seq_doc(seq, "{", None, "}", ",", PRETTY_INDENT_MINOR_NEST, arena)
104    }
105}
106
107impl PrettyDoc for Graph {
108    fn pretty_doc<'b, D, A>(&'b self, _arena: &'b D) -> DocBuilder<'b, D, A>
109    where
110        D: DocAllocator<'b, A>,
111        D::Doc: Clone,
112        A: Clone,
113    {
114        todo!("PrettyDoc for Graph")
115    }
116}
117
118fn pretty_string<'b, P, D, A>(contents: &'b P, arena: &'b D) -> DocBuilder<'b, D, A>
119where
120    P: PrettyDoc + 'b,
121    D: DocAllocator<'b, A>,
122    D::Doc: Clone,
123    A: Clone,
124{
125    pretty_surrounded(contents, "'", "'", arena)
126}