mod any;
mod assert;
mod bytes;
mod cell;
#[cfg(feature = "cli")]
mod cli;
#[cfg(feature = "datetime")]
mod datetime;
#[cfg(feature = "rust_decimal")]
pub mod decimal;
#[cfg(feature = "env")]
mod env;
mod features;
pub mod fmt;
#[cfg(feature = "fs")]
mod fs;
#[cfg(feature = "egui-widgets")]
pub mod gui;
#[cfg(feature = "hash")]
mod hash;
#[cfg(feature = "http")]
mod http;
pub mod io;
#[cfg(feature = "json")]
mod json;
mod list;
#[cfg(feature = "log")]
mod log;
mod map;
mod math;
mod mem;
mod native_enum_test;
#[cfg(feature = "net")]
mod net;
mod num;
mod path;
#[cfg(feature = "process")]
mod process;
#[cfg(feature = "ptr")]
mod ptr;
#[cfg(feature = "random")]
mod random;
pub mod re;
mod record;
mod reflect;
#[cfg(feature = "regex")]
mod regex;
mod str;
mod sync;
#[cfg(feature = "thread")]
mod thread;
#[cfg(feature = "time")]
mod time;
mod xur;
pub use features::module_feature;
pub use xur::{set_argv, set_invoked_by};
include!(concat!(env!("OUT_DIR"), "/prelude.rs"));
use xur_runtime::registry::{ElemHint, XurType};
use xur_runtime::{Context, Registry, RunResult, RuntimeError, Value};
pub fn registry() -> Registry {
let registry = Registry::new();
register(®istry);
registry
}
pub fn register(registry: &Registry) {
registry
.module("std")
.named_raw_function("import", &["path"], import)
.named_raw_function_returns_xur(
"try_import",
&["path"],
XurType::Named("Result", &[ElemHint::Name("module"), ElemHint::Name("str")]),
try_import,
)
.named_raw_function_returns_xur("panic", &["format", "args"], XurType::UNIT, panic)
.named_raw_function_returns_xur("argv", &[], XurType::list::<String>(), argv);
registry
.module("std::fmt")
.named_raw_function_returns::<String>("format", &["template", "args"], format);
io::register(registry);
#[cfg(feature = "fs")]
fs::register(registry);
#[cfg(feature = "process")]
process::register(registry);
#[cfg(feature = "thread")]
thread::register(registry);
sync::register(registry);
#[cfg(feature = "net")]
net::register(registry);
any::register(registry);
assert::register(registry);
reflect::register(registry);
#[cfg(feature = "egui-widgets")]
gui::register(registry);
#[cfg(feature = "http")]
http::register(registry);
#[cfg(feature = "json")]
json::register(registry);
re::register(registry);
math::register(registry);
mem::register(registry);
native_enum_test::register(registry);
list::register(registry);
#[cfg(feature = "log")]
log::register(registry);
map::register(registry);
bytes::register(registry);
cell::register(registry);
str::register(registry);
num::register(registry);
path::register(registry);
#[cfg(feature = "ptr")]
ptr::register(registry);
record::register(registry);
#[cfg(feature = "regex")]
regex::register(registry);
#[cfg(feature = "datetime")]
datetime::register(registry);
#[cfg(feature = "time")]
time::register(registry);
#[cfg(feature = "env")]
env::register(registry);
#[cfg(feature = "random")]
random::register(registry);
#[cfg(feature = "hash")]
hash::register(registry);
xur::register(registry);
#[cfg(feature = "cli")]
cli::register(registry);
#[cfg(feature = "rust_decimal")]
decimal::register(registry);
}
fn import(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
match args {
[Value::Str(path)] => cx.import(path),
_ => Err(RuntimeError::new(
"`std::import` takes a single path string",
)),
}
}
fn try_import(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
match args {
[Value::Str(path)] => {
let lang = *cx.lang();
match cx.import(path) {
Ok(module) => Ok(Value::adt(lang.ok, vec![module])),
Err(error) => Ok(Value::adt(lang.err, vec![Value::str(&error.message)])),
}
}
_ => Err(RuntimeError::new(
"`std::try_import` takes a single path string",
)),
}
}
fn option(cx: &dyn Context, value: Option<Value>) -> Value {
let lang = cx.lang();
match value {
Some(value) => Value::adt(lang.some, vec![value]),
None => Value::adt(lang.none, Vec::new()),
}
}
fn result(cx: &dyn Context, value: Result<Value, String>) -> Value {
let lang = cx.lang();
match value {
Ok(value) => Value::adt(lang.ok, vec![value]),
Err(message) => Value::adt(lang.err, vec![Value::str(&message)]),
}
}
fn error_text(error: impl std::fmt::Display) -> String {
error.to_string()
}
fn format(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let Some((Value::Str(template), args)) = args.split_first() else {
return Err(RuntimeError::new("`format` takes a format string"));
};
Ok(Value::str(&fmt::format(cx, template, args)?))
}
fn argv(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
if !args.is_empty() {
return Err(RuntimeError::new("`std::argv` takes no arguments"));
}
let Some((script, rest)) = xur::ARGV.get() else {
return Ok(Value::list(Vec::new()));
};
let mut items: Vec<Value> = Vec::with_capacity(1 + rest.len());
items.push(option(cx, script.clone()));
items.extend(rest.iter().map(|a| Value::str(a)));
Ok(Value::list(items))
}
fn panic(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
let Some((Value::Str(template), args)) = args.split_first() else {
return Err(RuntimeError::new("`panic` takes a format string"));
};
Err(RuntimeError::new(fmt::format(cx, template, args)?))
}