mz_sql_pretty/
util.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Utility functions used by the doc functions.
11
12use pretty::{Doc, RcDoc};
13
14use crate::TAB;
15
16pub(crate) fn intersperse_line_nest<'a, I>(v: I) -> RcDoc<'a>
17where
18    I: IntoIterator<Item = RcDoc<'a, ()>>,
19{
20    RcDoc::intersperse(v, Doc::line()).nest(TAB).group()
21}
22
23pub(crate) fn nest<'a>(title: RcDoc<'a>, v: RcDoc<'a>) -> RcDoc<'a> {
24    intersperse_line_nest([title, v])
25}
26
27pub(crate) fn nest_title<S>(title: S, v: RcDoc) -> RcDoc
28where
29    S: Into<String>,
30{
31    nest(RcDoc::text(title.into()), v)
32}
33
34pub(crate) fn title_comma_separate<'a, F, T, S>(title: S, f: F, v: &'a [T]) -> RcDoc<'a, ()>
35where
36    F: Fn(&'a T) -> RcDoc<'a>,
37    S: Into<String>,
38{
39    let title = RcDoc::text(title.into());
40    if v.is_empty() {
41        title
42    } else {
43        nest_comma_separate(title, f, v)
44    }
45}
46
47pub(crate) fn nest_comma_separate<'a, F, T: 'a, I>(
48    title: RcDoc<'a, ()>,
49    f: F,
50    v: I,
51) -> RcDoc<'a, ()>
52where
53    F: Fn(&'a T) -> RcDoc<'a>,
54    I: IntoIterator<Item = &'a T>,
55{
56    nest(title, comma_separate(f, v))
57}
58
59pub(crate) fn comma_separate<'a, F, T: 'a, I>(f: F, v: I) -> RcDoc<'a, ()>
60where
61    F: Fn(&'a T) -> RcDoc<'a>,
62    I: IntoIterator<Item = &'a T>,
63{
64    let docs = v.into_iter().map(f);
65    comma_separated(docs)
66}
67
68pub(crate) fn comma_separated<'a, I>(v: I) -> RcDoc<'a, ()>
69where
70    I: IntoIterator<Item = RcDoc<'a, ()>>,
71{
72    RcDoc::intersperse(v, RcDoc::concat([RcDoc::text(","), RcDoc::line()])).group()
73}
74
75pub(crate) fn bracket<A: Into<String>, B: Into<String>>(left: A, d: RcDoc, right: B) -> RcDoc {
76    bracket_doc(
77        RcDoc::text(left.into()),
78        d,
79        RcDoc::text(right.into()),
80        RcDoc::line_(),
81    )
82}
83
84pub(crate) fn bracket_doc<'a>(
85    left: RcDoc<'a>,
86    d: RcDoc<'a>,
87    right: RcDoc<'a>,
88    line: RcDoc<'a>,
89) -> RcDoc<'a> {
90    RcDoc::concat([
91        left,
92        RcDoc::concat([line.clone(), d]).nest(TAB),
93        line,
94        right,
95    ])
96    .group()
97}