Skip to main content

crate_generation

Function crate_generation 

Source
pub fn crate_generation(crate_dir: &Path) -> Option<String>
Expand description

The generation of the crate rooted at crate_dir: a hex SHA-256 over every file of GENERATION_INPUTS, each framed by the length and bytes of its crate-relative path and by its own length, so that no rename, concatenation or reordering can produce the digest of a different tree.

Files whose name begins with . are skipped. An editor’s swap file or a .DS_Store dropped into src/ after the build would otherwise move the tree’s generation without moving anything the compiler read, and the refusal that followed would be a lie.

None when crate_dir carries no crate at all: there is nothing there to be a generation of, and the caller — a released binary in a foreign repository — has no question to ask.

use majordomus_cli::generation::crate_generation;

let tree = tempfile::tempdir().unwrap();
// a directory with no crate in it is not a generation of anything
assert_eq!(crate_generation(tree.path()), None);

std::fs::create_dir_all(tree.path().join("src")).unwrap();
std::fs::write(tree.path().join("src/lib.rs"), "pub fn f() {}\n").unwrap();
let before = crate_generation(tree.path()).unwrap();
assert_eq!(before.len(), 64);

// one comment, no behaviour, and it is already another generation: the executable this
// tree builds is not the executable it built a moment ago, and only this says so
std::fs::write(tree.path().join("src/lib.rs"), "// note\npub fn f() {}\n").unwrap();
assert_ne!(before, crate_generation(tree.path()).unwrap());