use std::fmt::{self, Write};
use crate::{Format, Formatter};
use tamacro::DisplayFromFormat;
#[derive(Debug, Clone, DisplayFromFormat)]
pub struct Comment {
pub comment: String,
pub is_heading: bool,
}
impl Comment {
pub fn new() -> CommentBuilder {
CommentBuilder::new()
}
fn push_heading(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
if self.is_heading {
writeln!(
fmt,
"{}",
"/".repeat(80usize.saturating_sub(fmt.current_indent()))
)?;
}
Ok(())
}
}
impl Format for Comment {
fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
self.push_heading(fmt)?;
for line in self.comment.lines() {
writeln!(fmt, "// {line}")?;
}
self.push_heading(fmt)
}
}
pub struct CommentBuilder {
comment: String,
is_heading: bool,
}
impl Default for CommentBuilder {
fn default() -> Self {
Self::new()
}
}
impl CommentBuilder {
pub fn new() -> Self {
Self {
comment: String::new(),
is_heading: false,
}
}
pub fn new_with_str(comment: &str) -> Self {
Self {
comment: comment.to_string(),
is_heading: false,
}
}
pub fn comment(mut self, comment: String) -> Self {
self.comment = comment;
self
}
pub fn comment_with_str(self, comment: &str) -> Self {
self.comment(comment.to_string())
}
pub fn heading(mut self, b: bool) -> Self {
self.is_heading = b;
self
}
pub fn build(self) -> Comment {
Comment {
comment: self.comment,
is_heading: self.is_heading,
}
}
}
#[derive(Debug, Clone, DisplayFromFormat)]
pub struct DocComment {
pub docs: Vec<String>,
}
impl DocComment {
pub fn new() -> DocCommentBuilder {
DocCommentBuilder::new()
}
}
impl Format for DocComment {
fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
for line in &self.docs {
writeln!(fmt, "/// {line}")?;
}
Ok(())
}
}
pub struct DocCommentBuilder {
docs: Vec<String>,
}
impl Default for DocCommentBuilder {
fn default() -> Self {
Self::new()
}
}
impl DocCommentBuilder {
pub fn new() -> Self {
Self { docs: vec![] }
}
pub fn line(self, line: String) -> Self {
self.line_str(&line)
}
pub fn line_str(mut self, line: &str) -> Self {
self.docs.push(if line.is_empty() {
String::new()
} else {
line.to_string()
});
self
}
pub fn text(self, text: String) -> Self {
self.text_str(&text)
}
pub fn text_str(self, text: &str) -> Self {
const MAX_WIDTH: usize = 80;
let mut res = self;
for line in text.lines() {
if line.chars().count() <= MAX_WIDTH {
res = res.line_str(line);
continue;
}
let mut current = String::new();
let mut current_len = 0usize;
for word in line.split(' ') {
let word_len = word.chars().count();
if current.is_empty() {
current.push_str(word);
current_len = word_len;
} else if current_len + 1 + word_len <= MAX_WIDTH {
current.push(' ');
current.push_str(word);
current_len += 1 + word_len;
} else {
res = res.line_str(¤t);
current = word.to_string();
current_len = word_len;
}
}
if !current.is_empty() {
res = res.line_str(¤t);
}
}
res
}
pub fn build(self) -> DocComment {
DocComment { docs: self.docs }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn comment() {
let mut c = CommentBuilder::new_with_str("Hello, world").build();
assert_eq!(c.to_string(), "// Hello, world\n");
c = CommentBuilder::new_with_str("abc").heading(true).build();
assert_eq!(
c.to_string(),
"////////////////////////////////////////////////////////////////////////////////\n// abc\n////////////////////////////////////////////////////////////////////////////////\n"
);
}
#[test]
fn doc_comment() {
let mut c = DocComment::new().text_str("Hello\nworld").build();
assert_eq!(c.to_string(), "/// Hello\n/// world\n");
c = DocComment::new().line_str("ABC").build();
assert_eq!(c.to_string(), "/// ABC\n");
}
#[test]
fn text_str_single_char() {
let c = DocComment::new().text_str("x").build();
assert_eq!(c.to_string(), "/// x\n");
}
#[test]
fn text_str_non_ascii() {
let c = DocComment::new().text_str("café 島津").build();
assert_eq!(c.to_string(), "/// café 島津\n");
}
#[test]
fn text_str_wraps_without_corruption() {
let long = format!("{} {}", "A".repeat(82), "B".repeat(5));
let docs = DocCommentBuilder::new().text_str(&long).build().docs;
assert_eq!(docs, vec!["A".repeat(82), "B".repeat(5)]);
}
}