fix(tvix/nix-compat): Make CAHash deserialize more formats

Currently CAHash only deserializes the hash in hex code while
the serializer outputs a nixbase32 hash. This means that you can't currently
deserialize what has been serialized.

This change makes deserialize support any digest format (so hex, nixbase32
and base64) as well as flattens the deserialize code and error handling.

It also implements serde methods of HashAlgo directly using Display and TryFrom
implementations because otherwise these would get serialized as eg. Sha256 instead
of sha256 which also broke CAHash serialize/deserialize.

Change-Id: I1941a72eaec741e4956292adaaf0115b97f260ba
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11082
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Brian Olsen 2024-03-01 21:51:23 +01:00
parent 260c2938d4
commit eff2cc4f68
3 changed files with 232 additions and 69 deletions

View file

@ -157,3 +157,33 @@ fn deserialize_with_error_missing_hash_fixed_output() {
assert!(output.is_err());
}
#[test]
fn serialize_deserialize() {
let json_bytes = r#"
{
"path": "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432"
}"#;
let output: Output = serde_json::from_str(json_bytes).expect("must parse");
let s = serde_json::to_string(&output).expect("Serialize");
let output2: Output = serde_json::from_str(&s).expect("must parse again");
assert_eq!(output, output2);
}
#[test]
fn serialize_deserialize_fixed() {
let json_bytes = r#"
{
"path": "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432",
"hash": "08813cbee9903c62be4c5027726a418a300da4500b2d369d3af9286f4815ceba",
"hashAlgo": "r:sha256"
}"#;
let output: Output = serde_json::from_str(json_bytes).expect("must parse");
let s = serde_json::to_string_pretty(&output).expect("Serialize");
let output2: Output = serde_json::from_str(&s).expect("must parse again");
assert_eq!(output, output2);
}