windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
Documentation
// Example 48: File System Operations
// Demonstrates Windjammer's std.fs module

use std::fs

fn main() {
    println!("=== File System Demo ===")
    println!()
    
    // 1. Write and read files
    println!("1. File Operations:")
    
    match fs.write("test.txt", "Hello from Windjammer!") {
        Ok(_) => println!("   ✓ Wrote test.txt"),
        Err(e) => println!("   ✗ Write failed: {}", e)
    }
    
    match fs.read_to_string("test.txt") {
        Ok(contents) => println!("   ✓ Read: {}", contents),
        Err(e) => println!("   ✗ Read failed: {}", e)
    }
    
    // Append to file
    match fs.append("test.txt", "\nAppended line!") {
        Ok(_) => println!("   ✓ Appended to test.txt"),
        Err(e) => println!("   ✗ Append failed: {}", e)
    }
    
    println!()
    
    // 2. File existence and type checks
    println!("2. File Checks:")
    
    if fs.exists("test.txt") {
        println!("   ✓ test.txt exists")
    }
    
    if fs.is_file("test.txt") {
        println!("   ✓ test.txt is a file")
    }
    
    if fs.is_dir(".") {
        println!("   ✓ Current directory is a directory")
    }
    
    println!()
    
    // 3. Directory operations
    println!("3. Directory Operations:")
    
    match fs.create_dir_all("test_dir/sub_dir") {
        Ok(_) => println!("   ✓ Created test_dir/sub_dir"),
        Err(e) => println!("   ✗ Create failed: {}", e)
    }
    
    // Write a file in the directory
    match fs.write("test_dir/file1.txt", "File 1") {
        Ok(_) => println!("   ✓ Created test_dir/file1.txt"),
        Err(e) => println!("   ✗ Write failed: {}", e)
    }
    
    match fs.write("test_dir/file2.txt", "File 2") {
        Ok(_) => println!("   ✓ Created test_dir/file2.txt"),
        Err(e) => println!("   ✗ Write failed: {}", e)
    }
    
    println!()
    
    // 4. List directory contents
    println!("4. List Directory:")
    
    match fs.read_dir("test_dir") {
        Ok(entries) => {
            println!("   Contents of test_dir:")
            for entry in entries {
                let type_str = if entry.is_dir() { "di" } else { "file" }
                println!("     - {} ({})", entry.name(), type_str)
            }
        }
        Err(e) => println!("   ✗ Read dir failed: {}", e)
    }
    
    println!()
    
    // 5. File metadata
    println!("5. File Metadata:")
    
    match fs.metadata("test.txt") {
        Ok(meta) => {
            println!("   test.txt:")
            println!("     Size: {} bytes", meta.size())
            println!("     Is file: {}", meta.is_file())
            println!("     Is directory: {}", meta.is_dir())
            println!("     Read-only: {}", meta.is_readonly())
        }
        Err(e) => println!("   ✗ Metadata failed: {}", e)
    }
    
    println!()
    
    // 6. Path operations
    println!("6. Path Utilities:")
    
    let path = fs.join("test_di", "file1.txt")
    println!("   Joined path: {}", path)
    
    match fs.file_name("test_dir/file1.txt") {
        Some(name) => println!("   File name: {}", name),
        None => println!("   No file name")
    }
    
    match fs.extension("file1.txt") {
        Some(ext) => println!("   Extension: {}", ext),
        None => println!("   No extension")
    }
    
    match fs.file_stem("file1.txt") {
        Some(stem) => println!("   File stem: {}", stem),
        None => println!("   No file stem")
    }
    
    match fs.current_dir() {
        Ok(dir) => println!("   Current dir: {}", dir),
        Err(e) => println!("   ✗ Get current dir failed: {}", e)
    }
    
    println!()
    
    // 7. Copy and rename
    println!("7. Copy and Rename:")
    
    match fs.copy("test.txt", "test_copy.txt") {
        Ok(_) => println!("   ✓ Copied test.txt -> test_copy.txt"),
        Err(e) => println!("   ✗ Copy failed: {}", e)
    }
    
    match fs.rename("test_copy.txt", "test_renamed.txt") {
        Ok(_) => println!("   ✓ Renamed test_copy.txt -> test_renamed.txt"),
        Err(e) => println!("   ✗ Rename failed: {}", e)
    }
    
    println!()
    
    // 8. Cleanup
    println!("8. Cleanup:")
    
    match fs.remove_file("test.txt") {
        Ok(_) => println!("   ✓ Removed test.txt"),
        Err(e) => println!("   ✗ Remove failed: {}", e)
    }
    
    match fs.remove_file("test_renamed.txt") {
        Ok(_) => println!("   ✓ Removed test_renamed.txt"),
        Err(e) => println!("   ✗ Remove failed: {}", e)
    }
    
    match fs.remove_dir_all("test_dir") {
        Ok(_) => println!("   ✓ Removed test_dir/"),
        Err(e) => println!("   ✗ Remove dir failed: {}", e)
    }
    
    println!()
    println!("✨ File system operations with Windjammer!")
    println!("   ✅ Using std.fs abstraction (not std::fs directly)")
    println!("   ✅ Clean, Windjammer-native API")
    println!("   ✅ No external dependencies needed")
    println!("   ✅ Uses Rust's std::fs under the hood")
}