use semver::Version;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Source {
Release,
Nightly,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Candidate {
pub source: Source,
pub version: String,
pub git_ref: String,
pub asset_url: String,
pub sha256: Option<String>,
pub bin_sha256: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Current {
pub version: String,
pub build_sha: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ancestry {
Ahead,
Behind,
Identical,
Diverged,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Decision {
UpToDate,
Available(Candidate),
}
fn parse(v: &str) -> Option<Version> {
Version::parse(v.trim_start_matches('v')).ok()
}
fn release_is_newer(
cur: &Current,
c: &Candidate,
ancestry: &dyn Fn(&str, &str) -> Option<Ancestry>,
) -> bool {
let (Some(have), Some(want)) = (parse(&cur.version), parse(&c.version)) else {
return false;
};
if want <= have {
return false;
}
match &cur.build_sha {
None => true,
Some(sha) => !matches!(
ancestry(sha, &c.git_ref),
Some(Ancestry::Behind | Ancestry::Identical)
),
}
}
fn nightly_is_newer(
cur: &Current,
c: &Candidate,
ancestry: &dyn Fn(&str, &str) -> Option<Ancestry>,
) -> bool {
match &cur.build_sha {
Some(sha) => ancestry(sha, &c.git_ref) == Some(Ancestry::Ahead),
None => matches!((parse(&cur.version), parse(&c.version)), (Some(h), Some(w)) if w > h),
}
}
#[must_use]
pub fn decide(
cur: &Current,
release: Option<&Candidate>,
nightly: Option<&Candidate>,
ancestry: &dyn Fn(&str, &str) -> Option<Ancestry>,
) -> Decision {
let rel = release.filter(|c| release_is_newer(cur, c, ancestry));
let nig = nightly.filter(|c| nightly_is_newer(cur, c, ancestry));
let pick = match (rel, nig) {
(Some(r), Some(n)) => {
if ancestry(&r.git_ref, &n.git_ref) == Some(Ancestry::Ahead) {
n
} else {
r
}
}
(Some(r), None) => r,
(None, Some(n)) => n,
(None, None) => return Decision::UpToDate,
};
Decision::Available(pick.clone())
}
#[cfg(test)]
mod tests {
use super::*;
fn rel(v: &str) -> Candidate {
Candidate {
source: Source::Release,
version: v.into(),
git_ref: format!("v{v}"),
asset_url: String::new(),
sha256: None,
bin_sha256: None,
}
}
fn nig(v: &str, sha: &str) -> Candidate {
Candidate {
source: Source::Nightly,
version: v.into(),
git_ref: sha.into(),
asset_url: String::new(),
sha256: Some("00".into()),
bin_sha256: Some("11".into()),
}
}
fn cur(v: &str, sha: Option<&str>) -> Current {
Current {
version: v.into(),
build_sha: sha.map(Into::into),
}
}
fn hist(base: &str, head: &str) -> Option<Ancestry> {
const ORDER: [&str; 4] = ["old", "v0.69.1", "mid", "new"];
if base == "offline" || head == "offline" {
return None;
}
if base == head {
return Some(Ancestry::Identical);
}
if base == "rb" || head == "rb" {
return Some(Ancestry::Diverged);
}
let (b, h) = (
ORDER.iter().position(|x| *x == base)?,
ORDER.iter().position(|x| *x == head)?,
);
Some(if h > b {
Ancestry::Ahead
} else {
Ancestry::Behind
})
}
fn offline(_: &str, _: &str) -> Option<Ancestry> {
None
}
fn src(d: &Decision) -> Option<(Source, String)> {
match d {
Decision::UpToDate => None,
Decision::Available(c) => Some((c.source, c.git_ref.clone())),
}
}
#[test]
fn decide_case_table() {
type Row = (
&'static str,
Current,
Option<Candidate>,
Option<Candidate>,
fn(&str, &str) -> Option<Ancestry>,
Option<(Source, &'static str)>,
);
let r = Some((Source::Release, "v0.69.1"));
let n = Some((Source::Nightly, "new"));
let rows: Vec<Row> = vec![
(
"newer release, no sha",
cur("0.69.0", None),
Some(rel("0.69.1")),
None,
hist,
r,
),
(
"equal release",
cur("0.69.1", None),
Some(rel("0.69.1")),
None,
hist,
None,
),
(
"older release is never a downgrade",
cur("0.70.0", None),
Some(rel("0.69.1")),
None,
hist,
None,
),
(
"greater tag BEHIND the running commit is not newer",
cur("0.69.0", Some("mid")),
Some(rel("0.69.1")),
None,
hist,
None,
),
(
"greater tag ahead of the running commit",
cur("0.69.0", Some("old")),
Some(rel("0.69.1")),
None,
hist,
r,
),
(
"greater tag diverged (release branch) is newer",
cur("0.69.0", Some("rb")),
Some(rel("0.69.1")),
None,
hist,
r,
),
(
"nightly ahead of running commit",
cur("0.69.1", Some("mid")),
None,
Some(nig("0.69.1", "new")),
hist,
n,
),
(
"nightly at the running commit",
cur("0.69.1", Some("new")),
None,
Some(nig("0.69.1", "new")),
hist,
None,
),
(
"nightly behind the running commit",
cur("0.69.1", Some("new")),
None,
Some(nig("0.69.1", "mid")),
hist,
None,
),
(
"no sha, same version nightly: unknown never nags",
cur("0.69.1", None),
None,
Some(nig("0.69.1", "new")),
hist,
None,
),
(
"no sha, greater version nightly",
cur("0.69.0", None),
None,
Some(nig("0.70.0", "new")),
hist,
n,
),
(
"both newer, nightly ahead of tag -> nightly",
cur("0.69.0", Some("old")),
Some(rel("0.69.1")),
Some(nig("0.69.1", "new")),
hist,
n,
),
(
"both newer, nightly behind tag -> release",
cur("0.69.0", None),
Some(rel("0.69.1")),
Some(nig("0.70.0", "old")),
hist,
r,
),
(
"offline: release by semver, nightly unprovable",
cur("0.69.0", Some("old")),
Some(rel("0.69.1")),
Some(nig("0.69.1", "new")),
offline,
r,
),
(
"offline with sha, nightly only: no nag",
cur("0.69.1", Some("mid")),
None,
Some(nig("0.69.1", "new")),
offline,
None,
),
(
"unparseable current version: no nag",
cur("dev", None),
Some(rel("0.69.1")),
None,
hist,
None,
),
(
"nothing published",
cur("0.69.0", None),
None,
None,
hist,
None,
),
];
let mut bad = Vec::new();
for (name, c, re, ni, anc, want) in rows {
let got = src(&decide(&c, re.as_ref(), ni.as_ref(), &anc));
let want = want.map(|(s, g)| (s, g.to_string()));
if got != want {
bad.push(format!("{name}: got {got:?}, want {want:?}"));
}
}
assert!(bad.is_empty(), "decide rows failed:\n{}", bad.join("\n"));
}
}