ruchy 4.2.0

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
//! Handler for `ruchy new` command (CARGO-002)
//!
//! Creates new Ruchy projects with Cargo integration

use anyhow::{Context, Result};
use std::fs;
use std::path::Path;
use std::process::Command;

/// Handle `ruchy new` command - create new Ruchy project with Cargo integration
///
/// # Complexity
///
/// Complexity: 6 (within Toyota Way limits ≤10)
pub fn handle_new_command(name: &str, is_lib: bool, verbose: bool) -> Result<()> {
    // Step 1: Run `cargo new` to create base project
    create_cargo_project(name, is_lib, verbose)?;

    // Step 2: Add Ruchy-specific files
    let project_dir = Path::new(name);
    add_ruchy_files(project_dir, is_lib)?;

    // Step 3: Print success message
    println!("Created Ruchy project `{name}`");
    if verbose {
        println!(
            "Project type: {}",
            if is_lib { "library" } else { "binary" }
        );
        println!("Next steps:");
        println!("  cd {name}");
        println!("  cargo build");
        println!("  cargo run");
    }

    Ok(())
}

/// Create base Cargo project using `cargo new`
///
/// # Complexity
///
/// Complexity: 5 (within Toyota Way limits ≤10)
fn create_cargo_project(name: &str, is_lib: bool, verbose: bool) -> Result<()> {
    let mut cmd = Command::new("cargo");
    cmd.arg("new").arg(name);

    if is_lib {
        cmd.arg("--lib");
    }

    if verbose {
        println!(
            "Running: cargo new {}{}",
            name,
            if is_lib { " --lib" } else { "" }
        );
    }

    let output = cmd
        .output()
        .context("Failed to run cargo new - ensure cargo is installed")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("cargo new failed: {stderr}");
    }

    Ok(())
}

/// Add Ruchy-specific files to the project
///
/// # Complexity
///
/// Complexity: 4 (within Toyota Way limits ≤10)
fn add_ruchy_files(project_dir: &Path, is_lib: bool) -> Result<()> {
    // Add build.rs
    create_build_rs(project_dir)?;

    // Modify Cargo.toml
    modify_cargo_toml(project_dir)?;

    // Add .ruchy source files
    create_ruchy_source(project_dir, is_lib)?;

    // Create/update README
    create_readme(project_dir, is_lib)?;

    Ok(())
}

/// Create build.rs file
///
/// # Complexity
///
/// Complexity: 2 (within Toyota Way limits ≤10)
fn create_build_rs(project_dir: &Path) -> Result<()> {
    let build_rs_content = r#"//! Build script for Ruchy project
//!
//! Automatically transpiles .ruchy files to .rs files during cargo build

fn main() {
    // Transpile all .ruchy files in src/ to .rs files
    ruchy::build_transpiler::transpile_all("src", "**/*.ruchy", "src")
        .expect("Failed to transpile Ruchy files");

    // Tell Cargo to re-run this build script if any .ruchy files change
    println!("cargo:rerun-if-changed=src");
}
"#;

    let build_rs_path = project_dir.join("build.rs");
    fs::write(&build_rs_path, build_rs_content)
        .with_context(|| format!("Failed to write build.rs to {}", build_rs_path.display()))?;

    Ok(())
}

/// Modify Cargo.toml to add ruchy as build dependency
///
/// # Complexity
///
/// Complexity: 7 (within Toyota Way limits ≤10)
fn modify_cargo_toml(project_dir: &Path) -> Result<()> {
    let cargo_toml_path = project_dir.join("Cargo.toml");
    let content = fs::read_to_string(&cargo_toml_path).with_context(|| {
        format!(
            "Failed to read Cargo.toml from {}",
            cargo_toml_path.display()
        )
    })?;

    // Add build script reference and build-dependencies
    let modified_content = if content.contains("build =") {
        // Already has build key, just add build-dependencies
        add_build_dependencies(&content)
    } else {
        // Need to add both build key and build-dependencies
        add_build_script_and_dependencies(&content)
    };

    fs::write(&cargo_toml_path, modified_content).with_context(|| {
        format!(
            "Failed to write Cargo.toml to {}",
            cargo_toml_path.display()
        )
    })?;

    Ok(())
}

