xur-std 0.1.6

The standard library of the Xur language
Documentation
//! `std::cli`: argument parsing (pico-args) and interactive prompts (inquire).

use std::ffi::OsString;
use std::sync::{Arc, Mutex};

use facet::Facet;
use xur_runtime::convert::into_value;
use xur_runtime::registry::XurType;
use xur_runtime::{
    NativeObject, OptionalArg, Raise, Record, Registry, RunResult, RuntimeError, Value, XurStr,
    lock,
};
use xur_span::Symbol;

use crate::{error_text, option, result};

// ---------------------------------------------------------------------------
// Args — imperative argument parser (pico-args)
// ---------------------------------------------------------------------------

#[derive(Facet)]
#[facet(opaque)]
pub struct Args(Mutex<pico_args::Arguments>);

/// A recoverable CLI-parsing error. Match it as `Err(cli::Error::Unexpected(msg))`.
#[derive(Clone, Facet)]
#[repr(C)]
pub enum Error {
    /// An argument could not be read (missing required value, bad encoding, …).
    Arg(String),
    /// Unexpected leftover arguments after parsing.
    Unexpected(String),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Arg(m) => f.write_str(m),
            Error::Unexpected(m) => write!(f, "unexpected arguments: {m}"),
        }
    }
}

/// Builds pico-args keys from a long key, or a short+long pair.
fn keys_from(first: &str, second: Option<&str>) -> pico_args::Keys {
    match second {
        None => pico_args::Keys::from(leak_str(first)),
        Some(long) => pico_args::Keys::from([leak_str(first), leak_str(long)]),
    }
}

fn with_args<R>(object: &NativeObject, f: impl FnOnce(&Args) -> R) -> RunResult<R> {
    object
        .with(f)
        .ok_or_else(|| RuntimeError::new("expected an `Args` object"))
}

fn leak_str(s: &str) -> &'static str {
    Box::leak(s.to_owned().into_boxed_str())
}

fn drain_args(inner: &mut pico_args::Arguments) -> Vec<OsString> {
    std::mem::replace(inner, pico_args::Arguments::from_vec(Vec::new())).finish()
}

// ---------------------------------------------------------------------------
// Prompts — interactive terminal UI (inquire)
// ---------------------------------------------------------------------------

fn prompt_text(args: &[Value]) -> RunResult<Value> {
    let [Value::Str(message)] = args else {
        return Err(RuntimeError::new("`prompt` takes a message string"));
    };
    inquire::Text::new(message)
        .prompt()
        .map(|s| Value::str(&s))
        .map_err(|e| RuntimeError::new(error_text(e)))
}

fn prompt_confirm(args: &[Value]) -> RunResult<Value> {
    let [Value::Str(message)] = args else {
        return Err(RuntimeError::new("`confirm` takes a message string"));
    };
    inquire::Confirm::new(message)
        .prompt()
        .map(Value::Bool)
        .map_err(|e| RuntimeError::new(error_text(e)))
}

fn prompt_select(args: &[Value]) -> RunResult<Value> {
    let [Value::Str(message), Value::List(options)] = args else {
        return Err(RuntimeError::new(
            "`select` takes a message and a list of strings",
        ));
    };
    let options: Vec<String> = lock(options)
        .iter()
        .map(|v| match v {
            Value::Str(s) => Ok(s.to_string()),
            _ => Err(RuntimeError::new("options must be strings")),
        })
        .collect::<RunResult<_>>()?;
    if options.is_empty() {
        return Err(RuntimeError::new("options list must not be empty"));
    }
    inquire::Select::new(message, options)
        .prompt()
        .map(|s| Value::str(&s))
        .map_err(|e| RuntimeError::new(error_text(e)))
}

fn prompt_multi_select(args: &[Value]) -> RunResult<Value> {
    let [Value::Str(message), Value::List(options)] = args else {
        return Err(RuntimeError::new(
            "`multi_select` takes a message and a list of strings",
        ));
    };
    let options: Vec<String> = lock(options)
        .iter()
        .map(|v| match v {
            Value::Str(s) => Ok(s.to_string()),
            _ => Err(RuntimeError::new("options must be strings")),
        })
        .collect::<RunResult<_>>()?;
    if options.is_empty() {
        return Err(RuntimeError::new("options list must not be empty"));
    }
    inquire::MultiSelect::new(message, options)
        .prompt()
        .map(|selected| Value::list(selected.into_iter().map(|s| Value::str(&s)).collect()))
        .map_err(|e| RuntimeError::new(error_text(e)))
}

