1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
mod pretty_print; pub use self::pretty_print::PrettyPrint; use crate::FormatterConfig; use std::{ fmt::{self, Write}, mem::swap, }; #[derive(Debug, Clone)] pub struct FluentFormatter { config: FormatterConfig, indent: String, buffer: String, } impl Default for FluentFormatter { fn default() -> Self { Self { config: FormatterConfig::default(), indent: String::new(), buffer: String::new() } } } impl Write for FluentFormatter { fn write_str(&mut self, s: &str) -> fmt::Result { write!(self.buffer, "{}", s) } } impl FluentFormatter { pub fn get_buffer(&self) -> &str { self.buffer.as_str() } pub fn swap_indent(&mut self, rhs: &mut String) { swap(&mut self.indent, rhs) } pub fn write_new_indent<T: PrettyPrint>(&mut self, body: &T, indent: &str) -> fmt::Result { let mut indent = self.indent.to_owned() + indent; self.swap_indent(&mut indent); body.pretty_print(self)?; self.swap_indent(&mut indent); Ok(()) } pub fn write_with_indent(&mut self, line: &str) { self.buffer.push_str(&self.indent); self.buffer.push_str(line); } pub fn write_new_line(&mut self) { self.buffer.push('\n') } pub fn tab(&self) -> String { match self.config.indent_use_space { true => " ".repeat(self.config.indent_size as usize), false => String::from("\t"), } } pub fn clear(&mut self) { self.indent.clear(); self.buffer.clear(); } pub fn pretty_print(&mut self, text: impl PrettyPrint) -> Result<&str, fmt::Error> { self.clear(); text.pretty_print(self)?; Ok(self.get_buffer()) } }