refactor(nix-compat/store_path): centralize self_reference check

self_reference being set to true is only allowed for
`CAHash::Nar(NixHash::Sha256(_))`, so we can handle this in a check at
the front.

Change-Id: Ic363ade4789a7767cbe26a6959b143bb53e50e5a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10391
Reviewed-by: edef <edef@edef.eu>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Florian Klink 2023-12-21 17:05:54 +02:00 committed by flokli
parent 88adaea12b
commit 329a7d30a7

View file

@ -61,29 +61,28 @@ pub fn build_ca_path<'a, S: AsRef<str>, I: IntoIterator<Item = S>>(
references: I,
self_reference: bool,
) -> Result<StorePathRef<'a>, BuildStorePathError> {
let (ty, hash) = match &ca_hash {
CAHash::Text(ref digest) => {
if self_reference {
return Err(BuildStorePathError::InvalidReference());
}
// self references are only allowed for CAHash::Nar(NixHash::Sha256(_)).
if self_reference {
let CAHash::Nar(NixHash::Sha256(_)) = ca_hash else {
return Err(BuildStorePathError::InvalidReference());
};
}
(
make_references_string("text", references, false),
NixHash::Sha256(*digest),
)
}
let (ty, hash) = match &ca_hash {
CAHash::Text(ref digest) => (
make_references_string("text", references, false),
NixHash::Sha256(*digest),
),
CAHash::Nar(NixHash::Sha256(ref digest)) => (
make_references_string("source", references, self_reference),
NixHash::Sha256(*digest),
),
// for all other CAHash::Nar, another custom scheme is used.
CAHash::Nar(ref hash) => {
if references.into_iter().next().is_some() {
return Err(BuildStorePathError::InvalidReference());
}
if self_reference {
return Err(BuildStorePathError::InvalidReference());
}
(
"output:out".to_string(),
@ -99,9 +98,6 @@ pub fn build_ca_path<'a, S: AsRef<str>, I: IntoIterator<Item = S>>(
if references.into_iter().next().is_some() {
return Err(BuildStorePathError::InvalidReference());
}
if self_reference {
return Err(BuildStorePathError::InvalidReference());
}
(
"output:out".to_string(),