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
use crate::module::{error::AutodocsError, group_functions, options::Options, ModuleMetadata};

/// Glossary of all function for a module and it's submodules.
#[derive(Debug)]
pub struct ModuleGlossary {
    /// Formated function signatures by submodules.
    pub content: String,
}

/// Generate documentation based on an engine instance and a glossary of all functions.
/// Make sure all the functions, operators, plugins, etc. are registered inside this instance.
///
/// # CAUTION
///
/// This only works for docusaurus at the moment.
///
/// # Result
/// * A vector of documented modules.
///
/// # Errors
/// * Failed to generate function metadata as json.
/// * Failed to parse module metadata.
pub fn generate_module_glossary(
    engine: &rhai::Engine,
    options: &Options,
) -> Result<ModuleGlossary, AutodocsError> {
    let json_fns = engine
        .gen_fn_metadata_to_json(options.include_standard_packages)
        .map_err(|error| AutodocsError::Metadata(error.to_string()))?;

    let metadata = serde_json::from_str::<ModuleMetadata>(&json_fns)
        .map_err(|error| AutodocsError::Metadata(error.to_string()))?;

    generate_child_module_glossary(options, None, "global", &metadata)
}

fn generate_child_module_glossary(
    options: &Options,
    namespace: Option<String>,
    name: impl Into<String>,
    metadata: &ModuleMetadata,
) -> Result<ModuleGlossary, AutodocsError> {
    fn make_highlight(color: &str, item_type: &str, definition: &str) -> String {
        format!("- <Highlight color=\"{color}\">{item_type}</Highlight> <code>{{\"{definition}\"}}</code>\n",)
    }

    let name = name.into();
    let namespace = namespace.map_or(name.clone(), |namespace| namespace);

    let signatures = if let Some(functions) = &metadata.functions {
        let fn_groups = group_functions(options, &namespace, functions)?;
        let mut signatures = String::default();

        for (_, polymorphisms) in fn_groups {
            for fn_metadata in polymorphisms {
                let root_definition = fn_metadata.generate_function_definition();

                let serialized = root_definition.display();
                // FIXME: this only works for docusaurus.
                // TODO: customize colors.
                signatures += &if serialized.starts_with("op ") {
                    make_highlight(
                        "#16c6f3",
                        root_definition.type_to_str(),
                        serialized.trim_start_matches("op "),
                    )
                } else if serialized.starts_with("get ") {
                    make_highlight(
                        "#25c2a0",
                        root_definition.type_to_str(),
                        serialized.trim_start_matches("get "),
                    )
                } else if serialized.starts_with("set ") {
                    make_highlight(
                        "#25c2a0",
                        root_definition.type_to_str(),
                        serialized.trim_start_matches("set "),
                    )
                } else if serialized.starts_with("index get ") {
                    make_highlight(
                        "#25c2a0",
                        root_definition.type_to_str(),
                        serialized.trim_start_matches("index get "),
                    )
                } else if serialized.starts_with("index set ") {
                    make_highlight(
                        "#25c2a0",
                        root_definition.type_to_str(),
                        serialized.trim_start_matches("index set "),
                    )
                } else {
                    make_highlight(
                        "#C6cacb",
                        root_definition.type_to_str(),
                        serialized.trim_start_matches("fn "),
                    )
                };
            }
        }

        signatures
    } else {
        String::default()
    };

    // FIXME: this only works for docusaurus.
    let mut mg = ModuleGlossary {
        content: if name == "global" {
            format!(
                "{} \n\n### {}\n{}",
                include_str!("components/highlight.js"),
                name,
                signatures
            )
        } else {
            format!("### {}\n{}", name, signatures)
        },
    };

    // Generate signatures for each submodule. (if any)
    if let Some(sub_modules) = &metadata.modules {
        for (sub_module, value) in sub_modules {
            mg.content.push_str(&{
                let mg = generate_child_module_glossary(
                    options,
                    Some(format!("{}/{}", namespace, sub_module)),
                    sub_module,
                    &serde_json::from_value::<ModuleMetadata>(value.clone())
                        .map_err(|error| AutodocsError::Metadata(error.to_string()))?,
                )?;

                mg.content
            });
        }
    }

    Ok(mg)
}