/ compiler / ast / src / indent_display.rs
indent_display.rs
 1  // Copyright (C) 2019-2025 ADnet Contributors
 2  // This file is part of the ADL library.
 3  
 4  // The ADL 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 ADL 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 ADL library. If not, see <https://www.gnu.org/licenses/>.
16  
17  use std::{fmt, fmt::Write};
18  
19  /// Implements `Display` by putting 4 spaces in front of each line
20  /// of `T`'s output.
21  pub struct Indent<T>(pub T);
22  
23  impl<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  
29  const SPACES: &str = "    ";
30  
31  struct IndentWriter<'a, 'b> {
32      new_line: bool,
33      f: &'b mut fmt::Formatter<'a>,
34  }
35  
36  impl 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  }