/// Add build-dependencies section to Cargo.toml
///
/// # Complexity
///
/// Complexity: 2 (within Toyota Way limits ≤10)
fn add_build_dependencies(content: &str) -> String {
    // If build-dependencies already exists, add ruchy to it
    if content.contains("[build-dependencies]") {
        content.replace(
            "[build-dependencies]",
            "[build-dependencies]\nruchy = \"3.71\"",
        )
    } else {
        // Add new build-dependencies section at end
        format!("{content}\n[build-dependencies]\nruchy = \"3.71\"\n")
    }
}

/// Add both build script and build-dependencies to Cargo.toml
///
/// # Complexity
///
/// Complexity: 3 (within Toyota Way limits ≤10)
fn add_build_script_and_dependencies(content: &str) -> String {
    // Add build = "build.rs" to [package] section
    let with_build = content.replace("[package]", "[package]\nbuild = \"build.rs\"");

    // Add build-dependencies section
    add_build_dependencies(&with_build)
}

/// Create Ruchy source file (main.ruchy or lib.ruchy)
///
/// # Complexity
///
/// Complexity: 5 (within Toyota Way limits ≤10)
fn create_ruchy_source(project_dir: &Path, is_lib: bool) -> Result<()> {
    let (filename, content) = if is_lib {
        ("src/lib.ruchy", get_lib_template())
    } else {
        ("src/main.ruchy", get_main_template())
    };

    let file_path = project_dir.join(filename);
    fs::write(&file_path, content)
        .with_context(|| format!("Failed to write {filename} to {}", file_path.display()))?;

    Ok(())
}

/// Get template content for main.ruchy
///
/// # Complexity
///
/// Complexity: 1 (within Toyota Way limits ≤10)
fn get_main_template() -> &'static str {
    r#"// Ruchy main program
// This file will be automatically transpiled to main.rs during cargo build

fun main() {
    println("Hello, Ruchy!");

    // Example: Using variables
    let name = "World";
    println(f"Hello, {name}!");

    // Example: Using collections
    let numbers = [1, 2, 3, 4, 5];
    println(f"Numbers: {numbers:?}");
}
"#
}

/// Get template content for lib.ruchy
///
/// # Complexity
///
/// Complexity: 1 (within Toyota Way limits ≤10)
fn get_lib_template() -> &'static str {
    r"// Ruchy library
// This file will be automatically transpiled to lib.rs during cargo build

/// Add two numbers together
///
/// # Examples
///
/// ```
/// assert_eq!(add(2, 3), 5);
/// ```
pub fun add(a: i32, b: i32) -> i32 {
    a + b
}

/// Multiply two numbers
///
/// # Examples
///
/// ```
/// assert_eq!(multiply(2, 3), 6);
/// ```
pub fun multiply(a: i32, b: i32) -> i32 {
    a * b
}
"
}

/// Create/update README.md with Ruchy instructions
///
/// # Complexity
///
/// Complexity: 4 (within Toyota Way limits ≤10)
fn create_readme(project_dir: &Path, is_lib: bool) -> Result<()> {
    let readme_path = project_dir.join("README.md");
    let project_name = project_dir
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("ruchy-project");

    let content = get_readme_template(project_name, is_lib);

    // Append to existing README or create new one
    if readme_path.exists() {
        let existing = fs::read_to_string(&readme_path)?;
        fs::write(&readme_path, format!("{existing}\n\n{content}"))?;
    } else {
        fs::write(&readme_path, content)?;
    }

    Ok(())
}

