Add buffer abstraction
This commit is contained in:
parent
43a5ff4222
commit
f35eae9934
1 changed files with 65 additions and 0 deletions
|
@ -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)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue