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
//! Commands to generate a markdown documentation for the command line.

// TODO: Some Configuration could not be generated properly because there is a lack of information.
// - We don't know which parameter is required or not.
// - In aggregator, Configuration struct contains all parameters but it's not possible to know which sub command use one parameter.

mod extract_clap_info;
mod markdown_formatter;
mod test_doc_macro;

use clap::{Command, Parser};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;

pub use mithril_doc_derive::{self, *};

const DEFAULT_OUTPUT_FILE_TEMPLATE: &str = "[PROGRAM NAME]-command-line.md";

/// Information to document a field
#[derive(Clone, Default, Debug)]
pub struct FieldDoc {
    /// Name of the parameter
    pub parameter: String,
    /// Long option for the command line
    pub command_line_long: String,
    /// Short option for the command line
    pub command_line_short: String,
    /// Environment variable
    pub environment_variable: Option<String>,
    /// Description of the parameter
    pub description: String,
    /// Default value
    pub default_value: Option<String>,
    /// Usage example
    pub example: Option<String>,
    /// Is a mandatory parameter
    pub is_mandatory: bool,
}

/// Information about the struct.
#[derive(Clone, Default, Debug)]
pub struct StructDoc {
    /// List of fields
    pub data: Vec<FieldDoc>,
}

impl StructDoc {
    /// Create an empty struct.
    pub fn new() -> StructDoc {
        StructDoc { data: vec![] }
    }

    /// Add information about one parameter.
    pub fn add_param(
        &mut self,
        name: &str,
        description: &str,
        environment_variable: Option<String>,
        default: Option<String>,
        example: Option<String>,
    ) {
        let field_doc = FieldDoc {
            parameter: name.to_string(),
            command_line_long: "".to_string(),
            command_line_short: "".to_string(),
            environment_variable,
            description: description.to_string(),
            default_value: default,
            example,
            is_mandatory: false,
        };
        self.data.push(field_doc);
    }

    /// Merge two StructDoc into a third one.
    pub fn merge_struct_doc(&self, s2: &StructDoc) -> StructDoc {
        let mut data_map1 = self
            .data
            .iter()
            .map(|field_doc| (field_doc.parameter.clone(), field_doc.clone()))
            .collect::<BTreeMap<_, _>>();

        for field_doc in s2.data.iter() {
            if !data_map1.contains_key(&field_doc.parameter) {
                data_map1.insert(field_doc.parameter.clone(), field_doc.clone());
            } else {
                let mut d = data_map1.get(&field_doc.parameter).unwrap().clone();
                if d.default_value.is_none() {
                    d.default_value.clone_from(&field_doc.default_value);
                }
                if d.example.is_none() {
                    d.example.clone_from(&field_doc.example);
                }
                if d.environment_variable.is_none() {
                    d.environment_variable
                        .clone_from(&field_doc.environment_variable);
                }
                data_map1.insert(field_doc.parameter.clone(), d);
            }
        }
        let result = StructDoc {
            data: data_map1.values().cloned().collect(),
        };
        result
    }
}

/// Extractor for struct without Default trait.
pub trait Documenter {
    /// Extract information used to generate documentation.
    fn extract() -> StructDoc;
}

/// Extractor for struct with Default trait.
pub trait DocumenterDefault {
    /// Extract information used to generate documentation.
    fn extract() -> StructDoc;
}

/// Generate documentation
#[derive(Parser, Debug, PartialEq, Clone)]
pub struct GenerateDocCommands {
    /// Generated documentation file
    #[clap(long, default_value = DEFAULT_OUTPUT_FILE_TEMPLATE)]
    output: String,
}

impl GenerateDocCommands {
    fn save_doc(&self, cmd_name: &str, doc: &str) -> Result<(), String> {
        let output = if self.output.as_str() == DEFAULT_OUTPUT_FILE_TEMPLATE {
            format!("{}-command-line.md", cmd_name)
        } else {
            self.output.clone()
        };

        match File::create(&output) {
            Ok(mut buffer) => {
                if write!(buffer, "\n{}", doc).is_err() {
                    return Err(format!("Error writing in {}", output));
                }
                println!("Documentation generated in file `{}`", &output);
            }
            _ => return Err(format!("Could not create {}", output)),
        };
        Ok(())
    }

    /// Generate the command line documentation.
    pub fn execute(&self, cmd_to_document: &mut Command) -> Result<(), String> {
        self.execute_with_configurations(cmd_to_document, &[])
    }

