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