/// Get README template
///
/// # Complexity
///
/// Complexity: 2 (within Toyota Way limits ≤10)
fn get_readme_template(project_name: &str, is_lib: bool) -> String {
    let project_type = if is_lib { "Library" } else { "Application" };

    format!(
        r"# {project_name}

A Ruchy {project_type} with Cargo integration.

## About Ruchy

This project uses Ruchy, a modern systems programming language that transpiles to Rust.
Source files written in `.ruchy` syntax are automatically converted to `.rs` files during build.

## Building

```bash
# Build the project (auto-transpiles .ruchy → .rs)
cargo build

# Run the project{}
cargo run

# Run tests
cargo test

# Clean generated files
cargo clean
```

## Project Structure

- `src/{}` - Ruchy source code (auto-transpiled)
- `build.rs` - Build script for transpilation
- `Cargo.toml` - Project dependencies

## Learn More

- Ruchy Language: https://github.com/paiml/ruchy
- Documentation: https://docs.rs/ruchy
",
        if is_lib { "" } else { "\ncargo run" },
        if is_lib { "lib.ruchy" } else { "main.ruchy" }
    )
}

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

    #[test]
    fn test_get_main_template_not_empty() {
        let template = get_main_template();
        assert!(!template.is_empty());
    }

    #[test]
    fn test_get_main_template_has_fun_main() {
        let template = get_main_template();
        assert!(template.contains("fun main()"));
    }

    #[test]
    fn test_get_main_template_has_println() {
        let template = get_main_template();
        assert!(template.contains("println"));
    }

    #[test]
    fn test_get_lib_template_not_empty() {
        let template = get_lib_template();
        assert!(!template.is_empty());
    }

    #[test]
    fn test_get_lib_template_has_pub_functions() {
        let template = get_lib_template();
        assert!(template.contains("pub fun"));
    }

    #[test]
    fn test_get_lib_template_has_add_function() {
        let template = get_lib_template();
        assert!(template.contains("fun add"));
    }

    #[test]
    fn test_get_lib_template_has_multiply_function() {
        let template = get_lib_template();
        assert!(template.contains("fun multiply"));
    }

    #[test]
    fn test_add_build_dependencies_no_existing_section() {
        let content = "[package]\nname = \"test\"\n[dependencies]\nfoo = \"1.0\"";
        let result = add_build_dependencies(content);
        assert!(result.contains("[build-dependencies]"));
        assert!(result.contains("ruchy = \"3.71\""));
    }

    #[test]
    fn test_add_build_dependencies_with_existing_section() {
        let content = "[package]\nname = \"test\"\n[build-dependencies]\nbar = \"2.0\"";
        let result = add_build_dependencies(content);
        assert!(result.contains("ruchy = \"3.71\""));
        // Should only have one [build-dependencies] header (replaced)
        let count = result.matches("[build-dependencies]").count();
        assert_eq!(count, 1);
    }

    #[test]
    fn test_add_build_script_and_dependencies() {
        let content = "[package]\nname = \"test\"";
        let result = add_build_script_and_dependencies(content);
        assert!(result.contains("build = \"build.rs\""));
        assert!(result.contains("[build-dependencies]"));
        assert!(result.contains("ruchy = \"3.71\""));
    }

    #[test]
    fn test_get_readme_template_for_binary() {
        let result = get_readme_template("my_project", false);
        assert!(result.contains("# my_project"));
        assert!(result.contains("Application"));
        assert!(result.contains("main.ruchy"));
        assert!(result.contains("cargo run"));
    }

    #[test]
    fn test_get_readme_template_for_library() {
        let result = get_readme_template("my_lib", true);
        assert!(result.contains("# my_lib"));
        assert!(result.contains("Library"));
        assert!(result.contains("lib.ruchy"));
    }

    #[test]
    fn test_get_readme_template_has_build_instructions() {
        let result = get_readme_template("test_project", false);
        assert!(result.contains("cargo build"));
        assert!(result.contains("cargo test"));
        assert!(result.contains("cargo clean"));
    }

    #[test]
    fn test_get_readme_template_has_about_section() {
        let result = get_readme_template("test", false);
        assert!(result.contains("## About Ruchy"));
        assert!(result.contains(".ruchy"));
        assert!(result.contains(".rs"));
    }

    #[test]
    fn test_get_readme_template_has_learn_more() {
        let result = get_readme_template("test", false);
        assert!(result.contains("## Learn More"));
        assert!(result.contains("github.com/paiml/ruchy"));
    }

    #[test]
    fn test_main_template_has_variables_example() {
        let template = get_main_template();
        assert!(template.contains("let name"));
    }

    #[test]
    fn test_main_template_has_collections_example() {
        let template = get_main_template();
        assert!(template.contains("let numbers"));
        assert!(template.contains("[1, 2, 3, 4, 5]"));
    }

    #[test]
    fn test_lib_template_has_doctests() {
        let template = get_lib_template();
        assert!(template.contains("/// # Examples"));
        assert!(template.contains("assert_eq!"));
    }
}