Enum nix_compat::nixhash::CAHash
source · pub enum CAHash {
Flat(NixHash),
Nar(NixHash),
Text([u8; 32]),
}
Expand description
A Nix CAHash describes a content-addressed hash of a path.
The way Nix prints it as a string is a bit confusing, but there’s essentially
three modes, Flat
, Nar
and Text
.
Flat
and Nar
support all 4 algos that NixHash supports
(sha1, md5, sha256, sha512), Text
only supports sha256.
Variants§
Implementations§
source§impl CAHash
impl CAHash
pub fn hash(&self) -> Cow<'_, NixHash>
pub fn mode(&self) -> HashMode
sourcepub fn algo_str(&self) -> &'static str
pub fn algo_str(&self) -> &'static str
Returns a colon-separated string consisting of mode, recursiveness and hash algo. Used as a prefix in various string representations.
sourcepub fn from_nix_hex_str(s: &str) -> Option<Self>
pub fn from_nix_hex_str(s: &str) -> Option<Self>
Constructs a CAHash from the textual representation, which is one of the three:
text:sha256:$nixbase32sha256digest
fixed:r:$algo:$nixbase32digest
fixed:$algo:$nixbase32digest
which is the format that’s used in the NARInfo for example.
sourcepub fn to_nix_nixbase32_string(&self) -> String
pub fn to_nix_nixbase32_string(&self) -> String
Formats a CAHash in the Nix default hash format, which is the format that’s used in NARInfos for example.
sourcepub(crate) fn from_map<'de, D>(
map: &Map<String, Value>
) -> Result<Option<Self>, D::Error>where
D: Deserializer<'de>,
pub(crate) fn from_map<'de, D>(
map: &Map<String, Value>
) -> Result<Option<Self>, D::Error>where
D: Deserializer<'de>,
This takes a serde_json::Map and turns it into this structure. This is necessary to do such shenigans because we have external consumers, like the Derivation parser, who would like to know whether we have a invalid or a missing NixHashWithMode structure in another structure, e.g. Output. This means we have this combinatorial situation:
- no hash, no hashAlgo: no CAHash so we return Ok(None).
- present hash, missing hashAlgo: invalid, we will return missing_field
- missing hash, present hashAlgo: same
- present hash, present hashAlgo: either we return ourselves or a type/value validation error. This function is for internal consumption regarding those needs until we have a better solution. Now this is said, let’s explain how this works.
We want to map the serde data model into a CAHash.
The serde data model has a hash
field (containing a digest in nixbase32),
and a hashAlgo
field, containing the stringified hash algo.
In case the hash is recursive, hashAlgo also has a r:
prefix.
This is to match how nix show-derivation
command shows them in JSON
representation.