1use semver::Version;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
10#[serde(rename_all = "lowercase")]
11pub enum Source {
12 Release,
13 Nightly,
14}
15
16#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
18pub struct Candidate {
19 pub source: Source,
20 pub version: String,
22 pub git_ref: String,
25 pub asset_url: String,
27 pub sha256: Option<String>,
30 pub bin_sha256: Option<String>,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq)]
36pub struct Current {
37 pub version: String,
38 pub build_sha: Option<String>,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum Ancestry {
45 Ahead,
47 Behind,
49 Identical,
50 Diverged,
52}
53
54#[derive(Debug, Clone, PartialEq, Eq)]
56pub enum Decision {
57 UpToDate,
58 Available(Candidate),
59}
60
61fn parse(v: &str) -> Option<Version> {
62 Version::parse(v.trim_start_matches('v')).ok()
63}
64
65fn release_is_newer(
68 cur: &Current,
69 c: &Candidate,
70 ancestry: &dyn Fn(&str, &str) -> Option<Ancestry>,
71) -> bool {
72 let (Some(have), Some(want)) = (parse(&cur.version), parse(&c.version)) else {
73 return false;
74 };
75 if want <= have {
76 return false;
77 }
78 match &cur.build_sha {
79 None => true,
80 Some(sha) => !matches!(
81 ancestry(sha, &c.git_ref),
82 Some(Ancestry::Behind | Ancestry::Identical)
83 ),
84 }
85}
86
87fn nightly_is_newer(
91 cur: &Current,
92 c: &Candidate,
93 ancestry: &dyn Fn(&str, &str) -> Option<Ancestry>,
94) -> bool {
95 match &cur.build_sha {
96 Some(sha) => ancestry(sha, &c.git_ref) == Some(Ancestry::Ahead),
97 None => matches!((parse(&cur.version), parse(&c.version)), (Some(h), Some(w)) if w > h),
98 }
99}
100
101#[must_use]
105pub fn decide(
106 cur: &Current,
107 release: Option<&Candidate>,
108 nightly: Option<&Candidate>,
109 ancestry: &dyn Fn(&str, &str) -> Option<Ancestry>,
110) -> Decision {
111 let rel = release.filter(|c| release_is_newer(cur, c, ancestry));
112 let nig = nightly.filter(|c| nightly_is_newer(cur, c, ancestry));
113 let pick = match (rel, nig) {
114 (Some(r), Some(n)) => {
115 if ancestry(&r.git_ref, &n.git_ref) == Some(Ancestry::Ahead) {
116 n
117 } else {
118 r
119 }
120 }
121 (Some(r), None) => r,
122 (None, Some(n)) => n,
123 (None, None) => return Decision::UpToDate,
124 };
125 Decision::Available(pick.clone())
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131
132 fn rel(v: &str) -> Candidate {
133 Candidate {
134 source: Source::Release,
135 version: v.into(),
136 git_ref: format!("v{v}"),
137 asset_url: String::new(),
138 sha256: None,
139 bin_sha256: None,
140 }
141 }
142 fn nig(v: &str, sha: &str) -> Candidate {
143 Candidate {
144 source: Source::Nightly,
145 version: v.into(),
146 git_ref: sha.into(),
147 asset_url: String::new(),
148 sha256: Some("00".into()),
149 bin_sha256: Some("11".into()),
150 }
151 }
152 fn cur(v: &str, sha: Option<&str>) -> Current {
153 Current {
154 version: v.into(),
155 build_sha: sha.map(Into::into),
156 }
157 }
158 fn hist(base: &str, head: &str) -> Option<Ancestry> {
161 const ORDER: [&str; 4] = ["old", "v0.69.1", "mid", "new"];
162 if base == "offline" || head == "offline" {
163 return None;
164 }
165 if base == head {
166 return Some(Ancestry::Identical);
167 }
168 if base == "rb" || head == "rb" {
169 return Some(Ancestry::Diverged);
170 }
171 let (b, h) = (
172 ORDER.iter().position(|x| *x == base)?,
173 ORDER.iter().position(|x| *x == head)?,
174 );
175 Some(if h > b {
176 Ancestry::Ahead
177 } else {
178 Ancestry::Behind
179 })
180 }
181 fn offline(_: &str, _: &str) -> Option<Ancestry> {
182 None
183 }
184 fn src(d: &Decision) -> Option<(Source, String)> {
185 match d {
186 Decision::UpToDate => None,
187 Decision::Available(c) => Some((c.source, c.git_ref.clone())),
188 }
189 }
190
191 #[test]
193 fn decide_case_table() {
194 type Row = (
195 &'static str,
196 Current,
197 Option<Candidate>,
198 Option<Candidate>,
199 fn(&str, &str) -> Option<Ancestry>,
200 Option<(Source, &'static str)>,
201 );
202 let r = Some((Source::Release, "v0.69.1"));
203 let n = Some((Source::Nightly, "new"));
204 let rows: Vec<Row> = vec![
205 (
206 "newer release, no sha",
207 cur("0.69.0", None),
208 Some(rel("0.69.1")),
209 None,
210 hist,
211 r,
212 ),
213 (
214 "equal release",
215 cur("0.69.1", None),
216 Some(rel("0.69.1")),
217 None,
218 hist,
219 None,
220 ),
221 (
222 "older release is never a downgrade",
223 cur("0.70.0", None),
224 Some(rel("0.69.1")),
225 None,
226 hist,
227 None,
228 ),
229 (
230 "greater tag BEHIND the running commit is not newer",
231 cur("0.69.0", Some("mid")),
232 Some(rel("0.69.1")),
233 None,
234 hist,
235 None,
236 ),
237 (
238 "greater tag ahead of the running commit",
239 cur("0.69.0", Some("old")),
240 Some(rel("0.69.1")),
241 None,
242 hist,
243 r,
244 ),
245 (
246 "greater tag diverged (release branch) is newer",
247 cur("0.69.0", Some("rb")),
248 Some(rel("0.69.1")),
249 None,
250 hist,
251 r,
252 ),
253 (
254 "nightly ahead of running commit",
255 cur("0.69.1", Some("mid")),
256 None,
257 Some(nig("0.69.1", "new")),
258 hist,
259 n,
260 ),
261 (
262 "nightly at the running commit",
263 cur("0.69.1", Some("new")),
264 None,
265 Some(nig("0.69.1", "new")),
266 hist,
267 None,
268 ),
269 (
270 "nightly behind the running commit",
271 cur("0.69.1", Some("new")),
272 None,
273 Some(nig("0.69.1", "mid")),
274 hist,
275 None,
276 ),
277 (
278 "no sha, same version nightly: unknown never nags",
279 cur("0.69.1", None),
280 None,
281 Some(nig("0.69.1", "new")),
282 hist,
283 None,
284 ),
285 (
286 "no sha, greater version nightly",
287 cur("0.69.0", None),
288 None,
289 Some(nig("0.70.0", "new")),
290 hist,
291 n,
292 ),
293 (
294 "both newer, nightly ahead of tag -> nightly",
295 cur("0.69.0", Some("old")),
296 Some(rel("0.69.1")),
297 Some(nig("0.69.1", "new")),
298 hist,
299 n,
300 ),
301 (
302 "both newer, nightly behind tag -> release",
303 cur("0.69.0", None),
304 Some(rel("0.69.1")),
305 Some(nig("0.70.0", "old")),
306 hist,
307 r,
308 ),
309 (
310 "offline: release by semver, nightly unprovable",
311 cur("0.69.0", Some("old")),
312 Some(rel("0.69.1")),
313 Some(nig("0.69.1", "new")),
314 offline,
315 r,
316 ),
317 (
318 "offline with sha, nightly only: no nag",
319 cur("0.69.1", Some("mid")),
320 None,
321 Some(nig("0.69.1", "new")),
322 offline,
323 None,
324 ),
325 (
326 "unparseable current version: no nag",
327 cur("dev", None),
328 Some(rel("0.69.1")),
329 None,
330 hist,
331 None,
332 ),
333 (
334 "nothing published",
335 cur("0.69.0", None),
336 None,
337 None,
338 hist,
339 None,
340 ),
341 ];
342 let mut bad = Vec::new();
343 for (name, c, re, ni, anc, want) in rows {
344 let got = src(&decide(&c, re.as_ref(), ni.as_ref(), &anc));
345 let want = want.map(|(s, g)| (s, g.to_string()));
346 if got != want {
347 bad.push(format!("{name}: got {got:?}, want {want:?}"));
348 }
349 }
350 assert!(bad.is_empty(), "decide rows failed:\n{}", bad.join("\n"));
351 }
352}