nanorust 0.1.0

A terminal-based text editor written in Rust, inspired by GNU nano
Documentation
use nanorust::config::Config;
use nanorust::editor::Editor;
use nanorust::Result;
use std::path::Path;

/// Main entry point for the nano rust editor
/// 
/// Parses command-line arguments, creates an editor instance with the provided
/// configuration, optionally opens a file, and starts the main event loop.
/// 
/// # Exit Codes
/// - 0: Success
/// - 1: Error occurred (file not found, terminal initialization failure, etc.)
fn main() {
    // Run the editor and handle any errors
    if let Err(e) = run() {
        // Print error to stderr
        eprintln!("Error: {}", e);
        
        // Exit with error code
        std::process::exit(1);
    }
    
    // Exit successfully
    std::process::exit(0);
}

/// Main application logic
/// 
/// This function contains the core logic for initializing and running the editor.
/// It's separated from main() to allow proper error handling and cleanup.
fn run() -> Result<()> {
    // Parse command-line arguments using Config::from_args()
    let (config, filename) = Config::from_args();
    
    // Create editor instance with config using Editor::with_config(config)
    // This may fail if terminal initialization fails
    let mut editor = Editor::with_config(config)?;
    
    // If filename provided in args, call editor.open_file(filename)
    if let Some(file) = filename {
        let path = Path::new(&file);
        
        // Attempt to open the file
        // If it fails, we still want to start the editor with an empty buffer
        // but show an error message
        if let Err(e) = editor.open_file(path) {
            // For critical errors like file not found, we could either:
            // 1. Exit immediately (strict behavior)
            // 2. Start with empty buffer and show error (nano-like behavior)
            // We'll use option 2 for better user experience
            eprintln!("Warning: Could not open file '{}': {}", file, e);
            eprintln!("Starting with empty buffer...");
        }
    }
    
    // Call editor.run() to start event loop
    // This will run until the user quits
    editor.run()?;
    
    // Terminal is restored via Drop trait in Editor/Terminal
    // Exit with appropriate exit code (0 for success)
    Ok(())
}