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