leo_ast/
indent_display.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use std::{fmt, fmt::Write};
18
19/// Implements `Display` by putting 4 spaces in front of each line
20/// of `T`'s output.
21pub struct Indent<T>(pub T);
22
23impl<T: fmt::Display> fmt::Display for Indent<T> {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        write!(IndentWriter { f, new_line: true }, "{}", self.0)
26    }
27}
28
29const SPACES: &str = "    ";
30
31struct IndentWriter<'a, 'b> {
32    new_line: bool,
33    f: &'b mut fmt::Formatter<'a>,
34}
35
36impl Write for IndentWriter<'_, '_> {
37    fn write_str(&mut self, s: &str) -> fmt::Result {
38        let mut iter = s.lines().peekable();
39
40        while let Some(line) = iter.next() {
41            if self.new_line {
42                self.f.write_str(SPACES)?;
43            }
44            self.f.write_str(line)?;
45            if iter.peek().is_some() || s.ends_with('\n') {
46                self.f.write_str("\n")?;
47                self.new_line = true;
48            } else {
49                self.new_line = false;
50            }
51        }
52
53        Ok(())
54    }
55}