1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2024 Oxide Computer Company
use proc_macro2::TokenStream;
use quote::quote;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ExtractedDoc {
pub(crate) summary: Option<String>,
pub(crate) description: Option<String>,
}
impl ExtractedDoc {
pub(crate) fn from_attrs(attrs: &[syn::Attribute]) -> Self {
let doc = syn::Ident::new("doc", proc_macro2::Span::call_site());
let mut lines = attrs.iter().flat_map(|attr| {
if let syn::Meta::NameValue(nv) = &attr.meta {
if nv.path.is_ident(&doc) {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(s),
..
}) = &nv.value
{
return normalize_comment_string(s.value());
}
}
}
Vec::new()
});
// Skip initial blank lines; they make for excessively terse summaries.
let summary = loop {
match lines.next() {
Some(s) if s.is_empty() => (),
next => break next,
}
};
// Skip initial blank description lines.
let first = loop {
match lines.next() {
Some(s) if s.is_empty() => (),
next => break next,
}
};
let description = first.map(|first| {
lines
.fold(first, |acc, comment| {
if acc.ends_with('-')
|| acc.ends_with('\n')
|| acc.is_empty()
{
// Continuation lines and newlines.
format!("{}{}", acc, comment)
} else if comment.is_empty() {
// Blank lines get a markdown paragraph break (unless
// acc already ends in '\n' -- see above)
format!("{}\n\n", acc)
} else {
// Default to space-separating comment fragments.
format!("{} {}", acc, comment)
}
})
.trim_end()
.to_string()
});
Self { summary, description }
}
pub(crate) fn comment_text(&self, name: &str) -> String {
let mut buf = String::new();
buf.push_str("API Endpoint: ");
buf.push_str(name);
if let Some(s) = &self.summary {
buf.push_str("\n");
buf.push_str(&s);
}
if let Some(s) = &self.description {
buf.push_str("\n");
buf.push_str(&s);
}
buf
}
}
fn normalize_comment_string(s: String) -> Vec<String> {
s.split('\n')
.enumerate()
.map(|(idx, s)| {
// Rust-style comments are intrinsically single-line. We don't want
// to trim away formatting such as an initial '*'.
if idx == 0 {
s.trim_start().trim_end()
} else {
let trimmed = s.trim_start().trim_end();
trimmed.strip_prefix("* ").unwrap_or_else(|| {
trimmed.strip_prefix('*').unwrap_or(trimmed)
})
}
})
.map(ToString::to_string)
.collect()
}
pub(crate) fn string_to_doc_attrs(s: &str) -> TokenStream {
let lines = s.lines().map(|line| {
// Add a preceding space to make it look nice.
let line = format!(" {line}");
quote! {
#[doc = #line]
}
});
quote! {
#(#lines)*
}
}
#[cfg(test)]
mod tests {
use super::*;
use schema::Schema;
#[test]
fn test_extract_summary_description() {
/// Javadoc summary
/// Maybe there's another name for these...
/// ... but Java is the first place I saw these types of comments.
#[derive(Schema)]
struct JavadocComments;
assert_eq!(
ExtractedDoc::from_attrs(&JavadocComments::schema().attrs),
ExtractedDoc {
summary: Some("Javadoc summary".to_string()),
description: Some(
"Maybe there's another name for these... ... but Java \
is the first place I saw these types of comments."
.to_string()
)
},
);
/// Javadoc summary
///
/// Skip that blank.
#[derive(Schema)]
struct JavadocCommentsWithABlank;
assert_eq!(
ExtractedDoc::from_attrs(
&JavadocCommentsWithABlank::schema().attrs
),
ExtractedDoc {
summary: Some("Javadoc summary".to_string()),
description: Some("Skip that blank.".to_string())
},
);
/// Terse Javadoc summary
#[derive(Schema)]
struct JavadocCommentsTerse;
assert_eq!(
ExtractedDoc::from_attrs(&JavadocCommentsTerse::schema().attrs),
ExtractedDoc {
summary: Some("Terse Javadoc summary".to_string()),
description: None
},
);
/// Rustdoc summary
/// Did other folks do this or what this an invention I can right-
/// fully ascribe to Rust?
#[derive(Schema)]
struct RustdocComments;
assert_eq!(
ExtractedDoc::from_attrs(&RustdocComments::schema().attrs),
ExtractedDoc {
summary: Some("Rustdoc summary".to_string()),
description: Some(
"Did other folks do this or what this an invention I can \
right-fully ascribe to Rust?"
.to_string()
)
},
);
/// Rustdoc summary
///
/// Skip that blank.
#[derive(Schema)]
struct RustdocCommentsWithABlank;
assert_eq!(
ExtractedDoc::from_attrs(
&RustdocCommentsWithABlank::schema().attrs
),
ExtractedDoc {
summary: Some("Rustdoc summary".to_string()),
description: Some("Skip that blank.".to_string())
},
);
/// Just a Rustdoc summary
#[derive(Schema)]
struct JustTheRustdocSummary;
assert_eq!(
ExtractedDoc::from_attrs(&JustTheRustdocSummary::schema().attrs),
ExtractedDoc {
summary: Some("Just a Rustdoc summary".to_string()),
description: None
},
);
/// Just a Javadoc summary
#[derive(Schema)]
struct JustTheJavadocSummary;
assert_eq!(
ExtractedDoc::from_attrs(&JustTheJavadocSummary::schema().attrs),
ExtractedDoc {
summary: Some("Just a Javadoc summary".to_string()),
description: None
},
);
/// Summary
/// Text
/// More
///
/// Even
/// More
///
///
///
/// And another
/// paragraph
#[derive(Schema)]
struct SummaryDescriptionBreak;
assert_eq!(
ExtractedDoc::from_attrs(&SummaryDescriptionBreak::schema().attrs),
ExtractedDoc {
summary: Some("Summary".to_string()),
description: Some(
"Text More\n\nEven More\n\nAnd another paragraph"
.to_string()
)
},
);
}
}