refactor(tvix/nix-compat): make NixHash an enum with fixed-len bytes
Less Vec<u8> passed around. Change-Id: Ie153a6bfaa084d7490ffa38634efdf5f3c31a768 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9722 Reviewed-by: Connor Brewster <cbrewster@hey.com> Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI
This commit is contained in:
parent
786b0324a9
commit
4ae0f428bd
8 changed files with 226 additions and 220 deletions
|
|
@ -6,6 +6,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|||
use serde_json::{Map, Value};
|
||||
|
||||
use super::algos::SUPPORTED_ALGOS;
|
||||
use super::from_algo_and_digest;
|
||||
|
||||
pub enum NixHashMode {
|
||||
Flat,
|
||||
|
|
@ -103,40 +104,38 @@ impl NixHashWithMode {
|
|||
if let Some(s) = v.as_str() {
|
||||
match s.strip_prefix("r:") {
|
||||
Some(rest) => Ok(Some(Self::Recursive(
|
||||
(
|
||||
from_algo_and_digest(
|
||||
HashAlgo::try_from(rest).map_err(|e| {
|
||||
serde::de::Error::invalid_value(
|
||||
Unexpected::Other(&e.to_string()),
|
||||
&format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(),
|
||||
)
|
||||
})?,
|
||||
digest,
|
||||
&digest,
|
||||
)
|
||||
.try_into()
|
||||
.map_err(|e: nixhash::Error| {
|
||||
serde::de::Error::invalid_value(
|
||||
Unexpected::Other(&e.to_string()),
|
||||
&"a digest with right length",
|
||||
)
|
||||
})?,
|
||||
.map_err(|e: nixhash::Error| {
|
||||
serde::de::Error::invalid_value(
|
||||
Unexpected::Other(&e.to_string()),
|
||||
&"a digest with right length",
|
||||
)
|
||||
})?,
|
||||
))),
|
||||
None => Ok(Some(Self::Flat(
|
||||
(
|
||||
from_algo_and_digest(
|
||||
HashAlgo::try_from(s).map_err(|e| {
|
||||
serde::de::Error::invalid_value(
|
||||
Unexpected::Other(&e.to_string()),
|
||||
&format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(),
|
||||
)
|
||||
})?,
|
||||
digest,
|
||||
&digest,
|
||||
)
|
||||
.try_into()
|
||||
.map_err(|e: nixhash::Error| {
|
||||
serde::de::Error::invalid_value(
|
||||
Unexpected::Other(&e.to_string()),
|
||||
&"a digest with right length",
|
||||
)
|
||||
})?,
|
||||
.map_err(|e: nixhash::Error| {
|
||||
serde::de::Error::invalid_value(
|
||||
Unexpected::Other(&e.to_string()),
|
||||
&"a digest with right length",
|
||||
)
|
||||
})?,
|
||||
))),
|
||||
}
|
||||
} else {
|
||||
|
|
@ -162,12 +161,12 @@ impl Serialize for NixHashWithMode {
|
|||
let mut map = serializer.serialize_map(Some(2))?;
|
||||
match self {
|
||||
NixHashWithMode::Flat(h) => {
|
||||
map.serialize_entry("hash", &nixbase32::encode(&h.digest))?;
|
||||
map.serialize_entry("hashAlgo", &h.algo.to_string())?;
|
||||
map.serialize_entry("hash", &nixbase32::encode(h.digest_as_bytes()))?;
|
||||
map.serialize_entry("hashAlgo", &h.algo())?;
|
||||
}
|
||||
NixHashWithMode::Recursive(h) => {
|
||||
map.serialize_entry("hash", &nixbase32::encode(&h.digest))?;
|
||||
map.serialize_entry("hashAlgo", &format!("r:{}", &h.algo.to_string()))?;
|
||||
map.serialize_entry("hash", &nixbase32::encode(h.digest_as_bytes()))?;
|
||||
map.serialize_entry("hashAlgo", &format!("r:{}", &h.algo()))?;
|
||||
}
|
||||
};
|
||||
map.end()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue