inky-engine 3.0.1

A hand-fused Metal inference engine for Thinking Machines' Inkling 2 Mini, for Apple M5 Macs
//! Reads the commit a crates.io package was cut from. `build.rs` includes this file, and the binary's unit tests
//! check it, so the parser is the one the build runs.

/// The `git.sha1` that `cargo package` writes into a package's `.cargo_vcs_info.json`, when it is a full
/// 40-digit hex commit id; anything else is `None`.
pub fn vcs_info_sha(json: &str) -> Option<String> {
    let rest = &json[json.find("\"sha1\"")? + "\"sha1\"".len()..];
    let rest = rest.trim_start().strip_prefix(':')?.trim_start().strip_prefix('"')?;
    let sha = &rest[..rest.find('"')?];
    (sha.len() == 40 && sha.bytes().all(|b| b.is_ascii_hexdigit())).then(|| sha.to_ascii_lowercase())
}

#[cfg(test)]
mod tests {
    use super::vcs_info_sha;

    #[test]
    fn vcs_info_sha_reads_the_packaged_commit_and_rejects_anything_else() {
        let packaged = "{\n  \"git\": {\n    \"sha1\": \"b6b4c3bd1e5b86a344fe535024d6e82658b6d9bc\"\n  },\n  \"path_in_vcs\": \"\"\n}";
        assert_eq!(vcs_info_sha(packaged).as_deref(), Some("b6b4c3bd1e5b86a344fe535024d6e82658b6d9bc"));
        assert_eq!(
            vcs_info_sha("{\"git\":{\"sha1\":\"B6B4C3BD1E5B86A344FE535024D6E82658B6D9BC\",\"dirty\":true}}").as_deref(),
            Some("b6b4c3bd1e5b86a344fe535024d6e82658b6d9bc")
        );
        for bad in [
            "",
            "{}",
            "{\"git\":{\"sha1\":\"b6b4c3bd\"}}",
            "{\"git\":{\"sha1\":\"zzb4c3bd1e5b86a344fe535024d6e82658b6d9bc\"}}",
            "{\"git\":{\"sha1\":b6b4c3bd1e5b86a344fe535024d6e82658b6d9bc}}",
            "{\"git\":{\"sha1\":\"b6b4c3bd1e5b86a344fe535024d6e82658b6d9bc",
        ] {
            assert_eq!(vcs_info_sha(bad), None, "{bad:?}");
        }
    }
}