use crate::types::hooks::NativeHooks;
use crate::types::name::ToolName;
use crate::types::tree::TreePath;
#[derive(Debug, Clone)]
pub struct NativeDir {
tool: ToolName,
files: Vec<TreePath>,
hooks: Option<NativeHooks>,
}
impl NativeDir {
#[must_use]
pub(super) const fn new(
tool: ToolName,
files: Vec<TreePath>,
hooks: Option<NativeHooks>,
) -> Self {
Self { tool, files, hooks }
}
#[must_use]
pub const fn tool(&self) -> &ToolName {
&self.tool
}
#[must_use]
pub fn files(&self) -> &[TreePath] {
&self.files
}
#[must_use]
pub const fn hooks(&self) -> Option<&NativeHooks> {
self.hooks.as_ref()
}
#[must_use]
pub fn hook_count(&self) -> usize {
self.hooks.as_ref().map_or(0, |hooks| hooks.hooks().len())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::manifest::Requires;
use std::error::Error;
use std::path::Path;
#[test]
fn native_dir_counts_hook_entries() -> Result<(), Box<dyn Error>> {
let tool = ToolName::new("claude")?;
assert_eq!(
NativeDir::new(tool.clone(), Vec::new(), None).hook_count(),
0
);
let text = "[[hooks]]\nkind = \"message\"\nevent = \"Stop\"\ntext = \"a\"\n\n\
[[hooks]]\nkind = \"message\"\nevent = \"Stop\"\ntext = \"b\"\n";
let hooks = NativeHooks::parse(
Path::new("native/claude/hooks.toml"),
text,
&Requires::default(),
)
.map_err(|d| format!("{:?}", d.into_sorted()))?;
assert_eq!(
NativeDir::new(tool, Vec::new(), Some(hooks)).hook_count(),
2
);
Ok(())
}
}