cxx_qt_gen/generator/cpp/
utils.rs

1// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Leon Matthes <leon.matthes@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6/// A trait to allow indenting multi-line string
7/// This is specifically useful when using formatdoc! with a multi-line string argument.
8/// As the formatdoc! formatting doesn't support indenting multi-line arguments, we can indent
9/// those ourselves.
10pub(crate) trait Indent {
11    fn indented(&self, indent: usize) -> String;
12}
13
14impl Indent for str {
15    fn indented(&self, indent: usize) -> String {
16        self.lines()
17            .map(|line| " ".repeat(indent) + line)
18            .collect::<Vec<_>>()
19            .join("\n")
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    use indoc::{formatdoc, indoc};
28    use pretty_assertions::assert_str_eq;
29
30    #[test]
31    fn indent_string() {
32        let multiline_string = indoc! { r#"
33            A,
34            B,
35        "#};
36
37        assert_str_eq!(
38            formatdoc! { r#"
39            enum Test {{
40            {multiline_string}
41            }}
42        "#, multiline_string = multiline_string.indented(2) },
43            indoc! { r#"
44            enum Test {
45              A,
46              B,
47            }
48        "#}
49        );
50    }
51}