    /// Generate the command line documentation with config info.
    pub fn execute_with_configurations(
        &self,
        cmd_to_document: &mut Command,
        configs_info: &[StructDoc],
    ) -> Result<(), String> {
        let mut iter_config = configs_info.iter();
        let mut merged_struct_doc = StructDoc::new();
        for next_config in &mut iter_config {
            merged_struct_doc = merged_struct_doc.merge_struct_doc(next_config);
        }

        let doc =
            markdown_formatter::doc_markdown_with_config(cmd_to_document, Some(&merged_struct_doc));
        let cmd_name = cmd_to_document.get_name();

        self.save_doc(cmd_name, format!("\n{}", doc).as_str())
    }
}

#[cfg(test)]
mod tests {

    use std::collections::HashMap;

    use super::*;

    #[test]
    fn test_merge_struct_doc() {
        let s1 = {
            let mut s = StructDoc::default();
            s.add_param(
                "A",
                "Param first A",
                Some("env A".to_string()),
                Some("default A".to_string()),
                Some("example A".to_string()),
            );
            s.add_param("B", "Param first B", None, None, None);
            s.add_param(
                "C",
                "Param first C",
                Some("env C".to_string()),
                Some("default C".to_string()),
                Some("example C".to_string()),
            );
            s.add_param("D", "Param first D", None, None, None);
            s
        };

        let s2 = {
            let mut s = StructDoc::default();
            s.add_param("A", "Param second A", None, None, None);
            s.add_param(
                "B",
                "Param second B",
                Some("env B".to_string()),
                Some("default B".to_string()),
                Some("example B".to_string()),
            );
            s.add_param("E", "Param second E", None, None, None);
            s.add_param(
                "F",
                "Param second F",
                Some("env F".to_string()),
                Some("default F".to_string()),
                Some("example F".to_string()),
            );
            s
        };

        let result = s1.merge_struct_doc(&s2);

        let data = result.data;
        let data_map = data
            .into_iter()
            .map(|field_doc| (field_doc.parameter.clone(), field_doc))
            .collect::<HashMap<_, _>>();

        assert_eq!(6, data_map.len());
        assert_eq!("Param first A", data_map.get("A").unwrap().description);
        assert_eq!("Param first B", data_map.get("B").unwrap().description);
        assert_eq!("Param first C", data_map.get("C").unwrap().description);
        assert_eq!("Param first D", data_map.get("D").unwrap().description);
        assert_eq!("Param second E", data_map.get("E").unwrap().description);
        assert_eq!("Param second F", data_map.get("F").unwrap().description);

        assert_eq!(
            Some("default A".to_string()),
            data_map.get("A").unwrap().default_value
        );
        assert_eq!(
            Some("default B".to_string()),
            data_map.get("B").unwrap().default_value
        );
        assert_eq!(
            Some("default C".to_string()),
            data_map.get("C").unwrap().default_value
        );
        assert_eq!(None, data_map.get("D").unwrap().default_value);
        assert_eq!(None, data_map.get("E").unwrap().default_value);
        assert_eq!(
            Some("default F".to_string()),
            data_map.get("F").unwrap().default_value
        );

        assert_eq!(
            Some("example A".to_string()),
            data_map.get("A").unwrap().example
        );
        assert_eq!(
            Some("example B".to_string()),
            data_map.get("B").unwrap().example
        );
        assert_eq!(
            Some("example C".to_string()),
            data_map.get("C").unwrap().example
        );
        assert_eq!(None, data_map.get("D").unwrap().example);
        assert_eq!(None, data_map.get("E").unwrap().example);
        assert_eq!(
            Some("example F".to_string()),
            data_map.get("F").unwrap().example
        );

        assert_eq!(
            Some("env A".to_string()),
            data_map.get("A").unwrap().environment_variable
        );
        assert_eq!(
            Some("env B".to_string()),
            data_map.get("B").unwrap().environment_variable
        );
        assert_eq!(
            Some("env C".to_string()),
            data_map.get("C").unwrap().environment_variable
        );
        assert_eq!(None, data_map.get("D").unwrap().environment_variable);
        assert_eq!(None, data_map.get("E").unwrap().environment_variable);
        assert_eq!(
            Some("env F".to_string()),
            data_map.get("F").unwrap().environment_variable
        );
    }

    #[test]
    fn test_merge_struct_doc_should_keep_the_order() {
        let values = ["A", "B", "C", "D", "E", "F", "G"];
        let s1 = {
            let mut s = StructDoc::default();
            for value in values.iter() {
                s.add_param(value, value, None, None, None);
            }
            s
        };

        let s2 = s1.clone();

        for (index, value) in values.iter().enumerate() {
            assert_eq!(value, &s1.data[index].parameter);
            assert_eq!(value, &s2.data[index].parameter);
        }

        let result = s1.merge_struct_doc(&s2);
        for (index, value) in values.iter().enumerate() {
            assert_eq!(value, &result.data[index].parameter);
        }
    }
}