use std::fmt;
use crate::types::api::API_MAJOR;
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";
#[must_use]
pub fn release_prefix() -> String {
format!("{TAG_PREFIX}{API_MAJOR}")
}
#[must_use]
pub fn official_major(url: &GitUrl, prefix: &str) -> Option<u64> {
(url.as_str() == OFFICIAL_REPOSITORY && prefix == TAG_PREFIX).then_some(API_MAJOR)
}
pub const OFFICIAL_PACKS: [&str; 5] = [
"core",
"sdlc",
"journal",
"guideline-rust",
"guideline-python",
];
pub const OFFICIAL_TOOLS: [&str; 3] = ["claude", "musecode", "opencode"];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Flag {
Pack,
Tool,
Ext,
}
impl Flag {
#[must_use]
pub const fn noun(self) -> &'static str {
match self {
Self::Pack => "pack",
Self::Tool | Self::Ext => "tool",
}
}
const fn official(self) -> &'static [&'static str] {
match self {
Self::Pack => &OFFICIAL_PACKS,
Self::Tool | Self::Ext => &OFFICIAL_TOOLS,
}
}
const fn directory(self) -> &'static str {
match self {
Self::Pack => "packs",
Self::Tool | Self::Ext => "extensions",
}
}
const fn takes_git_url(self) -> bool {
matches!(self, Self::Ext)
}
}
impl fmt::Display for Flag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Pack | Self::Tool => write!(f, "--{}", self.noun()),
Self::Ext => f.write_str("ext add"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Argument {
Official {
url: GitUrl,
subdir: SubPath,
},
Path(LocalPath),
Git(GitUrl),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum ShorthandError {
#[error("not an official {}; {}", .0.noun(), hint(*.0))]
NotOfficial(Flag),
}
const fn hint(flag: Flag) -> &'static str {
match flag {
Flag::Pack | Flag::Tool => "pass a path, or add a git source to agentstacks.toml",
Flag::Ext => "pass a path or a git URL",
}
}
pub fn classify(flag: Flag, arg: &str) -> Result<Argument, Diagnostic> {
let at = || Location::verbatim(format!("{flag} \"{arg}\""));
if flag.takes_git_url() && (arg.contains("://") || arg.starts_with("git@")) {
return GitUrl::new(arg)
.map(Argument::Git)
.map_err(|error| Diagnostic::new(at(), SourceError::from(error)));
}
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 official_tool_musecode_expands_to_extensions_subdir() -> TestResult {
let Argument::Official { url, subdir } = accepted(Flag::Tool, "musecode")? else {
return Err("expected an official name".into());
};
assert_eq!(url.as_str(), OFFICIAL_REPOSITORY);
assert_eq!(subdir.as_str(), "extensions/musecode");
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(())
}
#[test]
fn ext_classifies_url_path_and_official() -> TestResult {
for text in ["https://example.com/x.git", "git@host:x.git"] {
assert_eq!(
accepted(Flag::Ext, text)?,
Argument::Git(GitUrl::new(text)?)
);
}
for text in ["./ext/x", "ext/x"] {
assert_eq!(
accepted(Flag::Ext, text)?,
Argument::Path(LocalPath::new(text)?)
);
}
let Argument::Official { url, subdir } = accepted(Flag::Ext, "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 ext_not_official_text() -> TestResult {
assert_eq!(
refusal(Flag::Ext, "x")?,
"ext add \"x\": not an official tool; pass a path or a git URL"
);
Ok(())
}
#[test]
fn dash_name_is_not_official() -> TestResult {
assert_eq!(
refusal(Flag::Ext, "-x")?,
"ext add \"-x\": not an official tool; pass a path or a git URL"
);
Ok(())
}
#[test]
fn ext_dash_url_refused_verbatim() -> TestResult {
assert_eq!(
refusal(Flag::Ext, "--upload-pack=x://y")?,
"ext add \"--upload-pack=x://y\": URL must not start with \"-\""
);
Ok(())
}
#[test]
fn init_still_takes_no_url() -> TestResult {
for flag in [Flag::Pack, Flag::Tool] {
let text = "https://example.com/x.git";
assert_eq!(
accepted(flag, text)?,
Argument::Path(LocalPath::new(text)?),
"{flag}"
);
assert_eq!(
refusal(flag, "git@host:x.git")?,
format!(
"{flag} \"git@host:x.git\": not an official {}; pass a path, or add a git source to agentstacks.toml",
flag.noun()
)
);
}
Ok(())
}
}