1
Fork 0

Add buffer abstraction

This commit is contained in:
Mark Rousskov 2019-08-27 17:09:13 -04:00
parent 43a5ff4222
commit f35eae9934

View file

@ -18,6 +18,71 @@ use crate::clean::{self, PrimitiveType};
use crate::html::item_type::ItemType;
use crate::html::render::{self, cache, CURRENT_DEPTH};
pub trait Print {
fn print(&self, buffer: &mut Buffer);
}
#[derive(Debug, Clone)]
pub struct Buffer {
for_html: bool,
buffer: String,
}
impl Buffer {
crate fn empty_from(v: &Buffer) -> Buffer {
Buffer {
for_html: v.for_html,
buffer: String::new(),
}
}
crate fn html() -> Buffer {
Buffer {
for_html: true,
buffer: String::new(),
}
}
crate fn is_empty(&self) -> bool {
self.buffer.is_empty()
}
crate fn into_inner(self) -> String {
self.buffer
}
crate fn insert_str(&mut self, idx: usize, s: &str) {
self.buffer.insert_str(idx, s);
}
crate fn push_str(&mut self, s: &str) {
self.buffer.push_str(s);
}
// Intended for consumption by write! and writeln! (std::fmt) but without
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
// import).
crate fn write_str(&mut self, s: &str) {
self.buffer.push_str(s);
}
// Intended for consumption by write! and writeln! (std::fmt) but without
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
// import).
crate fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
use fmt::Write;
self.buffer.write_fmt(v).unwrap();
}
crate fn display<T: fmt::Display>(&mut self, t: T) {
if self.for_html {
write!(self, "{}", t);
} else {
write!(self, "{:#}", t);
}
}
}
/// Helper to render an optional visibility with a space after it (if the
/// visibility is preset)
#[derive(Copy, Clone)]