fn prompt_password(args: &[Value]) -> RunResult<Value> {
    let [Value::Str(message)] = args else {
        return Err(RuntimeError::new("`password` takes a message string"));
    };
    inquire::Password::new(message)
        .without_confirmation()
        .prompt()
        .map(|s| Value::str(&s))
        .map_err(|e| RuntimeError::new(error_text(e)))
}

fn prompt_editor(args: &[Value]) -> RunResult<Value> {
    let [Value::Str(message)] = args else {
        return Err(RuntimeError::new("`editor` takes a message string"));
    };
    inquire::Editor::new(message)
        .prompt()
        .map(|s| Value::str(&s))
        .map_err(|e| RuntimeError::new(error_text(e)))
}

// ---------------------------------------------------------------------------
// Declarative arg parsing
// ---------------------------------------------------------------------------

fn parse_spec(cx: &dyn xur_runtime::Context, args: &[Value]) -> RunResult<Value> {
    let [Value::List(specs)] = args else {
        return Err(RuntimeError::new("`parse` takes a list of arg specs"));
    };
    let specs: Vec<Vec<String>> = lock(specs)
        .iter()
        .map(|spec| match spec {
            Value::List(items) => {
                let items = lock(items);
                items
                    .iter()
                    .map(|v| match v {
                        Value::Str(s) => Ok(s.to_string()),
                        _ => Err(RuntimeError::new("spec items must be strings")),
                    })
                    .collect::<RunResult<Vec<String>>>()
            }
            _ => Err(RuntimeError::new(
                "each spec must be a list like [\"--name\", \"-n\", \"option\"]",
            )),
        })
        .collect::<RunResult<_>>()?;

    let argv: Vec<OsString> = crate::xur::ARGV
        .get()
        .map(|(_, args)| args.iter().map(OsString::from).collect())
        .unwrap_or_default();
    let mut pargs = pico_args::Arguments::from_vec(argv);

    let mut fields = Vec::new();
    for spec in &specs {
        let (long, short, kind) = match spec.as_slice() {
            [long, kind] => (long.as_str(), None, kind.as_str()),
            [long, short, kind] => (long.as_str(), Some(short.as_str()), kind.as_str()),
            _ => {
                return Err(RuntimeError::new(
                    "each spec must be [long, kind] or [long, short, kind]",
                ));
            }
        };
        let field_name = long.trim_start_matches('-');
        let field_name = field_name.replace('-', "_");
        let keys: pico_args::Keys = match short {
            Some(s) => [leak_str(s), leak_str(long)].into(),
            None => leak_str(long).into(),
        };
        let value = match kind {
            "flag" => Value::Bool(pargs.contains(keys)),
            "option" => {
                let v: Option<String> = pargs
                    .opt_value_from_str(keys)
                    .map_err(|e| RuntimeError::new(error_text(e)))?;
                option(cx, v.map(|s| Value::str(&s)))
            }
            "value" => {
                let v: String = pargs
                    .value_from_str(keys)
                    .map_err(|e| RuntimeError::new(error_text(e)))?;
                Value::str(&v)
            }
            other => {
                return Err(RuntimeError::new(format!(
                    "unknown arg kind `{other}`, expected `flag`, `option`, or `value`"
                )));
            }
        };
        fields.push((Symbol::intern(&field_name), value));
    }

    let remaining =
        std::mem::replace(&mut pargs, pico_args::Arguments::from_vec(Vec::new())).finish();
    if !remaining.is_empty() {
        let names: Vec<_> = remaining
            .iter()
            .map(|os| os.to_string_lossy().into_owned())
            .collect();
        return Ok(result(
            cx,
            Err(format!("unexpected arguments: {}", names.join(", "))),
        ));
    }

    fields.push((Symbol::intern("rest"), Value::list(Vec::new())));

    Ok(result(cx, Ok(Value::Record(Arc::new(Record::new(fields))))))
}

// ---------------------------------------------------------------------------
// Table
// ---------------------------------------------------------------------------

fn table(cx: &mut dyn xur_runtime::Context, args: &[Value]) -> RunResult<Value> {
    use comfy_table::{Table, modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL_CONDENSED};

    let (headers, rows) = match args {
        [Value::List(rows)] => (None, rows),
        [Value::List(headers), Value::List(rows)] => (Some(headers), rows),
        _ => {
            return Err(RuntimeError::new(
                "`table` takes a list of rows, or a header list and a row list",
            ));
        }
    };

    let mut t = Table::new();
    t.load_preset(UTF8_FULL_CONDENSED);
    t.apply_modifier(UTF8_ROUND_CORNERS);

    if let Some(headers) = headers {
        let h: Vec<String> = lock(headers).iter().map(|v| cx.display(v)).collect();
        t.set_header(h);
    }

    for row in lock(rows).iter() {
        let Value::List(cells) = row else {
            return Err(RuntimeError::new("each row must be a list"));
        };
        let cells: Vec<String> = lock(cells).iter().map(|v| cx.display(v)).collect();
        t.add_row(cells);
    }

    Ok(Value::str(&t.to_string()))
}

// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------

pub(crate) fn register(registry: &Registry) {
    registry.lazy_module("std::cli", |module| {
        module.ty::<Error>("Error");
        // Imperative arg parser
        module.raw_function_returns::<Args>("args", |cx, args| {
            if !args.is_empty() {
                return Err(RuntimeError::new("`cli::args` takes no arguments"));
            }
            let argv: Vec<OsString> = crate::xur::ARGV
                .get()
                .map(|(_, args)| args.iter().map(OsString::from).collect())
                .unwrap_or_default();
            let inner = pico_args::Arguments::from_vec(argv);
            into_value(cx, Args(Mutex::new(inner)))
        });
        module
            .ty_borrowing::<Args>("Args")
            .method(
                "contains",
                |a: &Args, key: XurStr, short: OptionalArg<XurStr>| -> bool {
                    a.0.lock()
                        .unwrap()
                        .contains(keys_from(&key, short.0.as_deref()))
                },
            )
            .method(
                "option",
                |a: &Args, key: XurStr, short: OptionalArg<XurStr>| -> Raise<Option<String>> {
                    // The value is returned directly as an Option; a malformed
                    // value (rare) raises.
                    Raise(
                        a.0.lock()
                            .unwrap()
                            .opt_value_from_str(keys_from(&key, short.0.as_deref()))
                            .map_err(|e| RuntimeError::new(error_text(e))),
                    )
                },
            )
            .method(
                "value",
                |a: &Args, key: XurStr, short: OptionalArg<XurStr>| -> Result<String, Error> {
                    a.0.lock()
                        .unwrap()
                        .value_from_str(keys_from(&key, short.0.as_deref()))
                        .map_err(|e| Error::Arg(error_text(e)))
                },
            )
            .method("subcommand", |a: &Args| -> Result<Option<String>, Error> {
                a.0.lock()
                    .unwrap()
                    .subcommand()
                    .map_err(|e| Error::Arg(error_text(e)))
            })
            .method("free", |a: &Args| -> Result<Option<String>, Error> {
                a.0.lock()
                    .unwrap()
                    .opt_free_from_str()
                    .map_err(|e| Error::Arg(error_text(e)))
            })
            .raw_function_returns_xur("remaining", XurType::list::<String>(), |_cx, args| {
                let [Value::Native(object)] = args else {
                    return Err(RuntimeError::new("`remaining` takes no arguments"));
                };
                let remaining = with_args(object, |a| drain_args(&mut a.0.lock().unwrap()))?;
                Ok(Value::list(
                    remaining
                        .into_iter()
                        .map(|os| Value::str(&os.to_string_lossy()))
                        .collect(),
                ))
            })
            .method("finish", |a: &Args| -> Result<(), Error> {
                let remaining = drain_args(&mut a.0.lock().unwrap());
                if remaining.is_empty() {
                    Ok(())
                } else {
                    let names: Vec<_> = remaining
                        .iter()
                        .map(|os| os.to_string_lossy().into_owned())
                        .collect();
                    Err(Error::Unexpected(names.join(", ")))
                }
            });
        // Declarative parsing
        module.named_raw_function("parse", &["specs"], |cx, args| parse_spec(cx, args));
        // Table
        module.named_raw_function_returns::<String>("table", &["headers", "rows"], |cx, args| {
            table(cx, args)
        });
        // Prompts
        module
            .named_raw_function_returns::<String>("prompt", &["message"], |_cx, args| {
                prompt_text(args)
            })
            .named_raw_function_returns::<bool>("confirm", &["message"], |_cx, args| {
                prompt_confirm(args)
            })
            .named_raw_function_returns::<String>("select", &["message", "options"], |_cx, args| {
                prompt_select(args)
            })
            .named_raw_function_returns_xur(
                "multi_select",
                &["message", "options"],
                XurType::list::<String>(),
                |_cx, args| prompt_multi_select(args),
            )
            .named_raw_function_returns::<String>("password", &["message"], |_cx, args| {
                prompt_password(args)
            })
            .named_raw_function_returns::<String>("editor", &["message"], |_cx, args| {
                prompt_editor(args)
            });
    });
}