use std::fmt;
use crate::types::name::PackName;
use crate::types::name::ToolName;
use crate::types::report::Location;
const PROJECT_FILE: &str = "agentstacks.toml";
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SourceKey {
Pack(PackName),
Tool(ToolName),
}
impl SourceKey {
#[must_use]
pub fn location(&self) -> Location {
Location::at_key(PROJECT_FILE, self.to_string())
}
#[must_use]
pub fn subdir_location(&self) -> Location {
Location::at_key(PROJECT_FILE, format!("{self}.subdir"))
}
}
impl fmt::Display for SourceKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Pack(name) => write!(f, "packs.{name}"),
Self::Tool(name) => write!(f, "tools.{name}"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
type TestResult = Result<(), Box<dyn std::error::Error>>;
#[test]
fn source_key_displays_its_key_path() -> TestResult {
assert_eq!(
SourceKey::Pack(PackName::new("core")?).to_string(),
"packs.core"
);
assert_eq!(
SourceKey::Tool(ToolName::new("claude")?).to_string(),
"tools.claude"
);
Ok(())
}
#[test]
fn source_key_locations() -> TestResult {
let key = SourceKey::Pack(PackName::new("core")?);
assert_eq!(key.location().to_string(), "agentstacks.toml: packs.core");
assert_eq!(
key.subdir_location().to_string(),
"agentstacks.toml: packs.core.subdir"
);
Ok(())
}
#[test]
fn source_key_orders_packs_before_tools() -> TestResult {
let tool = SourceKey::Tool(ToolName::new("a")?);
let late_pack = SourceKey::Pack(PackName::new("z")?);
let early_pack = SourceKey::Pack(PackName::new("b")?);
assert!(late_pack < tool);
assert!(early_pack < late_pack);
Ok(())
}
}