bashexpand 0.1.0

Bash-style parameter expansion (${var:-default}, ${var@L}, ${var##pat}, ...) over any variable source: maps, closures, the environment, or your own backend.
Documentation
//! Run with: cargo run --example inline_closure
//!
//! The lightest way to use the crate: no map, just a lookup function.

use bashexpand::expand;

fn main() {
    // Annotate the argument as `&str` so type inference resolves the impl.
    let mut ctx = |name: &str| match name {
        "user" => Some(whoami()),
        "shell" => std::env::var("SHELL").ok(),
        _ => None,
    };

    let line = expand(
        "${user@u} uses ${shell##*/} (config: ${cfg:-~/.config/app.toml})",
        &mut ctx,
    )
    .unwrap();
    println!("{line}");
}

fn whoami() -> String {
    std::env::var("USER").unwrap_or_else(|_| "somebody".to_string())
}