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
//! Embedded scripting (Lua / JS) entry points. Port of `ffi/scripting.rs`.
//!
//! The module as a whole is gated in `bridge/mod.rs` behind
//! `any(feature = "scripting-lua", feature = "scripting-js")`, mirroring the old
//! per-function gating; within the module, the engine-specific methods keep a stable
//! signature and fail with `InvalidArgument` when their engine is compiled out
//! (feature-dependent behavior lives in the body, per the porting rules).
#[diplomat::bridge]
pub mod ffi {
use super::super::schematic::ffi::Schematic;
use super::super::shared::ffi::NucleationError;
/// Namespace for the script-runner free functions of the old ABI
/// (`run_lua_script`, `run_js_script`, `run_script`).
#[diplomat::opaque]
pub struct Scripting;
impl Scripting {
/// Run a Lua script file. Returns the schematic the script assigns to
/// `result`; `NotFound` if it produced none, `Parse` if it failed, and
/// `InvalidArgument` when built without the `scripting-lua` feature.
pub fn run_lua_script(path: &DiplomatStr) -> Result<Box<Schematic>, NucleationError> {
let path = std::str::from_utf8(path).map_err(|_| NucleationError::InvalidArgument)?;
#[cfg(feature = "scripting-lua")]
{
match crate::scripting::lua_engine::run_lua_script(path) {
Ok(Some(ss)) => Ok(Box::new(Schematic(ss.inner))),
Ok(None) => Err(NucleationError::NotFound),
Err(_) => Err(NucleationError::Parse),
}
}
#[cfg(not(feature = "scripting-lua"))]
{
let _ = path;
Err(NucleationError::InvalidArgument)
}
}
/// Run a JS script file. Returns the schematic the script assigns to
/// `result`; `NotFound` if it produced none, `Parse` if it failed, and
/// `InvalidArgument` when built without the `scripting-js` feature.
pub fn run_js_script(path: &DiplomatStr) -> Result<Box<Schematic>, NucleationError> {
let path = std::str::from_utf8(path).map_err(|_| NucleationError::InvalidArgument)?;
#[cfg(feature = "scripting-js")]
{
match crate::scripting::js_engine::run_js_script(path) {
Ok(Some(ss)) => Ok(Box::new(Schematic(ss.inner))),
Ok(None) => Err(NucleationError::NotFound),
Err(_) => Err(NucleationError::Parse),
}
}
#[cfg(not(feature = "scripting-js"))]
{
let _ = path;
Err(NucleationError::InvalidArgument)
}
}
/// Run a script file, auto-detecting the engine by extension (`.lua` or
/// `.js`). Returns the schematic the script assigns to `result`; `NotFound`
/// if it produced none, `Parse` if it failed (including unsupported
/// extensions).
pub fn run_script(path: &DiplomatStr) -> Result<Box<Schematic>, NucleationError> {
let path = std::str::from_utf8(path).map_err(|_| NucleationError::InvalidArgument)?;
match crate::scripting::run_script(path) {
Ok(Some(ss)) => Ok(Box::new(Schematic(ss.inner))),
Ok(None) => Err(NucleationError::NotFound),
Err(_) => Err(NucleationError::Parse),
}
}
}
}