use std::fmt::{self, Write};
use crate::formatter::Formatter;
#[derive(Debug, Clone)]
pub struct Comment {
comment: String,
is_heading: bool,
}
impl Comment {
pub fn new() -> Self {
Comment::with_string(String::new())
}
pub fn with_string(comment: String) -> Self {
Self {
comment,
is_heading: false,
}
}
pub fn with_str(comment: &str) -> Self {
Self::with_string(String::from(comment))
}
pub fn new_heading(comment: &str) -> Self {
Self {
comment: comment.to_string(),
is_heading: false,
}
}
pub fn set_heading(&mut self) -> &mut Self {
self.is_heading = true;
self
}
fn push_heading(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
if self.is_heading {
for _ in 0..(100 - fmt.get_indent()) {
write!(fmt, "/")?;
}
writeln!(fmt)?;
}
Ok(())
}
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
self.push_heading(fmt)?;
for line in self.comment.lines() {
writeln!(fmt, "// {line}")?;
}
self.push_heading(fmt)
}
}
impl Default for Comment {
fn default() -> Self {
Self::new()
}
}