code_gen/common/
code_buffer.rs1use std::fmt::{Display, Formatter};
2
3#[derive(Clone, Debug)]
5pub struct CodeBuffer {
6 indent: String,
7 line_ending: String,
8 code: String,
9}
10
11impl CodeBuffer {
12 pub const DEFAULT_INDENT: &'static str = " ";
16
17 pub const DEFAULT_LINE_ENDING: &'static str = "\n";
19
20 pub const DEFAULT_CAPACITY: usize = 4 * 1024;
22}
23
24impl CodeBuffer {
25 pub fn new<S0, S1>(indent: S0, line_ending: S1, capacity: usize) -> Self
29 where
30 S0: Into<String>,
31 S1: Into<String>,
32 {
33 Self {
34 indent: indent.into(),
35 line_ending: line_ending.into(),
36 code: String::with_capacity(capacity),
37 }
38 }
39}
40
41impl CodeBuffer {
42 pub fn export(self) -> String {
46 self.code
47 }
48}
49
50impl From<CodeBuffer> for String {
51 fn from(code_buffer: CodeBuffer) -> Self {
52 code_buffer.export()
53 }
54}
55
56impl Default for CodeBuffer {
57 fn default() -> Self {
58 Self::new(
59 Self::DEFAULT_INDENT,
60 Self::DEFAULT_LINE_ENDING,
61 Self::DEFAULT_CAPACITY,
62 )
63 }
64}
65
66impl CodeBuffer {
67 pub fn write(&mut self, code: &str) {
71 self.code.push_str(code);
72 }
73
74 pub fn indent(&mut self, level: usize) {
76 for _ in 0..level {
77 self.code.push_str(self.indent.as_mut_str());
78 }
79 }
80
81 pub fn end_line(&mut self) {
83 self.code.push_str(self.line_ending.as_str());
84 }
85
86 pub fn line(&mut self, level: usize, code: &str) {
88 self.indent(level);
89 self.write(code);
90 self.end_line();
91 }
92
93 pub fn space(&mut self) {
95 self.code.push_str(" ");
96 }
97}
98
99impl CodeBuffer {
100 pub fn peek(&self) -> &str {
104 self.code.as_str()
105 }
106
107 pub fn clear(&mut self) {
109 self.code.clear();
110 }
111}
112
113impl Display for CodeBuffer {
114 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
115 write!(f, "{}", self.peek())
116 }
117}