ruchy 4.1.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Documentation Generation Handler
//!
//! Handles generation of documentation from Ruchy source files.

use anyhow::{bail, Context, Result};
use colored::Colorize;
use ruchy::frontend::ast::{CommentKind, Expr, ExprKind, Pattern};
use ruchy::frontend::parser::Parser;
use std::fs;
use std::path::Path;

/// Handle doc command - generate documentation from Ruchy files
///
/// # Arguments
/// * `path` - Path to file or directory to document
/// * `output` - Output directory for generated documentation
/// * `format` - Output format (html, markdown, json)
/// * `private` - Include private items in documentation
/// * `_open` - Open documentation in browser (not yet implemented)
/// * `_all` - Include all items (not yet implemented)
/// * `verbose` - Enable verbose output
///
/// # Errors
/// Returns error if file cannot be read, parsed, or documentation cannot be generated
pub fn handle_doc_command(
    path: &Path,
    output: &Path,
    format: &str,
    private: bool,
    _open: bool,
    _all: bool,
    verbose: bool,
) -> Result<()> {
    // Validate format
    if !matches!(format, "html" | "markdown" | "json") {
        bail!(
            "Invalid format '{}'. Supported formats: html, markdown, json",
            format
        );
    }

    // Check if path exists
    if !path.exists() {
        bail!("File or directory not found: {}", path.display());
    }

    if verbose {
        println!("{} Parsing {}...", "".bright_blue(), path.display());
    }

    // Read and parse the file
    let source = fs::read_to_string(path)
        .with_context(|| format!("Failed to read file: {}", path.display()))?;

    let mut parser = Parser::new(&source);
    let ast = parser
        .parse()
        .map_err(|e| anyhow::anyhow!("Parse error: {}", e))?;

    if verbose {
        println!("{} Extracting documentation...", "".bright_blue());
    }

    // Extract documentation from AST
    let docs = extract_documentation(&ast, private);

    if verbose {
        println!(
            "{} Generating {} documentation...",
            "".bright_blue(),
            format
        );
    }

    // Create output directory
    fs::create_dir_all(output)
        .with_context(|| format!("Failed to create output directory: {}", output.display()))?;

    // Generate documentation in requested format
    let content = match format {
        "markdown" => generate_markdown_docs(&docs, path),
        "json" => generate_json_docs(&docs, path)?,
        "html" => generate_html_docs(&docs, path),
        _ => unreachable!(),
    };

    // Determine output filename
    let file_stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("docs");
    let extension = match format {
        "markdown" => "md",
        "json" => "json",
        "html" => "html",
        _ => unreachable!(),
    };
    let output_file = output.join(format!("{}.{}", file_stem, extension));

    // Write documentation
    fs::write(&output_file, content)
        .with_context(|| format!("Failed to write documentation: {}", output_file.display()))?;

    println!(
        "{} Generated documentation: {}",
        "".bright_green(),
        output_file.display()
    );

    Ok(())
}

/// Documentation item extracted from source code
#[derive(Debug)]
pub struct DocItem {
    pub kind: DocItemKind,
    pub name: String,
    pub params: Vec<String>,
    pub doc_comment: Option<String>,
}

#[derive(Debug)]
pub enum DocItemKind {
    Function,
}

/// Extract documentation from AST
pub fn extract_documentation(ast: &Expr, include_private: bool) -> Vec<DocItem> {
    let mut docs = Vec::new();
    extract_docs_recursive(ast, &mut docs, include_private);
    docs
}

/// Recursively extract documentation from AST nodes
fn extract_docs_recursive(expr: &Expr, docs: &mut Vec<DocItem>, include_private: bool) {
    match &expr.kind {
        ExprKind::Function { name, params, .. } => {
            // Extract leading doc comments from Comment structs
            let doc_comment = expr
                .leading_comments
                .iter()
                .map(|c| match &c.kind {
                    CommentKind::Line(text) | CommentKind::Block(text) | CommentKind::Doc(text) => {
                        text.clone()
                    }
                })
                .collect::<Vec<_>>()
                .join("\n");
            let has_doc = !doc_comment.is_empty() || include_private;

            if has_doc {
                // Extract parameter names from patterns
                let param_names: Vec<String> = params
                    .iter()
                    .map(|p| match &p.pattern {
                        Pattern::Identifier(name) => name.clone(),
                        _ => "_".to_string(),
                    })
                    .collect();

                docs.push(DocItem {
                    kind: DocItemKind::Function,
                    name: name.clone(),
                    params: param_names,
                    doc_comment: if doc_comment.is_empty() {
                        None
                    } else {
                        Some(doc_comment)
                    },
                });
            }
        }
        ExprKind::Block(exprs) => {
            for e in exprs {
                extract_docs_recursive(e, docs, include_private);
            }
        }
        _ => {}
    }
}

