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
//! Generate raw Rustdoc comments from a given [`crate::ast::ParsedDoxygen`]
//!
//! **The functions and structs here should _not_ be considered stable**

use crate::ast::ParsedDoxygen;

/// Generate raw Rustdoc comments from a given [`crate::ast::ParsedDoxygen`]
///
/// # Examples
/// ```
/// use doxygen_rs::ast::generate_ast;
/// use doxygen_rs::generator::generate_rustdoc;
/// use doxygen_rs::parser::parse_comment;
///
/// let parsed = parse_comment("@brief Random comment");
/// let ast = generate_ast(parsed);
/// let rustdoc = generate_rustdoc(ast);
/// ```
pub fn generate_rustdoc(doxygen: ParsedDoxygen) -> String {
    let mut rustdoc = String::new();

    if let Some(title) = doxygen.title {
        rustdoc += format!("# {}\n\n", title).as_str();
    }

    if let Some(deprecated) = doxygen.deprecated {
        if let Some(message) = deprecated.message {
            rustdoc += format!("**Warning!** This is deprecated! - {}", message).as_str();
        } else {
            rustdoc += "**Warning!** This is deprecated!".to_string().as_str();
        }
        rustdoc += "\n\n";
    }

    if let Some(brief) = doxygen.brief {
        rustdoc += &brief.to_string();
        rustdoc += "\n\n";
    }

    if let Some(description) = doxygen.description {
        for desc in description {
            rustdoc += format!("{}\n", desc).replace("< ", "").as_str();
        }
        rustdoc += "\n";
    }

    if let Some(warnings) = doxygen.warnings {
        rustdoc += "**Warning!**\n\n";
        for warning in warnings {
            rustdoc += format!("{:1}\n", warning.0).as_str();
        }
        rustdoc += "\n"
    }

    if let Some(returns) = doxygen.returns {
        rustdoc += "Returns:\n\n";
        for return_val in returns {
            rustdoc += format!("{:1}\n", return_val.0).as_str()
        }
        rustdoc += "\n";
    }

    if let Some(params) = doxygen.params {
        rustdoc += "# Arguments\n\n";
        for param in params {
            let mut dir = String::new();
            if let Some(direction) = param.direction {
                dir += format!(" [Direction: {}] ", direction.clone()).as_str()
            } else {
                dir += " "
            };

            if let Some(description) = param.description {
                rustdoc += format!("* `{}` -{}{:#1}", param.arg_name, dir, description).as_str();
            } else {
                rustdoc += format!("* `{}` -{}", param.arg_name, dir).as_str();
            }

            rustdoc += "\n";
        }

        rustdoc += "\n";
    }

    if let Some(return_values) = doxygen.return_values {
        rustdoc += "# Return values\n";
        for return_val in return_values {
            rustdoc += format!("{:1}\n", return_val.0).as_str();
        }
        rustdoc += "\n"
    }

    if let Some(notes) = doxygen.notes {
        rustdoc += "# Notes\n\n";
        for note in notes {
            rustdoc += format!("{:1}\n", note.0).as_str();
        }
        rustdoc += "\n"
    }

    if let Some(todos) = doxygen.todos {
        rustdoc += "# To Do\n\n";
        for todo in todos {
            rustdoc += format!("{:1}", todo).as_str();
        }

        rustdoc += "\n";
    }

    rustdoc
}