use std::fmt;
use crate::types::hooks::HookRun;
use crate::types::name::PackName;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HookModule(String);
impl HookModule {
#[must_use]
pub fn new(owner: &PackName, run: &HookRun) -> Self {
let pack = run.pack().unwrap_or(owner);
let path = run.path().as_str();
let script = path.strip_suffix(".lua").unwrap_or(path);
let segments = script.split('/').collect::<Vec<_>>().join(".");
Self(format!("{pack}.{segments}"))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for HookModule {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::*;
use crate::types::{HookAction, PackManifest};
type TestResult = Result<(), Box<dyn std::error::Error>>;
fn module_of(pack: &str, run: &str) -> Result<String, Box<dyn std::error::Error>> {
let text = format!(
"[pack]\nname = \"{pack}\"\nversion = \"1.0.0\"\napi = 0\ndescription = \"d\"\n\n\
[[hooks]]\nkind = \"lua\"\nevent = \"session.start\"\nrun = \"{run}\"\n"
);
let manifest = PackManifest::parse(Path::new("packs/p/pack.toml"), &text)
.map_err(|found| format!("{found:?}"))?;
let hook = manifest.hooks().first().ok_or("one hook")?;
let HookAction::Lua(lua) = hook.action() else {
return Err("a lua hook".into());
};
Ok(HookModule::new(&PackName::new(pack)?, lua.run()).to_string())
}
#[test]
fn module_from_another_packs_root() -> TestResult {
assert_eq!(
module_of("team-lint", "{{root core}}/hooks/enforce.lua")?,
"core.hooks.enforce"
);
Ok(())
}
#[test]
fn module_from_own_pack_relative() -> TestResult {
assert_eq!(
module_of("team-lint", "hooks/my-file.lua")?,
"team-lint.hooks.my-file"
);
Ok(())
}
#[test]
fn module_from_own_root_token() -> TestResult {
assert_eq!(
module_of("core", "{{root}}/hooks/a/b.lua")?,
"core.hooks.a.b"
);
Ok(())
}
}