/// Generate Markdown documentation
pub fn generate_markdown_docs(docs: &[DocItem], source_path: &Path) -> String {
    let mut output = String::new();
    output.push_str(&format!(
        "# Documentation for {}\n\n",
        source_path.display()
    ));

    for doc in docs {
        match doc.kind {
            DocItemKind::Function => {
                output.push_str(&format!("## `{}({})`\n\n", doc.name, doc.params.join(", ")));
                if let Some(comment) = &doc.doc_comment {
                    let clean_comment = comment
                        .lines()
                        .map(|line| line.trim_start_matches("///").trim())
                        .collect::<Vec<_>>()
                        .join("\n");
                    output.push_str(&format!("{}\n\n", clean_comment));
                } else {
                    output.push_str("*No documentation available*\n\n");
                }
            }
        }
    }

    output
}

/// Generate JSON documentation
pub fn generate_json_docs(docs: &[DocItem], source_path: &Path) -> Result<String> {
    let mut json_docs = Vec::new();

    for doc in docs {
        let mut obj = serde_json::Map::new();
        obj.insert("kind".to_string(), serde_json::json!("function"));
        obj.insert("name".to_string(), serde_json::json!(doc.name));
        obj.insert("params".to_string(), serde_json::json!(doc.params));
        if let Some(comment) = &doc.doc_comment {
            let clean_comment = comment
                .lines()
                .map(|line| line.trim_start_matches("///").trim())
                .collect::<Vec<_>>()
                .join("\n");
            obj.insert("doc_comment".to_string(), serde_json::json!(clean_comment));
        }
        json_docs.push(serde_json::Value::Object(obj));
    }

    let result = serde_json::json!({
        "source": source_path.display().to_string(),
        "items": json_docs
    });

    Ok(serde_json::to_string_pretty(&result)?)
}

