use std::collections::BTreeMap;
use std::path::Path;
use toml::{Table, Value};
use crate::types::api::API;
use crate::types::format::TomlFields;
use crate::types::format::parse_toml_document;
use crate::types::name::InvalidName;
use crate::types::name::PackName;
use crate::types::name::ToolName;
use crate::types::project::PackEntry;
use crate::types::project::PinRule;
use crate::types::project::Source;
use crate::types::project::SourceFields;
use crate::types::project::error::ProjectError;
use crate::types::report::Diagnostics;
use crate::types::report::FieldError;
use crate::types::report::TypeWord;
use crate::types::report::UnsupportedApi;
const TOP_LEVEL_KEYS: [&str; 3] = ["api", "tools", "packs"];
const TOOL_KEYS: [&str; 5] = ["git", "path", "tag", "rev", "subdir"];
const PACK_KEYS: [&str; 6] = ["git", "path", "tag", "rev", "subdir", "options"];
#[derive(Debug, Clone, PartialEq)]
pub struct ProjectFile {
tools: BTreeMap<ToolName, Source>,
packs: BTreeMap<PackName, PackEntry>,
}
impl ProjectFile {
pub fn parse(file: &Path, text: &str) -> Result<Self, Diagnostics> {
let table = match parse_toml_document(file, text) {
Ok(table) => table,
Err(syntax) => {
let mut diagnostics = Diagnostics::new();
diagnostics.push(syntax);
return Err(diagnostics);
}
};
let mut root = TomlFields::new(file, "", table);
root.refuse_unknown_keys(&TOP_LEVEL_KEYS);
if let Some(api) = root.required_integer("api").filter(|api| *api != API) {
root.refuse_at_file(ProjectError::from(UnsupportedApi::new(api)));
}
let tools = read_section(&mut root, "tools", ToolName::new, read_tool);
let packs = read_section(&mut root, "packs", PackName::new, read_pack);
if tools.supplies_nothing() || packs.supplies_nothing() {
root.refuse_at_file(ProjectError::NothingToInstall);
}
let diagnostics = root.into_diagnostics();
if diagnostics.is_empty() {
Ok(Self {
tools: tools.entries,
packs: packs.entries,
})
} else {
Err(diagnostics)
}
}
#[must_use]
pub const fn tools(&self) -> &BTreeMap<ToolName, Source> {
&self.tools
}
#[must_use]
pub const fn packs(&self) -> &BTreeMap<PackName, PackEntry> {
&self.packs
}
}
struct Section<N, E> {
entries: BTreeMap<N, E>,
listed: bool,
mistyped: bool,
}
impl<N, E> Section<N, E> {
const fn supplies_nothing(&self) -> bool {
!self.listed && !self.mistyped
}
}
fn read_section<N: Ord, E>(
root: &mut TomlFields,
key: &str,
parse_name: fn(&str) -> Result<N, InvalidName>,
read_entry: fn(&mut TomlFields, &str) -> Option<E>,
) -> Section<N, E> {
let mut entries = BTreeMap::new();
let Some(mut section) = root.optional_table(key) else {
return Section {
entries,
listed: false,
mistyped: root.raw(key).is_some(),
};
};
let names = section.entries();
for (name, _) in &names {
let parsed = parse_name(name);
if parsed.is_err() {
section.refuse(name, ProjectError::InvalidName);
}
let path = section.key_path(name);
let Some(mut entry) = section.optional_table(name) else {
continue;
};
let read = read_entry(&mut entry, &path);
section.absorb(entry);
if let (Ok(name), Some(read)) = (parsed, read) {
entries.insert(name, read);
}
}
root.absorb(section);
Section {
entries,
listed: !names.is_empty(),
mistyped: false,
}
}
fn read_tool(entry: &mut TomlFields, path: &str) -> Option<Source> {
let source = SourceFields::read_into(entry, path, PinRule::ExactlyOneOfTagRev);
entry.refuse_unknown_keys(&TOOL_KEYS);
source.map(Source::from_fields)
}
fn read_pack(entry: &mut TomlFields, path: &str) -> Option<PackEntry> {
let source = SourceFields::read_into(entry, path, PinRule::ExactlyOneOfTagRev);
let options = match entry.raw("options").cloned() {
Some(Value::Table(options)) => Some(options),
Some(other) => {
let got = TomlFields::type_word(&other);
entry.refuse(
"options",
FieldError::WrongType {
expected: TypeWord::Table,
got,
},
);
None
}
None => Some(Table::new()),
};
entry.refuse_unknown_keys(&PACK_KEYS);
Some(PackEntry::new(Source::from_fields(source?), options?))
}
#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use super::*;
use crate::types::project::GitTag;
use crate::types::project::GitUrl;
use crate::types::project::LocalPath;
use crate::types::project::Pin;
use crate::types::project::SubPath;
type TestResult = Result<(), Box<dyn std::error::Error>>;
const EXAMPLE: &str = "api = 0\n\
\n\
[tools.claude]\n\
git = \"https://github.com/airsstack/agentstacks\"\n\
tag = \"packs-v0.1.0\"\n\
subdir = \"extensions/claude\"\n\
\n\
[packs.core]\n\
git = \"https://github.com/airsstack/agentstacks\"\n\
tag = \"packs-v0.1.0\"\n\
subdir = \"packs/core\"\n\
options = { archive_keep = 50 }\n\
\n\
[packs.team-lint]\n\
path = \"packs/team-lint\"\n";
const BASE: &str = "api = 0\n\
[tools.claude]\n\
git = \"https://example.org/r\"\n\
tag = \"v1\"\n\
[packs.core]\n\
git = \"https://example.org/r\"\n\
tag = \"v1\"\n";
fn parse(text: &str) -> Result<ProjectFile, Vec<String>> {
ProjectFile::parse(Path::new("agentstacks.toml"), text).map_err(|diagnostics| {
diagnostics
.into_sorted()
.iter()
.map(ToString::to_string)
.collect()
})
}
fn refusals(text: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
match parse(text) {
Ok(project) => Err(format!("expected a refusal, parsed {project:?}").into()),
Err(shown) => Ok(shown),
}
}
fn refused_once(text: &str, message: &str) -> TestResult {
assert_eq!(refusals(text)?, [message], "{text}");
Ok(())
}
#[test]
fn project_file_parses_the_documented_example() -> TestResult {
let project = parse(EXAMPLE).map_err(|shown| shown.join("; "))?;
let url = GitUrl::new("https://github.com/airsstack/agentstacks")?;
let tag = Pin::Tag(GitTag::new("packs-v0.1.0")?);
assert_eq!(
project.tools().get(&ToolName::new("claude")?),
Some(&Source::Git {
url: url.clone(),
pin: tag.clone(),
subdir: Some(SubPath::new("extensions/claude")?),
})
);
let core = project
.packs()
.get(&PackName::new("core")?)
.ok_or("pack core")?;
assert_eq!(
core.source(),
&Source::Git {
url,
pin: tag,
subdir: Some(SubPath::new("packs/core")?),
}
);
assert_eq!(
core.options().get("archive_keep"),
Some(&Value::Integer(50))
);
let team_lint = project
.packs()
.get(&PackName::new("team-lint")?)
.ok_or("pack team-lint")?;
assert_eq!(
team_lint.source(),
&Source::Path(LocalPath::new("packs/team-lint")?)
);
assert!(team_lint.options().is_empty());
assert_eq!(project.tools().len(), 1);
assert_eq!(project.packs().len(), 2);
Ok(())
}
#[test]
fn project_file_refuses_a_syntax_error_with_its_line() -> TestResult {
refused_once(
"api = 0\n\nx = [1,",
"agentstacks.toml:3: unclosed array, expected `]`",
)
}
#[test]
fn project_file_refuses_api_rules() -> TestResult {
let rest = BASE.trim_start_matches("api = 0\n");
let cases = [
(
format!("api = 1\n{rest}"),
"agentstacks.toml: api 1 not supported (this agentstacks speaks 0)",
),
(rest.to_owned(), "agentstacks.toml: api: required"),
(
format!("api = \"0\"\n{rest}"),
"agentstacks.toml: api: expected integer, got string",
),
(
format!("api = 1979-05-27\n{rest}"),
"agentstacks.toml: api: expected integer, got datetime",
),
];
for (text, message) in cases {
refused_once(&text, message)?;
}
Ok(())
}
#[test]
fn project_file_refuses_nothing_to_install() -> TestResult {
let tool = "[tools.claude]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n";
let pack = "[packs.core]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n";
let cases = [
format!("api = 0\n{tool}"),
format!("api = 0\n{pack}"),
"api = 0\n[tools]\n[packs]\n".to_owned(),
"api = 0\n".to_owned(),
];
for text in cases {
refused_once(&text, "agentstacks.toml: nothing to install")?;
}
let shown: BTreeSet<String> = refusals("")?.into_iter().collect();
let expected: BTreeSet<String> = [
"agentstacks.toml: api: required",
"agentstacks.toml: nothing to install",
]
.map(str::to_owned)
.into();
assert_eq!(shown, expected);
Ok(())
}
#[test]
fn project_file_gives_only_the_wrong_type_for_a_mistyped_table_beside_a_listed_one()
-> TestResult {
refused_once(
"api = 0\ntools = 1\n[packs.core]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n",
"agentstacks.toml: tools: expected table, got integer",
)?;
refused_once(
"api = 0\npacks = \"x\"\n[tools.claude]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n",
"agentstacks.toml: packs: expected table, got string",
)
}
#[test]
fn project_file_reports_nothing_to_install_beside_a_mistyped_table() -> TestResult {
let expected = |wrong_type: &str| -> BTreeSet<String> {
[wrong_type, "agentstacks.toml: nothing to install"]
.map(str::to_owned)
.into()
};
let packs_only: BTreeSet<String> =
refusals("api = 0\npacks = \"x\"\n")?.into_iter().collect();
assert_eq!(
packs_only,
expected("agentstacks.toml: packs: expected table, got string")
);
let tools_only: BTreeSet<String> = refusals("api = 0\ntools = 1\n")?.into_iter().collect();
assert_eq!(
tools_only,
expected("agentstacks.toml: tools: expected table, got integer")
);
let both_empty: BTreeSet<String> = refusals("api = 0\ntools = 1\n[packs]\n")?
.into_iter()
.collect();
assert_eq!(
both_empty,
expected("agentstacks.toml: tools: expected table, got integer")
);
let both_mistyped: BTreeSet<String> = refusals("api = 0\ntools = 1\npacks = \"x\"\n")?
.into_iter()
.collect();
let both_wrong: BTreeSet<String> = [
"agentstacks.toml: tools: expected table, got integer",
"agentstacks.toml: packs: expected table, got string",
]
.map(str::to_owned)
.into();
assert_eq!(both_mistyped, both_wrong);
Ok(())
}
#[test]
fn project_file_accepts_the_project_root_as_a_pack_path() -> TestResult {
let text = BASE.replace(
"[packs.core]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n",
"[packs.root]\npath = \".\"\n",
);
let project = parse(&text).map_err(|shown| shown.join("; "))?;
let root = project
.packs()
.get(&PackName::new("root")?)
.ok_or("pack root")?;
assert_eq!(root.source(), &Source::Path(LocalPath::new(".")?));
Ok(())
}
#[test]
fn project_file_refuses_invalid_table_keys() -> TestResult {
let pack = "git = \"https://example.org/r\"\ntag = \"v1\"\n";
let tool = "[tools.claude]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n";
refused_once(
&format!("api = 0\n{tool}[packs.Core]\n{pack}"),
"agentstacks.toml: packs.Core: not a valid name",
)?;
refused_once(
&format!("api = 0\n[tools.Claude]\n{pack}[packs.core]\n{pack}"),
"agentstacks.toml: tools.Claude: not a valid name",
)
}
#[test]
fn project_file_refuses_unknown_keys() -> TestResult {
refused_once(
&format!("{BASE}branch = \"main\"\n"),
"agentstacks.toml: packs.core.branch: unknown key",
)?;
refused_once(
&BASE.replace(
"tag = \"v1\"\n[packs",
"tag = \"v1\"\noptions = { x = 1 }\n[packs",
),
"agentstacks.toml: tools.claude.options: unknown key",
)?;
refused_once(
&format!("foo = 1\n{BASE}"),
"agentstacks.toml: foo: unknown key",
)
}
#[test]
fn project_file_refuses_wrong_typed_tables() -> TestResult {
refused_once(
"api = 0\n[packs.core]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n\
[tools]\nclaude = \"x\"\n",
"agentstacks.toml: tools.claude: expected table, got string",
)?;
refused_once(
&format!("{BASE}options = 5\n"),
"agentstacks.toml: packs.core.options: expected table, got integer",
)
}
#[test]
fn project_file_applies_source_rules_to_every_entry() -> TestResult {
let tool = "[tools.claude]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n";
let pack = "[packs.core]\ngit = \"https://example.org/r\"\ntag = \"v1\"\n";
let hex = "0123456789abcdef0123456789abcdef01234567";
let cases = [
(
format!("api = 0\n{tool}{pack}path = \"packs/x\"\n"),
"agentstacks.toml: packs.core: needs exactly one of git, path",
),
(
format!("api = 0\n{tool}{pack}rev = \"{hex}\"\n"),
"agentstacks.toml: packs.core: needs exactly one of tag, rev",
),
(
format!(
"api = 0\n{tool}[packs.core]\ngit = \"https://example.org/r\"\nrev = \"main\"\n"
),
"agentstacks.toml: packs.core.rev: not a commit id",
),
(
format!("api = 0\n{tool}[packs.team-lint]\npath = \"packs/x\"\ntag = \"v1\"\n"),
"agentstacks.toml: packs.team-lint: tag, rev and subdir apply only to git sources",
),
(
format!("api = 0\n{tool}[packs.team-lint]\npath = \"/abs\"\n"),
"agentstacks.toml: packs.team-lint.path: absolute paths are not portable",
),
(
format!("api = 0\n[tools.claude]\ngit = \"-x\"\ntag = \"v1\"\n{pack}"),
"agentstacks.toml: tools.claude.git: URL must not start with \"-\"",
),
(
format!("api = 0\n{tool}subdir = \"/x\"\n{pack}"),
"agentstacks.toml: tools.claude.subdir: must stay inside the repository",
),
];
for (text, message) in cases {
refused_once(&text, message)?;
}
Ok(())
}
#[test]
fn project_file_reports_every_error() -> TestResult {
let text = "api = 1\n\
[tools.claude]\n\
git = \"https://example.org/r\"\n\
tag = \"v1\"\n\
options = { x = 1 }\n\
[packs.Core]\n\
git = \"https://example.org/r\"\n\
tag = \"v1\"\n\
[packs.core]\n\
git = \"https://example.org/r\"\n\
tag = \"v1\"\n\
branch = \"main\"\n";
let shown: BTreeSet<String> = refusals(text)?.into_iter().collect();
let expected: BTreeSet<String> = [
"agentstacks.toml: api 1 not supported (this agentstacks speaks 0)",
"agentstacks.toml: packs.Core: not a valid name",
"agentstacks.toml: packs.core.branch: unknown key",
"agentstacks.toml: tools.claude.options: unknown key",
]
.map(str::to_owned)
.into();
assert_eq!(shown, expected);
Ok(())
}
#[test]
fn project_file_keeps_option_values_raw() -> TestResult {
let text = format!("{BASE}options = {{ archive_keep = \"many\", ratio = 1.5 }}\n");
let project = parse(&text).map_err(|shown| shown.join("; "))?;
let core = project
.packs()
.get(&PackName::new("core")?)
.ok_or("pack core")?;
assert_eq!(
core.options().get("archive_keep"),
Some(&Value::String("many".to_owned()))
);
assert!(matches!(core.options().get("ratio"), Some(Value::Float(_))));
Ok(())
}
}