forge_doc/writer/
as_doc.rs

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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use crate::{
    document::{read_context, DocumentContent},
    parser::ParseSource,
    writer::BufWriter,
    CommentTag, Comments, CommentsRef, Document, Markdown, PreprocessorOutput,
    CONTRACT_INHERITANCE_ID, DEPLOYMENTS_ID, GIT_SOURCE_ID, INHERITDOC_ID,
};
use forge_fmt::solang_ext::SafeUnwrap;
use itertools::Itertools;
use solang_parser::pt::{Base, FunctionDefinition};
use std::path::{Path, PathBuf};

/// The result of [`AsDoc::as_doc`].
pub type AsDocResult = Result<String, std::fmt::Error>;

/// A trait for formatting a parse unit as documentation.
pub trait AsDoc {
    /// Formats a parse tree item into a doc string.
    fn as_doc(&self) -> AsDocResult;
}

impl AsDoc for String {
    fn as_doc(&self) -> AsDocResult {
        Ok(self.to_owned())
    }
}

impl AsDoc for Comments {
    fn as_doc(&self) -> AsDocResult {
        CommentsRef::from(self).as_doc()
    }
}

impl AsDoc for CommentsRef<'_> {
    // TODO: support other tags
    fn as_doc(&self) -> AsDocResult {
        let mut writer = BufWriter::default();

        // Write author tag(s)
        let authors = self.include_tag(CommentTag::Author);
        if !authors.is_empty() {
            writer.write_bold(&format!("Author{}:", if authors.len() == 1 { "" } else { "s" }))?;
            writer.writeln_raw(authors.iter().map(|a| &a.value).join(", "))?;
            writer.writeln()?;
        }

        // Write notice tags
        let notices = self.include_tag(CommentTag::Notice);
        for n in notices.iter() {
            writer.writeln_raw(&n.value)?;
            writer.writeln()?;
        }

        // Write dev tags
        let devs = self.include_tag(CommentTag::Dev);
        for d in devs.iter() {
            writer.write_italic(&d.value)?;
            writer.writeln()?;
        }

        // Write custom tags
        let customs = self.get_custom_tags();
        if !customs.is_empty() {
            writer.write_bold(&format!("Note{}:", if customs.len() == 1 { "" } else { "s" }))?;
            for c in customs.iter() {
                writer.writeln_raw(format!(
                    "{}{}: {}",
                    if customs.len() == 1 { "" } else { "- " },
                    &c.tag,
                    &c.value
                ))?;
                writer.writeln()?;
            }
        }

        Ok(writer.finish())
    }
}

impl AsDoc for Base {
    fn as_doc(&self) -> AsDocResult {
        Ok(self.name.identifiers.iter().map(|ident| ident.name.to_owned()).join("."))
    }
}

