1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use std::cell::RefCell;
use std::rc::Rc;
use miette::Result;
use miette::miette;
use mlua::prelude::*;
use tracing::info;
use crate::assertions::AssertionType;
use crate::assertions::LuaAssertionType;
use crate::assertions::Status;
use crate::error::FrorkError;
use crate::registry::Registry;
use crate::utils::Utils;
/// Handles each assertion's status once it has been evaluated. The binary
/// supplies the rendering and any prompting; nothing here writes to stdout.
pub trait StatusHandler: Fn(&Status, &dyn AssertionType) -> Result<()> + Clone + 'static {}
impl<T> StatusHandler for T where T: Fn(&Status, &dyn AssertionType) -> Result<()> + Clone + 'static {}
/// Evaluates inline Fennel code.
pub fn run_code(code: &str, handle_status: impl StatusHandler) -> Result<()> {
let (lua, frork_module, fennel_module) = setup_lua(handle_status)?;
let ok_fn: LuaFunction = frork_module
.get("ok")
.map_err(|e| miette!("Failed to get frork.ok: {e}"))?;
lua.globals()
.set("ok", ok_fn)
.map_err(|e| miette!("Failed to set ok global: {e}"))?;
let eval_fn: LuaFunction = fennel_module
.get("eval")
.map_err(|e| miette!("Failed to get fennel.eval: {e}"))?;
eval_fn
.call::<()>(code)
.map_err(|e| miette!("Failed to execute fennel code: {e}"))?;
Ok(())
}
/// Evaluates a `.fnl` script file, with its directory on the search path.
pub fn run_script(script: &str, handle_status: impl StatusHandler) -> Result<()> {
let (lua, _frork_module, fennel_module) = setup_lua(handle_status)?;
// Add script directory to Lua and Fennel search paths.
let script_path = std::path::Path::new(script);
if let Some(script_dir) = script_path.parent()
&& let Some(script_dir_str) = script_dir.to_str()
{
let package_table: LuaTable = lua
.globals()
.get("package")
.map_err(|e| miette!("Failed to get package table: {e}"))?;
// Add to Lua search path.
let current_path: String = package_table
.get("path")
.map_err(|e| miette!("Failed to get current Lua path: {e}"))?;
let new_path = format!(
"{};{}/?.lua;{}/?/init.lua",
current_path, script_dir_str, script_dir_str
);
package_table
.set("path", new_path)
.map_err(|e| miette!("Failed to set Lua path: {e}"))?;
// Add to Fennel search path.
let current_fennel_path: String = fennel_module
.get("path")
.unwrap_or_else(|_| "./?.fnl;./?/init.fnl".to_string());
let new_fennel_path = format!(
"{};{}/?.fnl;{}/?/init.fnl",
current_fennel_path, script_dir_str, script_dir_str
);
fennel_module
.set("path", new_fennel_path)
.map_err(|e| miette!("Failed to set Fennel path: {e}"))?;
}
lua.load(format!(
r#"require("fennel").install().dofile("{}")"#,
script
))
.exec()
.map_err(|e| miette!("Failed to execute script: {e}"))?;
Ok(())
}
fn setup_lua(handle_status: impl StatusHandler) -> Result<(Lua, LuaTable, LuaTable)> {
let lua = Lua::new();
let fennel_code = include_str!("../fennel-1.6.0.lua");
let fennel_module: LuaTable = lua
.load(fennel_code)
.eval()
.map_err(|e| miette!("Failed to load Fennel: {e}"))?;
lua.register_module("fennel", &fennel_module)
.map_err(|e| miette!("Failed to register fennel module: {e}"))?;
let frork_value = Frork::new(handle_status, lua.clone())
.into_lua(&lua)
.map_err(|e| miette!("Failed to create frork table: {e}"))?;
let LuaValue::Table(frork_table) = frork_value else {
// Frork::into_lua always returns a table.
unreachable!();
};
lua.register_module("frork", &frork_table)
.map_err(|e| miette!("Failed to register frork module: {e}"))?;
Ok((lua, frork_table, fennel_module))
}
struct Frork<F> {
// RefCell needed for interior mutability — register() adds new assertion
// types at runtime when called from Lua/Fennel code.
registry: RefCell<Registry>,
handle_status: F,
lua: Lua,
}
impl<F> Frork<F> {
fn new(handle_status: F, lua: Lua) -> Self {
Self {
registry: RefCell::new(Registry::default()),
handle_status,
lua,
}
}
}
impl<F: StatusHandler> Frork<F> {
fn register(&self, name: &str, lua_assertion_type: LuaAssertionType) -> LuaResult<()> {
self.registry
.borrow_mut()
.register(name, lua_assertion_type);
info!("Registered assertion type: {}", name);
Ok(())
}
fn ok(&self, args: LuaMultiValue) -> LuaResult<()> {
if args.is_empty() {
return Err(LuaError::external(FrorkError::NoOperation));
}
let mut args_iter = args.into_iter();
let assertion_type = args_iter
.next()
.and_then(|v| v.to_string().ok())
.ok_or_else(|| LuaError::external(FrorkError::MissingAssertionType))?;
let assertion_args: LuaMultiValue = args_iter.collect();
let factory = self
.registry
.borrow()
.get_factory(&assertion_type)
.map_err(LuaError::external)?;
let assertion = factory
.create(&self.lua, assertion_args)
.map_err(LuaError::external)?;
let status = assertion.status().map_err(LuaError::external)?;
(self.handle_status)(&status, assertion.as_ref()).map_err(LuaError::external)?;
Ok(())
}
}
impl<F: StatusHandler> IntoLua for Frork<F> {
fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
let frork_table = lua.create_table()?;
let frork = Rc::new(self);
let frork_clone = frork.clone();
// The &Lua annotation is required: without it the closure infers a
// single concrete lifetime and no longer satisfies create_function.
let register = move |_lua: &Lua, (name, assertion_type): (String, LuaAssertionType)| {
frork_clone.register(&name, assertion_type)
};
let register_fn = lua.create_function(register)?;
frork_table.set("register", register_fn)?;
let frork_clone = frork.clone();
let ok_fn = lua.create_function(move |_lua, args: LuaMultiValue| frork_clone.ok(args))?;
frork_table.set("ok", ok_fn)?;
frork_table.set("utils", Utils {})?;
Ok(LuaValue::Table(frork_table))
}
}
#[cfg(test)]
mod tests {
use super::*;
use fs_err as fs;
use tempfile::TempDir;
type Recorded = Rc<RefCell<Vec<String>>>;
/// A status handler that records what it was handed, standing in for the
/// binary's printing.
fn recorder() -> (Recorded, impl StatusHandler) {
let seen: Recorded = Rc::new(RefCell::new(Vec::new()));
let sink = seen.clone();
let handler = move |status: &Status, assertion: &dyn AssertionType| {
sink.borrow_mut().push(format!("{status:?}: {assertion}"));
Ok(())
};
(seen, handler)
}
#[test]
fn test_run_code_evaluates_fennel_and_reports_status() {
let (seen, handler) = recorder();
run_code(r#"(ok :directory "/tmp")"#, handler).unwrap();
assert_eq!(seen.borrow().as_slice(), ["Ok: directory /tmp"]);
}
#[test]
fn test_run_code_reports_each_assertion_in_order() {
let (seen, handler) = recorder();
run_code(
r#"(ok :directory "/tmp")
(ok :directory "/frork-does-not-exist")"#,
handler,
)
.unwrap();
assert_eq!(
seen.borrow().as_slice(),
[
"Ok: directory /tmp",
"Missing: directory /frork-does-not-exist",
]
);
}
#[test]
fn test_register_adds_a_type_usable_from_fennel() {
let (seen, handler) = recorder();
run_code(
r#"(local frork (require :frork))
(frork.register :always-missing
{:status (fn [] "missing")
:install (fn [] nil)
:display (fn [a] (.. "custom " a))})
(ok :always-missing "thing")"#,
handler,
)
.unwrap();
assert_eq!(seen.borrow().as_slice(), ["Missing: custom thing"]);
}
#[test]
fn test_ok_without_arguments_is_rejected() {
let (_seen, handler) = recorder();
let error = run_code("(ok)", handler).unwrap_err();
assert!(error.to_string().contains("Failed to execute fennel code"));
}
#[test]
fn test_ok_rejects_unknown_assertion_types() {
let (seen, handler) = recorder();
assert!(run_code(r#"(ok :not-a-real-type)"#, handler).is_err());
assert!(seen.borrow().is_empty());
}
#[test]
fn test_run_code_propagates_handler_failures() {
let handler =
|_status: &Status, _assertion: &dyn AssertionType| Err(miette!("handler said no"));
assert!(run_code(r#"(ok :directory "/tmp")"#, handler).is_err());
}
#[test]
fn test_run_code_rejects_invalid_fennel() {
let (_seen, handler) = recorder();
assert!(run_code("(this is not (valid", handler).is_err());
}
#[test]
fn test_frork_utils_are_exposed_to_fennel() {
let (seen, handler) = recorder();
run_code(
r#"(local frork (require :frork))
(ok :directory (frork.utils.dirname "/tmp/b.txt"))"#,
handler,
)
.unwrap();
assert_eq!(seen.borrow().as_slice(), ["Ok: directory /tmp"]);
}
#[test]
fn test_run_script_evaluates_a_file() {
let dir = TempDir::new().unwrap();
let script = dir.path().join("check.fnl");
fs::write(
&script,
r#"(local frork (require :frork))
(frork.ok :directory "/tmp")"#,
)
.unwrap();
let (seen, handler) = recorder();
run_script(script.to_str().unwrap(), handler).unwrap();
assert_eq!(seen.borrow().as_slice(), ["Ok: directory /tmp"]);
}
#[test]
fn test_run_script_puts_the_script_directory_on_the_search_path() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("helper.fnl"), r#"{:target "/tmp"}"#).unwrap();
let script = dir.path().join("main.fnl");
fs::write(
&script,
r#"(local frork (require :frork))
(local helper (require :helper))
(frork.ok :directory helper.target)"#,
)
.unwrap();
let (seen, handler) = recorder();
run_script(script.to_str().unwrap(), handler).unwrap();
assert_eq!(seen.borrow().as_slice(), ["Ok: directory /tmp"]);
}
#[test]
fn test_run_script_without_a_parent_directory() {
let (_seen, handler) = recorder();
// An empty path has no parent, so the search-path block is skipped.
assert!(run_script("", handler).is_err());
}
#[test]
fn test_run_script_reports_a_missing_file() {
let (_seen, handler) = recorder();
let error = run_script("/frork-does-not-exist/nope.fnl", handler).unwrap_err();
assert!(error.to_string().contains("Failed to execute script"));
}
}