use crate::types::format::TomlFields;
use crate::types::report::FormatError;
use crate::types::report::UnsupportedApi;
pub const API: i64 = 1;
pub const API_MAJOR: u64 = {
assert!(API >= 0, "API is a version number and cannot be negative");
API.unsigned_abs()
};
const fn component(text: &str) -> u64 {
match u64::from_str_radix(text, 10) {
Ok(number) => number,
Err(_) => 0,
}
}
pub const VERSION: semver::Version = semver::Version::new(
component(env!("CARGO_PKG_VERSION_MAJOR")),
component(env!("CARGO_PKG_VERSION_MINOR")),
component(env!("CARGO_PKG_VERSION_PATCH")),
);
pub const LEGACY_API: i64 = 0;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ApiCheck {
Current,
Legacy,
Unsupported(i64),
}
#[must_use]
pub const fn check(found: i64) -> ApiCheck {
if found == API {
ApiCheck::Current
} else if found == LEGACY_API {
ApiCheck::Legacy
} else {
ApiCheck::Unsupported(found)
}
}
pub(in crate::types) fn read<E: Into<FormatError>>(
root: &mut TomlFields,
wrap: impl FnOnce(UnsupportedApi) -> E,
) -> Option<ApiCheck> {
match check(root.required_integer("api")?) {
ApiCheck::Unsupported(found) => {
root.refuse_at_file(wrap(UnsupportedApi::new(found)));
None
}
read => Some(read),
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::*;
use crate::types::format::parse_toml_document;
use crate::types::lock::LockError;
#[test]
fn version_is_the_package_version() {
assert_eq!(VERSION.to_string(), env!("CARGO_PKG_VERSION"));
}
#[test]
fn component_reads_a_number_and_falls_back_to_zero() {
assert_eq!(component("12"), 12);
assert_eq!(component("0"), 0);
assert_eq!(component("1-rc"), 0);
assert_eq!(component(""), 0);
}
#[test]
fn api_is_one() {
assert_eq!(API, 1);
}
#[test]
fn api_major_is_the_api() {
assert_eq!(API_MAJOR, 1);
}
#[test]
fn current_is_the_api_this_agentstacks_speaks() {
assert_eq!(check(1), ApiCheck::Current);
}
#[test]
fn zero_is_legacy() {
assert_eq!(check(LEGACY_API), ApiCheck::Legacy);
assert_eq!(LEGACY_API, 0);
}
#[test]
fn two_and_minus_one_are_unsupported() {
assert_eq!(check(2), ApiCheck::Unsupported(2));
assert_eq!(check(-1), ApiCheck::Unsupported(-1));
}
fn read_text(
text: &str,
) -> Result<(Option<ApiCheck>, Vec<String>), Box<dyn std::error::Error>> {
let file = Path::new("agentstacks.lock");
let table = parse_toml_document(file, text).map_err(|found| found.to_string())?;
let mut root = TomlFields::new(file, "", table);
let read = read(&mut root, LockError::from);
let shown = root
.into_diagnostics()
.into_sorted()
.iter()
.map(ToString::to_string)
.collect();
Ok((read, shown))
}
#[test]
fn read_answers_current_and_legacy_without_a_refusal() -> Result<(), Box<dyn std::error::Error>>
{
assert_eq!(read_text("api = 1\n")?, (Some(ApiCheck::Current), vec![]));
assert_eq!(read_text("api = 0\n")?, (Some(ApiCheck::Legacy), vec![]));
Ok(())
}
#[test]
fn read_refuses_an_unsupported_api_in_the_callers_error()
-> Result<(), Box<dyn std::error::Error>> {
assert_eq!(
read_text("api = 2\n")?,
(
None,
vec![
"agentstacks.lock: api 2 not supported (this agentstacks speaks 1)".to_owned()
]
)
);
Ok(())
}
#[test]
fn read_answers_none_for_a_missing_or_mistyped_api() -> Result<(), Box<dyn std::error::Error>> {
assert_eq!(
read_text("")?,
(None, vec!["agentstacks.lock: api: required".to_owned()])
);
assert_eq!(
read_text("api = \"1\"\n")?,
(
None,
vec!["agentstacks.lock: api: expected integer, got string".to_owned()]
)
);
Ok(())
}
}