xur-std 0.1.4

The standard library of the Xur language
Documentation
use xur_runtime::{Registry, XurStr};

pub(crate) fn register(registry: &Registry) {
    registry
        .module("std::env")
        .function("get", |name: XurStr| std::env::var(&*name).ok())
        .function("set", |name: XurStr, value: XurStr| {
            // Safety: xur scripts control their own threading; this is the
            // expected trade-off for a scripting language.
            unsafe { std::env::set_var(&*name, &*value) };
        })
        .function("remove", |name: XurStr| {
            unsafe { std::env::remove_var(&*name) };
        })
        .function("vars", || {
            std::env::vars()
                .map(|(k, v)| vec![k, v])
                .collect::<Vec<_>>()
        })
        .function("current_dir", || {
            std::env::current_dir()
                .map(|p| p.display().to_string())
                .map_err(crate::io::Error::from)
        })
        .function("home_dir", || std::env::var("HOME").ok())
        .function("exe_path", || {
            std::env::current_exe()
                .map(|p| p.display().to_string())
                .ok()
        });
}