/// Generate HTML documentation
pub fn generate_html_docs(docs: &[DocItem], source_path: &Path) -> String {
    let mut output = String::new();
    output.push_str("<!DOCTYPE html>\n<html>\n<head>\n");
    output.push_str(&format!(
        "<title>Documentation for {}</title>\n",
        source_path.display()
    ));
    output.push_str("<style>\n");
    output.push_str("body { font-family: Arial, sans-serif; margin: 40px; }\n");
    output.push_str("h1 { color: #333; }\n");
    output.push_str("h2 { color: #666; border-bottom: 1px solid #ddd; padding-bottom: 5px; }\n");
    output.push_str("code { background: #f4f4f4; padding: 2px 5px; border-radius: 3px; }\n");
    output.push_str("</style>\n</head>\n<body>\n");
    output.push_str(&format!(
        "<h1>Documentation for {}</h1>\n",
        source_path.display()
    ));

    for doc in docs {
        match doc.kind {
            DocItemKind::Function => {
                output.push_str(&format!(
                    "<h2><code>{}({})</code></h2>\n",
                    doc.name,
                    doc.params.join(", ")
                ));
                if let Some(comment) = &doc.doc_comment {
                    let clean_comment = comment
                        .lines()
                        .map(|line| line.trim_start_matches("///").trim())
                        .collect::<Vec<_>>()
                        .join("<br>\n");
                    output.push_str(&format!("<p>{}</p>\n", clean_comment));
                } else {
                    output.push_str("<p><em>No documentation available</em></p>\n");
                }
            }
        }
    }

    output.push_str("</body>\n</html>\n");
    output
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_markdown_empty() {
        let docs: Vec<DocItem> = vec![];
        let result = generate_markdown_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("# Documentation for"));
    }

    #[test]
    fn test_generate_markdown_with_function() {
        let docs = vec![DocItem {
            kind: DocItemKind::Function,
            name: "foo".to_string(),
            params: vec!["x".to_string(), "y".to_string()],
            doc_comment: Some("/// Does something".to_string()),
        }];
        let result = generate_markdown_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("## `foo(x, y)`"));
        assert!(result.contains("Does something"));
    }

    #[test]
    fn test_generate_json_empty() {
        let docs: Vec<DocItem> = vec![];
        let result = generate_json_docs(&docs, Path::new("test.ruchy")).unwrap();
        assert!(result.contains("\"items\": []"));
    }

    #[test]
    fn test_generate_json_with_function() {
        let docs = vec![DocItem {
            kind: DocItemKind::Function,
            name: "bar".to_string(),
            params: vec!["a".to_string()],
            doc_comment: None,
        }];
        let result = generate_json_docs(&docs, Path::new("test.ruchy")).unwrap();
        assert!(result.contains("\"name\": \"bar\""));
    }

    #[test]
    fn test_generate_html_empty() {
        let docs: Vec<DocItem> = vec![];
        let result = generate_html_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("<!DOCTYPE html>"));
        assert!(result.contains("</html>"));
    }

    #[test]
    fn test_generate_html_with_function() {
        let docs = vec![DocItem {
            kind: DocItemKind::Function,
            name: "baz".to_string(),
            params: vec![],
            doc_comment: Some("/// Test function".to_string()),
        }];
        let result = generate_html_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("<code>baz()</code>"));
        assert!(result.contains("Test function"));
    }

    // ===== EXTREME TDD Round 152 - Doc Handler Tests =====

    #[test]
    fn test_generate_markdown_no_doc_comment() {
        let docs = vec![DocItem {
            kind: DocItemKind::Function,
            name: "undocumented".to_string(),
            params: vec!["x".to_string()],
            doc_comment: None,
        }];
        let result = generate_markdown_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("*No documentation available*"));
    }

    #[test]
    fn test_generate_html_no_doc_comment() {
        let docs = vec![DocItem {
            kind: DocItemKind::Function,
            name: "undocumented".to_string(),
            params: vec![],
            doc_comment: None,
        }];
        let result = generate_html_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("<em>No documentation available</em>"));
    }

    #[test]
    fn test_generate_json_with_doc_comment() {
        let docs = vec![DocItem {
            kind: DocItemKind::Function,
            name: "documented".to_string(),
            params: vec!["a".to_string(), "b".to_string()],
            doc_comment: Some("/// Does something important".to_string()),
        }];
        let result = generate_json_docs(&docs, Path::new("test.ruchy")).unwrap();
        assert!(result.contains("doc_comment"));
        assert!(result.contains("Does something important"));
    }

    #[test]
    fn test_doc_item_debug() {
        let doc = DocItem {
            kind: DocItemKind::Function,
            name: "test".to_string(),
            params: vec![],
            doc_comment: None,
        };
        let debug_str = format!("{:?}", doc);
        assert!(debug_str.contains("Function"));
        assert!(debug_str.contains("test"));
    }

    #[test]
    fn test_generate_markdown_multiple_params() {
        let docs = vec![DocItem {
            kind: DocItemKind::Function,
            name: "multi".to_string(),
            params: vec!["a".to_string(), "b".to_string(), "c".to_string()],
            doc_comment: None,
        }];
        let result = generate_markdown_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("`multi(a, b, c)`"));
    }

    #[test]
    fn test_generate_html_css_styles() {
        let docs: Vec<DocItem> = vec![];
        let result = generate_html_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("<style>"));
        assert!(result.contains("font-family"));
        assert!(result.contains("</style>"));
    }

    #[test]
    fn test_doc_item_kind_function() {
        let kind = DocItemKind::Function;
        let debug_str = format!("{:?}", kind);
        assert_eq!(debug_str, "Function");
    }

    #[test]
    fn test_generate_json_source_path() {
        let docs: Vec<DocItem> = vec![];
        let result = generate_json_docs(&docs, Path::new("/path/to/test.ruchy")).unwrap();
        assert!(result.contains("/path/to/test.ruchy"));
    }

    #[test]
    fn test_generate_markdown_multiline_doc() {
        let docs = vec![DocItem {
            kind: DocItemKind::Function,
            name: "multiline".to_string(),
            params: vec![],
            doc_comment: Some("/// Line 1\n/// Line 2\n/// Line 3".to_string()),
        }];
        let result = generate_markdown_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("Line 1"));
        assert!(result.contains("Line 2"));
        assert!(result.contains("Line 3"));
    }

    #[test]
    fn test_handle_doc_command_invalid_format() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let source_file = temp_dir.path().join("test.ruchy");
        std::fs::write(&source_file, "fun test() { 42 }").unwrap();

        let result = handle_doc_command(
            &source_file,
            temp_dir.path(),
            "invalid_format",
            false,
            false,
            false,
            false,
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid format"));
    }

    #[test]
    fn test_handle_doc_command_nonexistent_file() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let result = handle_doc_command(
            Path::new("/nonexistent/file.ruchy"),
            temp_dir.path(),
            "markdown",
            false,
            false,
            false,
            false,
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[test]
    fn test_generate_html_head_section() {
        let docs: Vec<DocItem> = vec![];
        let result = generate_html_docs(&docs, Path::new("test.ruchy"));
        assert!(result.contains("<!DOCTYPE html>"));
        assert!(result.contains("<head>"));
        assert!(result.contains("<title>"));
        assert!(result.contains("</head>"));
    }
}