impl AsDoc for Document {
    fn as_doc(&self) -> AsDocResult {
        let mut writer = BufWriter::default();

        match &self.content {
            DocumentContent::OverloadedFunctions(items) => {
                writer
                    .write_title(&format!("function {}", items.first().unwrap().source.ident()))?;
                if let Some(git_source) = read_context!(self, GIT_SOURCE_ID, GitSource) {
                    writer.write_link("Git Source", &git_source)?;
                    writer.writeln()?;
                }

                for item in items.iter() {
                    let func = item.as_function().unwrap();
                    let mut heading = item.source.ident();
                    if !func.params.is_empty() {
                        heading.push_str(&format!(
                            "({})",
                            func.params
                                .iter()
                                .map(|p| p.1.as_ref().map(|p| p.ty.to_string()).unwrap_or_default())
                                .join(", ")
                        ));
                    }
                    writer.write_heading(&heading)?;
                    writer.write_section(&item.comments, &item.code)?;
                }
            }
            DocumentContent::Constants(items) => {
                writer.write_title("Constants")?;
                if let Some(git_source) = read_context!(self, GIT_SOURCE_ID, GitSource) {
                    writer.write_link("Git Source", &git_source)?;
                    writer.writeln()?;
                }

                for item in items.iter() {
                    let var = item.as_variable().unwrap();
                    writer.write_heading(&var.name.safe_unwrap().name)?;
                    writer.write_section(&item.comments, &item.code)?;
                }
            }
            DocumentContent::Single(item) => {
                writer.write_title(&item.source.ident())?;
                if let Some(git_source) = read_context!(self, GIT_SOURCE_ID, GitSource) {
                    writer.write_link("Git Source", &git_source)?;
                    writer.writeln()?;
                }

                if let Some(deployments) = read_context!(self, DEPLOYMENTS_ID, Deployments) {
                    writer.write_deployments_table(deployments)?;
                }

                match &item.source {
                    ParseSource::Contract(contract) => {
                        if !contract.base.is_empty() {
                            writer.write_bold("Inherits:")?;

                            // we need this to find the _relative_ paths
                            let src_target_dir = self.target_src_dir();

                            let mut bases = vec![];
                            let linked =
                                read_context!(self, CONTRACT_INHERITANCE_ID, ContractInheritance);
                            for base in contract.base.iter() {
                                let base_doc = base.as_doc()?;
                                let base_ident = &base.name.identifiers.last().unwrap().name;

                                let link = linked
                                    .as_ref()
                                    .and_then(|link| {
                                        link.get(base_ident).map(|path| {
                                            let path = Path::new("/").join(
                                                path.strip_prefix(&src_target_dir)
                                                    .ok()
                                                    .unwrap_or(path),
                                            );
                                            Markdown::Link(&base_doc, &path.display().to_string())
                                                .as_doc()
                                        })
                                    })
                                    .transpose()?
                                    .unwrap_or(base_doc);

                                bases.push(link);
                            }

                            writer.writeln_raw(bases.join(", "))?;
                            writer.writeln()?;
                        }

                        writer.writeln_doc(&item.comments)?;

                        if let Some(state_vars) = item.variables() {
                            writer.write_subtitle("State Variables")?;
                            state_vars.into_iter().try_for_each(|(item, comments, code)| {
                                let comments = comments.merge_inheritdoc(
                                    &item.name.safe_unwrap().name,
                                    read_context!(self, INHERITDOC_ID, Inheritdoc),
                                );

                                writer.write_heading(&item.name.safe_unwrap().name)?;
                                writer.write_section(&comments, code)?;
                                writer.writeln()
                            })?;
                        }

                        if let Some(funcs) = item.functions() {
                            writer.write_subtitle("Functions")?;

                            for (func, comments, code) in funcs.iter() {
                                self.write_function(&mut writer, func, comments, code)?;
                            }
                        }

                        if let Some(events) = item.events() {
                            writer.write_subtitle("Events")?;
                            events.into_iter().try_for_each(|(item, comments, code)| {
                                writer.write_heading(&item.name.safe_unwrap().name)?;
                                writer.write_section(comments, code)?;
                                writer.try_write_events_table(&item.fields, comments)
                            })?;
                        }

                        if let Some(errors) = item.errors() {
                            writer.write_subtitle("Errors")?;
                            errors.into_iter().try_for_each(|(item, comments, code)| {
                                writer.write_heading(&item.name.safe_unwrap().name)?;
                                writer.write_section(comments, code)?;
                                writer.try_write_errors_table(&item.fields, comments)
                            })?;
                        }

                        if let Some(structs) = item.structs() {
                            writer.write_subtitle("Structs")?;
                            structs.into_iter().try_for_each(|(item, comments, code)| {
                                writer.write_heading(&item.name.safe_unwrap().name)?;
                                writer.write_section(comments, code)?;
                                writer.try_write_properties_table(&item.fields, comments)
                            })?;
                        }

                        if let Some(enums) = item.enums() {
                            writer.write_subtitle("Enums")?;
                            enums.into_iter().try_for_each(|(item, comments, code)| {
                                writer.write_heading(&item.name.safe_unwrap().name)?;
                                writer.write_section(comments, code)
                            })?;
                        }
                    }

                    ParseSource::Function(func) => {
                        // TODO: cleanup
                        // Write function docs
                        writer.writeln_doc(
                            &item.comments.exclude_tags(&[CommentTag::Param, CommentTag::Return]),
                        )?;

                        // Write function header
                        writer.write_code(&item.code)?;

                        // Write function parameter comments in a table
                        let params =
                            func.params.iter().filter_map(|p| p.1.as_ref()).collect::<Vec<_>>();
                        writer.try_write_param_table(CommentTag::Param, &params, &item.comments)?;

                        // Write function return parameter comments in a table
                        let returns =
                            func.returns.iter().filter_map(|p| p.1.as_ref()).collect::<Vec<_>>();
                        writer.try_write_param_table(
                            CommentTag::Return,
                            &returns,
                            &item.comments,
                        )?;

                        writer.writeln()?;
                    }

                    ParseSource::Struct(ty) => {
                        writer.write_section(&item.comments, &item.code)?;
                        writer.try_write_properties_table(&ty.fields, &item.comments)?;
                    }
                    ParseSource::Event(ev) => {
                        writer.write_section(&item.comments, &item.code)?;
                        writer.try_write_events_table(&ev.fields, &item.comments)?;
                    }
                    ParseSource::Error(err) => {
                        writer.write_section(&item.comments, &item.code)?;
                        writer.try_write_errors_table(&err.fields, &item.comments)?;
                    }
                    ParseSource::Variable(_) | ParseSource::Enum(_) | ParseSource::Type(_) => {
                        writer.write_section(&item.comments, &item.code)?;
                    }
                }
            }
            DocumentContent::Empty => (),
        };

        Ok(writer.finish())
    }
}

impl Document {
    /// Where all the source files are written to
    fn target_src_dir(&self) -> PathBuf {
        self.out_target_dir.join("src")
    }

    /// Writes a function to the buffer.
    fn write_function(
        &self,
        writer: &mut BufWriter,
        func: &FunctionDefinition,
        comments: &Comments,
        code: &str,
    ) -> Result<(), std::fmt::Error> {
        let func_name = func.name.as_ref().map_or(func.ty.to_string(), |n| n.name.to_owned());
        let comments =
            comments.merge_inheritdoc(&func_name, read_context!(self, INHERITDOC_ID, Inheritdoc));

        // Write function name
        writer.write_heading(&func_name)?;

        writer.writeln()?;

        // Write function docs
        writer.writeln_doc(&comments.exclude_tags(&[CommentTag::Param, CommentTag::Return]))?;

        // Write function header
        writer.write_code(code)?;

        // Write function parameter comments in a table
        let params = func.params.iter().filter_map(|p| p.1.as_ref()).collect::<Vec<_>>();
        writer.try_write_param_table(CommentTag::Param, &params, &comments)?;

        // Write function return parameter comments in a table
        let returns = func.returns.iter().filter_map(|p| p.1.as_ref()).collect::<Vec<_>>();
        writer.try_write_param_table(CommentTag::Return, &returns, &comments)?;

        writer.writeln()?;
        Ok(())
    }
}