use airsl::description::{Function, LuaType, ModuleDescription};
use airsl::mlua::{self, Lua, Table, Value};
use airsl::modules::Path;
use airsl::{HostModule, InstallContext, ModuleName};
use crate::snapshot::copy_table;
pub(crate) const CURATED_REGISTRY_KEY: &str = "buildl.curated";
#[derive(Debug)]
pub(crate) struct Recorded<M>(M);
impl<M: HostModule> Recorded<M> {
pub(crate) const fn new(module: M) -> Self {
Self(module)
}
}
impl<M: HostModule> HostModule for Recorded<M> {
fn name(&self) -> &ModuleName {
self.0.name()
}
fn describe(&self) -> airsl::Result<ModuleDescription> {
self.0.describe()
}
fn install(&self, lua: &Lua, table: &Table, context: &InstallContext<'_>) -> airsl::Result<()> {
self.0.install(lua, table, context)?;
let name = self.0.name().as_str();
record(lua, name, table).map_err(|error| airsl::Error::lua(name, error))
}
}
fn record(lua: &Lua, name: &str, table: &Table) -> mlua::Result<()> {
let recorded =
if let Value::Table(recorded) = lua.named_registry_value::<Value>(CURATED_REGISTRY_KEY)? {
recorded
} else {
let recorded = lua.create_table()?;
lua.set_named_registry_value(CURATED_REGISTRY_KEY, recorded.clone())?;
recorded
};
recorded.set(name, copy_table(lua, table)?)
}
#[derive(Debug, Default)]
pub(crate) struct CuratedPath(Path);
impl HostModule for CuratedPath {
fn name(&self) -> &ModuleName {
self.0.name()
}
fn describe(&self) -> airsl::Result<ModuleDescription> {
ModuleDescription::builder("Lexical path manipulation that touches no file.")
.function(
Function::new("join", "Joins path parts with the path separator.")
.variadic(LuaType::STRING, "The parts to join.")
.returns(LuaType::STRING),
)
.function(
Function::new("dirname", "Returns the directory part of a path.")
.param("path", LuaType::STRING, "The path to read.")
.returns(LuaType::STRING),
)
.function(
Function::new("basename", "Returns the last component of a path.")
.param("path", LuaType::STRING, "The path to read.")
.returns(LuaType::STRING),
)
.function(
Function::new(
"stem",
"Returns the last component of a path without its extension.",
)
.param("path", LuaType::STRING, "The path to read.")
.returns(LuaType::STRING),
)
.function(
Function::new(
"ext",
"Returns the extension of a path, or an empty string when it has none.",
)
.param("path", LuaType::STRING, "The path to read.")
.returns(LuaType::STRING),
)
.function(
Function::new(
"normalize",
"Collapses redundant separators and dot components in a path.",
)
.param("path", LuaType::STRING, "The path to read.")
.returns(LuaType::STRING),
)
.function(
Function::new("relative_to", "Expresses a path relative to a base path.")
.param("path", LuaType::STRING, "The path to express.")
.param("base", LuaType::STRING, "The path to measure it from.")
.returns(LuaType::STRING),
)
.function(
Function::new(
"is_absolute",
"Reports whether a path is absolute on this platform.",
)
.param("path", LuaType::STRING, "The path to read.")
.returns(LuaType::BOOLEAN),
)
.build()
}
fn install(&self, lua: &Lua, table: &Table, context: &InstallContext<'_>) -> airsl::Result<()> {
self.0.install(lua, table, context)?;
table
.set("absolute", Value::Nil)
.map_err(|error| airsl::Error::lua("path", error))
}
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use airsl::mlua::{Lua, Table};
use airsl::{Engine, HostModule, InstallContext, ModuleName, ModuleSet, Policy, Script};
use super::{CURATED_REGISTRY_KEY, CuratedPath, Recorded};
struct Reader(ModuleName);
impl HostModule for Reader {
fn name(&self) -> &ModuleName {
&self.0
}
fn describe(&self) -> airsl::Result<airsl::description::ModuleDescription> {
airsl::description::ModuleDescription::builder("Reads back a recorded module.")
.field(
"path",
airsl::description::LuaType::TABLE,
"The recorded path module.",
)
.build()
}
fn install(
&self,
lua: &Lua,
table: &Table,
_context: &InstallContext<'_>,
) -> airsl::Result<()> {
let recorded: Table = lua.named_registry_value(CURATED_REGISTRY_KEY).unwrap();
table
.set("path", recorded.get::<Table>("path").unwrap())
.unwrap();
Ok(())
}
}
#[test]
fn records_a_copy_of_the_module_table_as_it_installed() {
let mut modules = ModuleSet::new();
modules
.insert(Box::new(Recorded::new(CuratedPath::default())))
.unwrap();
modules
.insert(Box::new(Reader(ModuleName::new("reader").unwrap())))
.unwrap();
let engine = Engine::builder()
.policy(Policy::pure())
.stdlib(modules)
.build()
.unwrap();
let result = engine
.eval_to::<String>(
&Script::from_source(
"local copy = airsstack.reader.path\n\
local same = copy == airsstack.path\n\
airsstack.path.stem = nil\n\
return tostring(same) .. ' ' .. type(copy.stem) .. ' ' .. type(copy.absolute)",
"build.lua",
)
.unwrap(),
)
.unwrap();
assert_eq!(result, "false function nil");
}
#[test]
fn the_recorded_descriptions_match_what_install_sets() {
let root = airsl::RootTable::default();
airsl::luals::verify(&CuratedPath::default(), &root).unwrap();
airsl::luals::verify(&Recorded::new(CuratedPath::default()), &root).unwrap();
}
#[test]
fn keeps_path_arithmetic_and_drops_absolute() {
let mut modules = ModuleSet::new();
modules.insert(Box::new(CuratedPath::default())).unwrap();
let engine = Engine::builder()
.policy(Policy::pure())
.stdlib(modules)
.build()
.unwrap();
let result = engine
.eval_to::<String>(
&Script::from_source(
"return type(airsstack.path.absolute) .. ' ' .. airsstack.path.stem('src/a.c')",
"build.lua",
)
.unwrap(),
)
.unwrap();
assert_eq!(result, "nil a");
}
}