use std::fmt;
use crate::types::project::GitUrl;
use crate::types::project::LocalPath;
use crate::types::project::SourceError;
use crate::types::project::SubPath;
use crate::types::report::Diagnostic;
use crate::types::report::Location;
pub const OFFICIAL_REPOSITORY: &str = "https://github.com/airsstack/agentstacks";
pub const TAG_PREFIX: &str = "packs-v";
pub const OFFICIAL_PACKS: [&str; 5] = [
"core",
"sdlc",
"journal",
"guideline-rust",
"guideline-python",
];
pub const OFFICIAL_TOOLS: [&str; 2] = ["claude", "opencode"];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Flag {
Pack,
Tool,
}
impl Flag {
#[must_use]
pub const fn noun(self) -> &'static str {
match self {
Self::Pack => "pack",
Self::Tool => "tool",
}
}
const fn official(self) -> &'static [&'static str] {
match self {
Self::Pack => &OFFICIAL_PACKS,
Self::Tool => &OFFICIAL_TOOLS,
}
}
const fn directory(self) -> &'static str {
match self {
Self::Pack => "packs",
Self::Tool => "extensions",
}
}
}
impl fmt::Display for Flag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "--{}", self.noun())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Argument {
Official {
url: GitUrl,
subdir: SubPath,
},
Path(LocalPath),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum ShorthandError {
#[error("not an official {}; pass a path, or add a git source to agentstacks.toml", .0.noun())]
NotOfficial(Flag),
}
pub fn classify(flag: Flag, arg: &str) -> Result<Argument, Diagnostic> {
let at = || Location::file(format!("{flag} \"{arg}\""));
if arg.contains('/') || arg.starts_with('.') {
return LocalPath::new(arg)
.map(Argument::Path)
.map_err(|error| Diagnostic::new(at(), SourceError::from(error)));
}
if !flag.official().contains(&arg) {
return Err(Diagnostic::new(at(), ShorthandError::NotOfficial(flag)));
}
let url = GitUrl::new(OFFICIAL_REPOSITORY)
.map_err(|e| Diagnostic::new(at(), SourceError::from(e)))?;
let subdir = SubPath::new(&format!("{}/{arg}", flag.directory()))
.map_err(|e| Diagnostic::new(at(), SourceError::from(e)))?;
Ok(Argument::Official { url, subdir })
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::FormatError;
type TestResult = Result<(), Box<dyn std::error::Error>>;
fn accepted(flag: Flag, arg: &str) -> Result<Argument, String> {
classify(flag, arg).map_err(|refused| refused.to_string())
}
fn refusal(flag: Flag, arg: &str) -> Result<String, Box<dyn std::error::Error>> {
Ok(classify(flag, arg)
.err()
.ok_or("expected a refusal")?
.to_string())
}
#[test]
fn official_pack_expands_to_the_repository_and_packs_subdir() -> TestResult {
let Argument::Official { url, subdir } = accepted(Flag::Pack, "core")? else {
return Err("expected an official name".into());
};
assert_eq!(url.as_str(), "https://github.com/airsstack/agentstacks");
assert_eq!(subdir.as_str(), "packs/core");
Ok(())
}
#[test]
fn official_tool_expands_to_extensions_subdir() -> TestResult {
let Argument::Official { url, subdir } = accepted(Flag::Tool, "claude")? else {
return Err("expected an official name".into());
};
assert_eq!(url.as_str(), OFFICIAL_REPOSITORY);
assert_eq!(subdir.as_str(), "extensions/claude");
Ok(())
}
#[test]
fn official_tool_opencode_expands_to_extensions_subdir() -> TestResult {
let Argument::Official { url, subdir } = accepted(Flag::Tool, "opencode")? else {
return Err("expected an official name".into());
};
assert_eq!(url.as_str(), OFFICIAL_REPOSITORY);
assert_eq!(subdir.as_str(), "extensions/opencode");
Ok(())
}
#[test]
fn every_official_pack_name_is_accepted() -> TestResult {
for name in OFFICIAL_PACKS {
let Argument::Official { subdir, .. } = accepted(Flag::Pack, name)? else {
return Err(format!("{name} is not official").into());
};
assert_eq!(subdir.as_str(), format!("packs/{name}"));
}
assert_eq!(
OFFICIAL_PACKS,
[
"core",
"sdlc",
"journal",
"guideline-rust",
"guideline-python"
]
);
Ok(())
}
#[test]
fn cmux_is_not_an_official_pack() -> TestResult {
assert_eq!(
refusal(Flag::Pack, "cmux")?,
"--pack \"cmux\": not an official pack; pass a path, or add a git source to agentstacks.toml"
);
Ok(())
}
#[test]
fn slash_or_leading_dot_makes_a_path() -> TestResult {
for text in ["./packs/core", "packs/x", ".hidden"] {
assert_eq!(
accepted(Flag::Pack, text)?,
Argument::Path(LocalPath::new(text)?)
);
}
Ok(())
}
#[test]
fn bare_name_is_never_a_path() -> TestResult {
assert!(matches!(
accepted(Flag::Pack, "core")?,
Argument::Official { .. }
));
Ok(())
}
#[test]
fn unknown_pack_is_refused_with_the_flag_location() -> TestResult {
assert_eq!(
refusal(Flag::Pack, "x")?,
"--pack \"x\": not an official pack; pass a path, or add a git source to agentstacks.toml"
);
Ok(())
}
#[test]
fn unknown_name_is_carried_as_the_shorthand_variant() -> TestResult {
let refused = classify(Flag::Tool, "x")
.err()
.ok_or("expected a refusal")?;
assert!(matches!(
refused.error(),
FormatError::Shorthand(ShorthandError::NotOfficial(Flag::Tool))
));
Ok(())
}
#[test]
fn unknown_tool_is_refused_with_the_tool_wording() -> TestResult {
assert_eq!(
refusal(Flag::Tool, "x")?,
"--tool \"x\": not an official tool; pass a path, or add a git source to agentstacks.toml"
);
Ok(())
}
#[test]
fn absolute_path_is_refused() -> TestResult {
assert_eq!(
refusal(Flag::Pack, "/x")?,
"--pack \"/x\": absolute paths are not portable"
);
Ok(())
}
#[test]
fn climbing_path_is_refused() -> TestResult {
assert_eq!(
refusal(Flag::Pack, "../x")?,
"--pack \"../x\": must stay inside the project"
);
Ok(())
}
}