puerto 0.33.0

Puerto: la CLI de un framework full-stack DDD en Rust. Genera proyectos con Clean Architecture, la inyección de dependencias cableada sola y soporte de SQLx, sin cablear nada a mano. Antes, puerto-framework.
//! `puerto generar --simular <subcomando> …` (Fase 37.5): the same command on a throwaway copy of
//! the project, and what it would change there. Nothing is written in the project.
use crate::tree::{self, Change};
use std::path::{Path, PathBuf};

/// `run` on a copy of the project at `cwd`; its changes there, or its error.
pub fn simulate(
    cwd: &Path,
    run: impl FnOnce(&Path) -> Result<(), Box<dyn std::error::Error>>,
) -> Result<Vec<(PathBuf, Change)>, Box<dyn std::error::Error>> {
    let copy = tempfile::tempdir()?;
    tree::copy_project(cwd, copy.path())?;
    let before = tree::snapshot(copy.path())?;
    run(copy.path())?;
    Ok(tree::changes(&before, &tree::snapshot(copy.path())?))
}

/// `+ ruta`, `~ ruta (+3 −1)`, `- ruta`, one a line.
pub fn report(changes: &[(PathBuf, Change)]) -> String {
    if changes.is_empty() {
        return "Nada cambiaría.\n".into();
    }
    changes
        .iter()
        .map(|(path, change)| match change {
            Change::Created => format!("+ {}\n", path.display()),
            Change::Modified { added, removed } => {
                format!("~ {} (+{added} −{removed})\n", path.display())
            }
            Change::Deleted => format!("- {}\n", path.display()),
        })
        .collect()
}

/// The command line again, without its `--simular` (only the flag: a field after `--` stays).
pub fn without_simulate(args: Vec<String>) -> Vec<String> {
    let fields = args.iter().position(|a| a == "--").unwrap_or(args.len());
    args.into_iter()
        .enumerate()
        .filter(|(at, a)| *at >= fields || a != "--simular")
        .map(|(_, a)| a)
        .collect()
}

/// This CLI in `dir` with `args`: its questions on the terminal, its output only if it fails.
pub fn run_cli_in(dir: &Path, args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
    use std::process::{Command, Stdio};
    let output = Command::new(std::env::current_exe()?)
        .args(args)
        .current_dir(dir)
        .stdin(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()?;
    if output.status.success() {
        return Ok(());
    }
    print!("{}", String::from_utf8_lossy(&output.stdout));
    Err("(simulación: no se ha escrito nada en el proyecto)".into())
}