openapi_nexus_go/ast/common/
go_doc_comment.rs1use pretty::RcDoc;
4use serde::{Deserialize, Serialize};
5
6use crate::consts::MAX_LINE_WIDTH;
7use openapi_nexus_core::traits::ToRcDoc;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct GoDocComment(pub String);
12
13impl GoDocComment {
14 pub fn new(comment: String) -> Self {
15 Self(comment)
16 }
17}
18
19impl ToRcDoc for GoDocComment {
20 fn to_rcdoc(&self) -> RcDoc<'static, ()> {
21 let lines: Vec<&str> = self.0.lines().collect();
22 if lines.is_empty() {
23 return RcDoc::nil();
24 }
25
26 if lines.len() == 1 && lines[0].len() + 3 <= MAX_LINE_WIDTH {
27 RcDoc::text(format!("// {}", lines[0]))
29 } else {
30 let mut parts = vec![RcDoc::text("//")];
32 for line in lines {
33 parts.push(RcDoc::hardline());
34 if line.is_empty() {
35 parts.push(RcDoc::text("//"));
36 } else {
37 parts.push(RcDoc::text(format!("// {}", line)));
38 }
39 }
40 RcDoc::concat(parts)
41 }
42 }
43}