1mod any;
8mod assert;
9mod bytes;
10mod cell;
11#[cfg(feature = "cli")]
12mod cli;
13#[cfg(feature = "datetime")]
14mod datetime;
15#[cfg(feature = "rust_decimal")]
16pub mod decimal;
17#[cfg(feature = "env")]
18mod env;
19pub mod fmt;
20#[cfg(feature = "fs")]
21mod fs;
22#[cfg(feature = "egui-widgets")]
23pub mod gui;
24#[cfg(feature = "hash")]
25mod hash;
26#[cfg(feature = "http")]
27mod http;
28pub mod io;
29#[cfg(feature = "json")]
30mod json;
31mod list;
32#[cfg(feature = "log")]
33mod log;
34mod map;
35mod math;
36mod mem;
37mod native_enum_test;
38#[cfg(feature = "net")]
39mod net;
40mod num;
41mod path;
42#[cfg(feature = "process")]
43mod process;
44#[cfg(feature = "ptr")]
45mod ptr;
46#[cfg(feature = "random")]
47mod random;
48pub mod re;
49mod record;
50mod reflect;
51#[cfg(feature = "regex")]
52mod regex;
53mod str;
54mod sync;
55#[cfg(feature = "thread")]
56mod thread;
57#[cfg(feature = "time")]
58mod time;
59mod xur;
60
61pub use xur::{set_argv, set_invoked_by};
62
63include!(concat!(env!("OUT_DIR"), "/prelude.rs"));
64
65use xur_runtime::registry::{ElemHint, XurType};
66use xur_runtime::{Context, Registry, RunResult, RuntimeError, Value};
67
68pub fn registry() -> Registry {
70 let registry = Registry::new();
71 register(®istry);
72 registry
73}
74
75pub fn register(registry: &Registry) {
77 registry
78 .module("std")
79 .named_raw_function("import", &["path"], import)
80 .named_raw_function_returns_xur(
81 "try_import",
82 &["path"],
83 XurType::Named("Result", &[ElemHint::Name("module"), ElemHint::Name("str")]),
84 try_import,
85 )
86 .named_raw_function_returns_xur("panic", &["format", "args"], XurType::UNIT, panic)
87 .named_raw_function_returns_xur("argv", &[], XurType::list::<String>(), argv);
88 registry
89 .module("std::fmt")
90 .named_raw_function_returns::<String>("format", &["template", "args"], format);
91 io::register(registry);
92 #[cfg(feature = "fs")]
93 fs::register(registry);
94 #[cfg(feature = "process")]
95 process::register(registry);
96 #[cfg(feature = "thread")]
97 thread::register(registry);
98 sync::register(registry);
99 #[cfg(feature = "net")]
100 net::register(registry);
101 any::register(registry);
102 assert::register(registry);
103 reflect::register(registry);
104 #[cfg(feature = "egui-widgets")]
105 gui::register(registry);
106 #[cfg(feature = "http")]
107 http::register(registry);
108 #[cfg(feature = "json")]
109 json::register(registry);
110 re::register(registry);
111 math::register(registry);
112 mem::register(registry);
113 native_enum_test::register(registry);
114 list::register(registry);
115 #[cfg(feature = "log")]
116 log::register(registry);
117 map::register(registry);
118 bytes::register(registry);
119 cell::register(registry);
120 str::register(registry);
121 num::register(registry);
122 path::register(registry);
123 #[cfg(feature = "ptr")]
124 ptr::register(registry);
125 record::register(registry);
126 #[cfg(feature = "regex")]
127 regex::register(registry);
128 #[cfg(feature = "datetime")]
129 datetime::register(registry);
130 #[cfg(feature = "time")]
131 time::register(registry);
132 #[cfg(feature = "env")]
133 env::register(registry);
134 #[cfg(feature = "random")]
135 random::register(registry);
136 #[cfg(feature = "hash")]
137 hash::register(registry);
138 xur::register(registry);
139 #[cfg(feature = "cli")]
140 cli::register(registry);
141 #[cfg(feature = "rust_decimal")]
142 decimal::register(registry);
143}
144
145fn import(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
147 match args {
148 [Value::Str(path)] => cx.import(path),
149 _ => Err(RuntimeError::new(
150 "`std::import` takes a single path string",
151 )),
152 }
153}
154
155fn try_import(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
158 match args {
159 [Value::Str(path)] => {
160 let lang = *cx.lang();
161 match cx.import(path) {
162 Ok(module) => Ok(Value::adt(lang.ok, vec![module])),
163 Err(error) => Ok(Value::adt(lang.err, vec![Value::str(&error.message)])),
164 }
165 }
166 _ => Err(RuntimeError::new(
167 "`std::try_import` takes a single path string",
168 )),
169 }
170}
171
172fn option(cx: &dyn Context, value: Option<Value>) -> Value {
174 let lang = cx.lang();
175 match value {
176 Some(value) => Value::adt(lang.some, vec![value]),
177 None => Value::adt(lang.none, Vec::new()),
178 }
179}
180
181fn result(cx: &dyn Context, value: Result<Value, String>) -> Value {
183 let lang = cx.lang();
184 match value {
185 Ok(value) => Value::adt(lang.ok, vec![value]),
186 Err(message) => Value::adt(lang.err, vec![Value::str(&message)]),
187 }
188}
189
190fn error_text(error: impl std::fmt::Display) -> String {
192 error.to_string()
193}
194
195fn format(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
197 let Some((Value::Str(template), args)) = args.split_first() else {
198 return Err(RuntimeError::new("`format` takes a format string"));
199 };
200 Ok(Value::str(&fmt::format(cx, template, args)?))
201}
202
203fn argv(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
205 if !args.is_empty() {
206 return Err(RuntimeError::new("`std::argv` takes no arguments"));
207 }
208 let Some((script, rest)) = xur::ARGV.get() else {
209 return Ok(Value::list(Vec::new()));
210 };
211 let mut items: Vec<Value> = Vec::with_capacity(1 + rest.len());
212 items.push(option(cx, script.clone()));
213 items.extend(rest.iter().map(|a| Value::str(a)));
214 Ok(Value::list(items))
215}
216
217fn panic(cx: &mut dyn Context, args: &[Value]) -> RunResult<Value> {
220 let Some((Value::Str(template), args)) = args.split_first() else {
221 return Err(RuntimeError::new("`panic` takes a format string"));
222 };
223 Err(RuntimeError::new(fmt::format(cx, template, args)?))
224}