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

use serde::Serialize;
use crate::ast::span::Span;
use crate::format::Writer;
use crate::impl_node_defaults;
use crate::traits::write::Write;

#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub struct DocComment {
    pub(crate) span: Span,
    pub(crate) path: Vec<usize>,
    pub(crate) name: Option<String>,
    pub(crate) desc: Option<String>,
}

impl_node_defaults!(DocComment);

impl DocComment {

    pub fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|n| n.as_str())
    }

    pub fn desc(&self) -> Option<&str> {
        self.desc.as_ref().map(|n| n.as_str())
    }
}

impl Write for DocComment {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        let mut contents = vec![];
        if let Some(name) = self.name() {
            contents.push("/// @name ");
            contents.push(name);
            contents.push("\n");
        }
        if let Some(desc) = self.desc() {
            contents.push("/// ");
            contents.push(desc);
            contents.push("\n");
        }
        writer.write_contents(self, contents);

    }
}