use std::{collections::BTreeMap, fmt, ops::Deref, sync::OnceLock};
use anyhow::{bail, Context, Result};
use serde::Deserialize;
use crate::model::ToolName;
pub const SOURCE: &str = include_str!("../builtins/pins.toml");
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Probe {
pub asset: String,
pub sums_asset: String,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct BuiltinPin {
pub repo: String,
pub tag: String,
pub version: String,
#[serde(default)]
pub revision: Option<String>,
#[serde(default)]
pub asset: Option<String>,
#[serde(default)]
pub sums_asset: Option<String>,
#[serde(default)]
pub sha256: BTreeMap<String, String>,
#[serde(default)]
pub program: Option<String>,
#[serde(default)]
pub probe: Option<Probe>,
}
impl BuiltinPin {
#[must_use]
pub fn tag_name(&self) -> String {
self.tag.replace("{version}", &self.version)
}
#[must_use]
pub fn fill(&self, template: &str, target: &str) -> String {
template
.replace("{tag}", &self.tag_name())
.replace("{version}", &self.version)
.replace("{target}", target)
}
fn validate(&self, name: &str) -> Result<()> {
let safe = |s: &str| {
!s.is_empty()
&& s.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '{' | '}'))
};
let hex = |s: &str, len: usize| s.len() == len && s.bytes().all(|b| b.is_ascii_hexdigit());
let (owner, repo) = self
.repo
.split_once('/')
.with_context(|| format!("{name}: repo must be owner/name"))?;
if !safe(owner) || !safe(repo) || repo.contains('/') {
bail!("{name}: unsafe repo {:?}", self.repo);
}
if !self.tag.contains("{version}") || !safe(&self.tag) {
bail!(
"{name}: tag template {:?} must contain {{version}}",
self.tag
);
}
if !self.version.starts_with(|c: char| c.is_ascii_digit()) {
bail!("{name}: version {:?} must start with a digit", self.version);
}
crate::release_update::stable_version(&self.version)
.with_context(|| format!("{name}: version {:?}", self.version))?;
if let Some(revision) = &self.revision {
if !hex(revision, 40) {
bail!("{name}: revision must be a 40-character commit id");
}
}
if self.asset.is_some() != self.sums_asset.is_some()
|| self.asset.is_some() == self.sha256.is_empty()
{
bail!("{name}: asset, sums_asset and [sha256] go together");
}
for (target, digest) in &self.sha256 {
if !safe(target) || !hex(digest, 64) {
bail!("{name}: bad sha256 entry {target}");
}
}
if self.sha256.contains_key("any") && self.sha256.len() != 1 {
bail!("{name}: `any` must be the only [sha256] key");
}
if self.probe.is_some() && self.program.is_none() {
bail!("{name}: probe needs a program");
}
if self.program.is_some() && self.probe.is_none() && self.sha256.contains_key("any") {
bail!("{name}: a program needs a per-target asset or a probe");
}
Ok(())
}
}
pub fn parse(text: &str) -> Result<BTreeMap<String, BuiltinPin>> {
let pins: BTreeMap<String, BuiltinPin> =
toml::from_str(text).context("parse builtins/pins.toml")?;
let mut names: Vec<_> = ToolName::BUILTINS.iter().map(|t| t.as_str()).collect();
names.sort_unstable();
if pins.keys().map(String::as_str).collect::<Vec<_>>() != names {
bail!("builtins/pins.toml must pin exactly {}", names.join(", "));
}
for (name, pin) in &pins {
pin.validate(name)?;
}
Ok(pins)
}
fn all() -> &'static BTreeMap<String, BuiltinPin> {
static PINS: OnceLock<BTreeMap<String, BuiltinPin>> = OnceLock::new();
PINS.get_or_init(|| parse(SOURCE).expect("embedded builtins/pins.toml is valid"))
}
#[must_use]
pub fn pin(tool: &str) -> &'static BuiltinPin {
all()
.get(tool)
.unwrap_or_else(|| panic!("{tool} is not a built-in"))
}
#[derive(Clone, Copy)]
pub struct Pin(pub fn() -> &'static str);
impl Deref for Pin {
type Target = str;
fn deref(&self) -> &str {
(self.0)()
}
}
impl fmt::Display for Pin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self)
}
}
impl PartialEq<Pin> for str {
fn eq(&self, other: &Pin) -> bool {
self == &**other
}
}
impl PartialEq<Pin> for &str {
fn eq(&self, other: &Pin) -> bool {
*self == &**other
}
}
impl PartialEq<Pin> for String {
fn eq(&self, other: &Pin) -> bool {
self.as_str() == &**other
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_shipped_pins_parse_and_validate() {
let pins = parse(SOURCE).unwrap();
assert_eq!(pins.len(), ToolName::BUILTINS.len());
}
#[test]
fn a_missing_extra_or_malformed_pin_is_refused() {
let missing = SOURCE.replace("[bobbin]", "[not-bobbin]");
assert!(parse(&missing).is_err());
let dp = pin("desire-path");
let revision = dp.revision.as_deref().unwrap();
let short = SOURCE.replace(revision, &revision[..8]);
assert!(parse(&short).is_err());
let prefixed = SOURCE.replace(
&format!("version = \"{}\"", dp.version),
&format!("version = \"v{}\"", dp.version),
);
assert!(parse(&prefixed).is_err());
let digest_value = &pin("camayoc").sha256["any"];
let digest = SOURCE.replace(digest_value.as_str(), &digest_value[..8]);
assert!(parse(&digest).is_err());
let unknown = SOURCE.replace("[quipu]\n", "[quipu]\nsurprise = 1\n");
assert!(parse(&unknown).is_err());
}
#[test]
fn templates_fill_from_the_pin() {
let quipu = pin("quipu");
assert_eq!(quipu.tag_name(), format!("quipu-ai-v{}", quipu.version));
let probe = quipu.probe.as_ref().unwrap();
assert_eq!(
quipu.fill(&probe.asset, "aarch64-apple-darwin"),
format!(
"quipu-quipu-ai-v{}-aarch64-apple-darwin.tar.gz",
quipu.version
)
);
}
#[test]
fn a_pin_reads_like_the_constant_it_replaced() {
const V: Pin = Pin(|| "1.2.3");
assert_eq!(format!("x {V}"), "x 1.2.3");
assert!("1.2.3" == V);
let owned = format!("{}.{}.{}", 1, 2, 3);
assert!(owned == V);
assert_eq!(&V[..1], "1");
}
}