Skip to main content

pacenotes_server/store/
derived_version.rs

1/// Marks a version derived from a package's bytes rather than written in a sidecar.
2pub const DERIVED_VERSION_PREFIX: &str = "sha-";
3
4/// What every unversioned package got before the manifest became a pure
5/// function of the files on disk.
6const LEGACY_DEFAULT_VERSION: &str = "0.0.0";
7
8/// `sha-` + the first 8 hex chars of the sha256: the same on every rebuild and
9/// replica, and different for every payload, unlike the old fixed `0.0.0`.
10pub fn derived_version(sha256: &str) -> String {
11    format!("{DERIVED_VERSION_PREFIX}{}", &sha256[..sha256.len().min(8)])
12}
13
14/// A version a person chose, as opposed to the legacy default or a derived one.
15pub fn is_chosen_version(version: &str) -> bool {
16    version != LEGACY_DEFAULT_VERSION && !version.starts_with(DERIVED_VERSION_PREFIX)
17}
18
19#[cfg(test)]
20mod tests {
21    #[test]
22    fn a_derived_version_is_the_sha_prefix() {
23        assert_eq!(
24            super::derived_version(
25                "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
26            ),
27            "sha-2cf24dba"
28        );
29    }
30
31    #[test]
32    fn only_a_version_someone_wrote_counts_as_chosen() {
33        assert!(super::is_chosen_version("2.3.4"));
34        assert!(!super::is_chosen_version("0.0.0"));
35        assert!(!super::is_chosen_version("sha-2cf24dba"));
36    }
37}