use std::fmt::{Display, Formatter};
use crate::{Expression, Statement};
#[derive(Clone, Debug)]
pub struct CodeBuffer {
indent: String,
line_ending: String,
code: String,
}
impl CodeBuffer {
pub const DEFAULT_INDENT: &'static str = "\t";
pub const DEFAULT_LINE_ENDING: &'static str = "\n";
pub const DEFAULT_CAPACITY: usize = 4 * 1024;
}
impl CodeBuffer {
pub fn new(indent: &str, line_ending: &str, capacity: usize) -> Self {
Self {
indent: indent.to_string(),
line_ending: line_ending.to_string(),
code: String::with_capacity(capacity),
}
}
}
impl Default for CodeBuffer {
fn default() -> Self {
Self::new(
Self::DEFAULT_INDENT,
Self::DEFAULT_LINE_ENDING,
Self::DEFAULT_CAPACITY,
)
}
}
impl CodeBuffer {
pub fn write(&mut self, code: &str) {
self.code.push_str(code);
}
pub fn indent(&mut self, level: usize) {
for _ in 0..level {
self.code.push_str(self.indent.as_mut_str());
}
}
pub fn end_line(&mut self) {
self.code.push_str(self.line_ending.as_str());
}
pub fn line(&mut self, level: usize, code: &str) {
self.indent(level);
self.write(code);
self.end_line();
}
pub fn space(&mut self) {
self.code.push_str(" ");
}
}
impl CodeBuffer {
pub fn peek(&self) -> &str {
self.code.as_str()
}
pub fn export(self) -> String {
self.code
}
pub fn clear(&mut self) {
self.code.clear();
}
}
impl CodeBuffer {
pub fn display_expression<E>(expression: &E) -> String
where
E: Expression,
{
let mut b: CodeBuffer = CodeBuffer::default();
expression.write(&mut b);
b.export()
}
pub fn display_statement<S>(statement: &S) -> String
where
S: Statement,
{
let mut b: CodeBuffer = CodeBuffer::default();
statement.write(&mut b, 0);
b.export()
}
}
impl Display for CodeBuffer {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.peek())
}
}
#[cfg(test)]
mod tests {
use crate::CodeBuffer;
#[test]
fn write_peek() {
let mut b: CodeBuffer = CodeBuffer::default();
b.indent(1);
b.write("one");
b.end_line();
b.line(2, "two");
b.space();
assert_eq!(b.peek(), "\tone\n\t\ttwo\n ");
}
#[test]
fn export() {
let mut b: CodeBuffer = CodeBuffer::default();
b.write("code");
assert_eq!(b.export(), "code");
}
#[test]
fn clear() {
let mut b: CodeBuffer = CodeBuffer::default();
b.write("code");
b.clear();
assert_eq!(b.export(), "");
}
#[test]
fn display() {
let mut b: CodeBuffer = CodeBuffer::default();
b.write("code");
assert_eq!(b.to_string(), "code");
}
}