puerto 0.34.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.
//! The Tareas tab (Fase 37.8): the project's Taskfile, as `task --list --json` lists it.
use std::path::Path;

#[derive(Debug, Clone, PartialEq)]
pub struct Task {
    pub name: String,
    pub desc: String,
}

/// The tasks, and the binary that lists and runs them.
#[derive(Debug, Clone)]
pub struct Taskfile {
    pub bin: String,
    pub tasks: Vec<Task>,
}

pub const NOT_FOUND: &str =
    "task no está en el PATH: entra en el proyecto con devenv shell (o direnv allow, una vez)";

/// What `task --list --json` prints, as tasks.
pub fn parse(json: &str) -> Result<Vec<Task>, String> {
    let value: serde_json::Value =
        serde_json::from_str(json).map_err(|e| format!("task --list --json: {e}"))?;
    let tasks = value["tasks"]
        .as_array()
        .ok_or("task --list --json no da una lista de tareas")?;
    Ok(tasks
        .iter()
        .filter_map(|t| {
            Some(Task {
                name: t["name"].as_str()?.to_string(),
                desc: t["desc"].as_str().unwrap_or_default().to_string(),
            })
        })
        .collect())
}

/// The tasks of the project at `base`. Arch and Fedora package Task as `go-task` (their `task` is
/// Taskwarrior): the first one that lists them as JSON is it.
pub fn load(base: &Path) -> Result<Taskfile, String> {
    let mut failure = None;
    for bin in ["go-task", "task"] {
        let Ok(output) = std::process::Command::new(bin)
            .args(["--list", "--json"])
            .current_dir(base)
            .stdin(std::process::Stdio::null())
            .output()
        else {
            continue;
        };
        match parse(&String::from_utf8_lossy(&output.stdout)) {
            Ok(tasks) if output.status.success() => {
                return Ok(Taskfile {
                    bin: bin.to_string(),
                    tasks,
                });
            }
            Ok(_) | Err(_) => {
                let stderr = String::from_utf8_lossy(&output.stderr);
                failure.get_or_insert(format!("{bin}: {}", stderr.lines().next().unwrap_or("")));
            }
        }
    }
    Err(failure.unwrap_or_else(|| NOT_FOUND.to_string()))
}

/// The tasks whose name has the first word of `line` in it (all, with nothing written).
pub fn filtered<'a>(tasks: &'a [Task], line: Option<&str>) -> Vec<&'a Task> {
    let word = line
        .and_then(|l| l.split_whitespace().next())
        .unwrap_or("")
        .to_lowercase();
    tasks
        .iter()
        .filter(|t| t.name.to_lowercase().contains(&word))
        .collect()
}