1use crate::{
2 CONTRACT_INHERITANCE_ID, CommentTag, Comments, CommentsRef, DEPLOYMENTS_ID, Document,
3 GIT_SOURCE_ID, INHERITDOC_ID, Markdown, PreprocessorOutput,
4 document::{DocumentContent, read_context},
5 helpers::function_signature,
6 parser::ParseSource,
7 solang_ext::SafeUnwrap,
8 writer::BufWriter,
9};
10use itertools::Itertools;
11use solang_parser::pt::{Base, FunctionDefinition};
12use std::path::Path;
13
14pub type AsDocResult = Result<String, std::fmt::Error>;
16
17pub trait AsDoc {
19 fn as_doc(&self) -> AsDocResult;
21}
22
23impl AsDoc for String {
24 fn as_doc(&self) -> AsDocResult {
25 Ok(self.to_owned())
26 }
27}
28
29impl AsDoc for Comments {
30 fn as_doc(&self) -> AsDocResult {
31 CommentsRef::from(self).as_doc()
32 }
33}
34
35impl AsDoc for CommentsRef<'_> {
36 fn as_doc(&self) -> AsDocResult {
38 let mut writer = BufWriter::default();
39
40 let titles = self.include_tag(CommentTag::Title);
42 if !titles.is_empty() {
43 writer.write_bold(&format!("Title{}:", if titles.len() == 1 { "" } else { "s" }))?;
44 writer.writeln_raw(titles.iter().map(|t| &t.value).join(", "))?;
45 writer.writeln()?;
46 }
47
48 let authors = self.include_tag(CommentTag::Author);
50 if !authors.is_empty() {
51 writer.write_bold(&format!("Author{}:", if authors.len() == 1 { "" } else { "s" }))?;
52 writer.writeln_raw(authors.iter().map(|a| &a.value).join(", "))?;
53 writer.writeln()?;
54 }
55
56 let notices = self.include_tag(CommentTag::Notice);
58 for n in notices.iter() {
59 writer.writeln_raw(&n.value)?;
60 writer.writeln()?;
61 }
62
63 let devs = self.include_tag(CommentTag::Dev);
65 for d in devs.iter() {
66 writer.write_dev_content(&d.value)?;
67 writer.writeln()?;
68 }
69
70 let customs = self.get_custom_tags();
72 if !customs.is_empty() {
73 writer.write_bold(&format!("Note{}:", if customs.len() == 1 { "" } else { "s" }))?;
74 for c in customs.iter() {
75 writer.writeln_raw(format!(
76 "{}{}: {}",
77 if customs.len() == 1 { "" } else { "- " },
78 &c.tag,
79 &c.value
80 ))?;
81 writer.writeln()?;
82 }
83 }
84
85 Ok(writer.finish())
86 }
87}
88
89impl AsDoc for Base {
90 fn as_doc(&self) -> AsDocResult {
91 Ok(self.name.identifiers.iter().map(|ident| ident.name.to_owned()).join("."))
92 }
93}
94
95impl AsDoc for Document {
96 fn as_doc(&self) -> AsDocResult {
97 let mut writer = BufWriter::default();
98
99 match &self.content {
100 DocumentContent::OverloadedFunctions(items) => {
101 writer
102 .write_title(&format!("function {}", items.first().unwrap().source.ident()))?;
103 if let Some(git_source) = read_context!(self, GIT_SOURCE_ID, GitSource) {
104 writer.write_link("Git Source", &git_source)?;
105 writer.writeln()?;
106 }
107
108 for item in items {
109 let func = item.as_function().unwrap();
110 let heading = function_signature(func).replace(',', ", ");
111 writer.write_heading(&heading)?;
112 writer.write_section(&item.comments, &item.code)?;
113 }
114 }
115 DocumentContent::Constants(items) => {
116 writer.write_title("Constants")?;
117 if let Some(git_source) = read_context!(self, GIT_SOURCE_ID, GitSource) {
118 writer.write_link("Git Source", &git_source)?;
119 writer.writeln()?;
120 }
121
122 for item in items {
123 let var = item.as_variable().unwrap();
124 writer.write_heading(&var.name.safe_unwrap().name)?;
125 writer.write_section(&item.comments, &item.code)?;
126 }
127 }
128 DocumentContent::Single(item) => {
129 writer.write_title(&item.source.ident())?;
130 if let Some(git_source) = read_context!(self, GIT_SOURCE_ID, GitSource) {
131 writer.write_link("Git Source", &git_source)?;
132 writer.writeln()?;
133 }
134
135 if let Some(deployments) = read_context!(self, DEPLOYMENTS_ID, Deployments) {
136 writer.write_deployments_table(deployments)?;
137 }
138
139 match &item.source {
140 ParseSource::Contract(contract) => {
141 if !contract.base.is_empty() {
142 writer.write_bold("Inherits:")?;
143
144 let mut bases = vec![];
145 let linked =
146 read_context!(self, CONTRACT_INHERITANCE_ID, ContractInheritance);
147 for base in &contract.base {
148 let base_doc = base.as_doc()?;
149 let base_ident = &base.name.identifiers.last().unwrap().name;
150
151 let link = linked
152 .as_ref()
153 .and_then(|link| {
154 link.get(base_ident).map(|path| {
155 let path = if cfg!(windows) {
156 Path::new("\\").join(path)
157 } else {
158 Path::new("/").join(path)
159 };
160 Markdown::Link(&base_doc, &path.display().to_string())
161 .as_doc()
162 })
163 })
164 .transpose()?
165 .unwrap_or(base_doc);
166
167 bases.push(link);
168 }
169
170 writer.writeln_raw(bases.join(", "))?;
171 writer.writeln()?;
172 }
173
174 writer.writeln_doc(&item.comments)?;
175
176 if let Some(state_vars) = item.variables() {
177 writer.write_subtitle("State Variables")?;
178 state_vars.into_iter().try_for_each(|(item, comments, code)| {
179 let comments = comments.merge_inheritdoc(
180 &item.name.safe_unwrap().name,
181 read_context!(self, INHERITDOC_ID, Inheritdoc),
182 );
183
184 writer.write_heading(&item.name.safe_unwrap().name)?;
185 writer.write_section(&comments, code)?;
186 writer.writeln()
187 })?;
188 }
189
190 if let Some(funcs) = item.functions() {
191 writer.write_subtitle("Functions")?;
192
193 for (func, comments, code) in &funcs {
194 self.write_function(&mut writer, func, comments, code)?;
195 }
196 }
197
198 if let Some(events) = item.events() {
199 writer.write_subtitle("Events")?;
200 events.into_iter().try_for_each(|(item, comments, code)| {
201 writer.write_heading(&item.name.safe_unwrap().name)?;
202 writer.write_section(comments, code)?;
203 writer.try_write_events_table(&item.fields, comments)
204 })?;
205 }
206
207 if let Some(errors) = item.errors() {
208 writer.write_subtitle("Errors")?;
209 errors.into_iter().try_for_each(|(item, comments, code)| {
210 writer.write_heading(&item.name.safe_unwrap().name)?;
211 writer.write_section(comments, code)?;
212 writer.try_write_errors_table(&item.fields, comments)
213 })?;
214 }
215
216 if let Some(structs) = item.structs() {
217 writer.write_subtitle("Structs")?;
218 structs.into_iter().try_for_each(|(item, comments, code)| {
219 writer.write_heading(&item.name.safe_unwrap().name)?;
220 writer.write_section(comments, code)?;
221 writer.try_write_properties_table(&item.fields, comments)
222 })?;
223 }
224
225 if let Some(enums) = item.enums() {
226 writer.write_subtitle("Enums")?;
227 enums.into_iter().try_for_each(|(item, comments, code)| {
228 writer.write_heading(&item.name.safe_unwrap().name)?;
229 writer.write_section(comments, code)?;
230 writer.try_write_variant_table(item, comments)
231 })?;
232 }
233 }
234
235 ParseSource::Function(func) => {
236 writer.writeln_doc(
239 &item.comments.exclude_tags(&[CommentTag::Param, CommentTag::Return]),
240 )?;
241
242 writer.write_code(&item.code)?;
244
245 let params =
247 func.params.iter().filter_map(|p| p.1.as_ref()).collect::<Vec<_>>();
248 writer.try_write_param_table(CommentTag::Param, ¶ms, &item.comments)?;
249
250 let returns =
252 func.returns.iter().filter_map(|p| p.1.as_ref()).collect::<Vec<_>>();
253 writer.try_write_param_table(
254 CommentTag::Return,
255 &returns,
256 &item.comments,
257 )?;
258
259 writer.writeln()?;
260 }
261
262 ParseSource::Struct(ty) => {
263 writer.write_section(&item.comments, &item.code)?;
264 writer.try_write_properties_table(&ty.fields, &item.comments)?;
265 }
266 ParseSource::Event(ev) => {
267 writer.write_section(&item.comments, &item.code)?;
268 writer.try_write_events_table(&ev.fields, &item.comments)?;
269 }
270 ParseSource::Error(err) => {
271 writer.write_section(&item.comments, &item.code)?;
272 writer.try_write_errors_table(&err.fields, &item.comments)?;
273 }
274 ParseSource::Variable(_) | ParseSource::Enum(_) | ParseSource::Type(_) => {
275 writer.write_section(&item.comments, &item.code)?;
276 }
277 }
278 }
279 DocumentContent::Empty => (),
280 };
281
282 Ok(writer.finish())
283 }
284}
285
286impl Document {
287 fn write_function(
289 &self,
290 writer: &mut BufWriter,
291 func: &FunctionDefinition,
292 comments: &Comments,
293 code: &str,
294 ) -> Result<(), std::fmt::Error> {
295 let func_sign = function_signature(func);
296 let func_name = func.name.as_ref().map_or(func.ty.to_string(), |n| n.name.to_owned());
297 let comments =
298 comments.merge_inheritdoc(&func_sign, read_context!(self, INHERITDOC_ID, Inheritdoc));
299
300 writer.write_heading(&func_name)?;
302
303 writer.writeln()?;
304
305 writer.writeln_doc(&comments.exclude_tags(&[CommentTag::Param, CommentTag::Return]))?;
307
308 writer.write_code(code)?;
310
311 let params = func.params.iter().filter_map(|p| p.1.as_ref()).collect::<Vec<_>>();
313 writer.try_write_param_table(CommentTag::Param, ¶ms, &comments)?;
314
315 let returns = func.returns.iter().filter_map(|p| p.1.as_ref()).collect::<Vec<_>>();
317 writer.try_write_param_table(CommentTag::Return, &returns, &comments)?;
318
319 writer.writeln()?;
320 Ok(())
321 }
322}