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
use crate::backend::*;
use crate::model::*;

use super::conversion::*;

pub(crate) fn javadoc_print(f: &mut dyn Printer, doc: &Doc<Validated>) -> FormattingResult<()> {
    f.newline()?;
    docstring_print(f, &doc.brief)?;

    for detail in &doc.details {
        f.newline()?;

        match detail {
            DocParagraph::Details(docstring) => {
                f.writeln("<p>")?;
                docstring_print(f, docstring)?;
                f.write("</p>")?;
            }
            DocParagraph::Warning(docstring) => {
                f.writeln("<p><b>Warning:</b> ")?;
                docstring_print(f, docstring)?;
                f.write("</p>")?;
            }
        }
    }

    Ok(())
}

pub(crate) fn docstring_print(
    f: &mut dyn Printer,
    docstring: &DocString<Validated>,
) -> FormattingResult<()> {
    for el in docstring.elements() {
        match el {
            DocStringElement::Text(text) => f.write(text)?,
            DocStringElement::Null => f.write("{@code null}")?,
            DocStringElement::Iterator => f.write("{@link java.util.List}")?,
            DocStringElement::Reference(reference) => reference_print(f, reference)?,
        }
    }

    Ok(())
}

fn reference_print(f: &mut dyn Printer, reference: &Validated) -> FormattingResult<()> {
    match reference {
        Validated::Argument(param_name) => {
            f.write(&format!("{{@code {}}}", param_name.mixed_case()))?
        }
        Validated::Class(class) => {
            f.write(&format!("{{@link {}}}", class.name.camel_case()))?;
        }
        Validated::ClassMethod(class, method_name, _) => {
            f.write(&format!(
                "{{@link {}#{}}}",
                class.name().camel_case(),
                method_name.mixed_case()
            ))?;
        }
        Validated::ClassConstructor(class, constructor) => {
            let params = constructor
                .function
                .arguments
                .iter()
                .map(|param| param.arg_type.as_java_primitive())
                .collect::<Vec<_>>()
                .join(", ");

            let class_name = class.name().camel_case();
            f.write(&format!("{{@link {class_name}#{class_name}({params})}}"))?;
        }
        Validated::ClassDestructor(class, _) => {
            let method_name = if let DestructionMode::Custom(name) = &class.destruction_mode {
                name.mixed_case()
            } else {
                "close".to_string()
            };

            f.write(&format!(
                "{{@link {}#{}}}",
                class.name().camel_case(),
                method_name
            ))?;
        }
        Validated::Struct(st) => {
            f.write(&format!("{{@link {}}}", st.name().camel_case()))?;
        }
        Validated::StructField(st, field_name) => {
            f.write(&format!(
                "{{@link {}#{}}}",
                st.name().camel_case(),
                field_name.mixed_case()
            ))?;
        }
        Validated::Enum(handle) => {
            f.write(&format!("{{@link {}}}", handle.name.camel_case()))?;
        }
        Validated::EnumVariant(handle, variant_name) => {
            f.write(&format!(
                "{{@link {}#{}}}",
                handle.name.camel_case(),
                variant_name.capital_snake_case()
            ))?;
        }
        Validated::Interface(interface) => {
            f.write(&format!("{{@link {}}}", interface.name.camel_case()))?;
        }
        Validated::InterfaceMethod(interface, callback_name) => {
            f.write(&format!(
                "{{@link {}#{}}}",
                interface.name.camel_case(),
                callback_name.mixed_case()
            ))?;
        }
    }

    Ok(())
}