refactor(tvix/derivation): make output hashes an Option<Hash>
This conveys better if an output of a Derivation is fixed-output or not, and provides a Hash struct that can be used to store the algo and digest. In case it's not, this can simply be None. The serde field attributes have been updated to still accept the same JSON. We currently still store the hash algo and digest as strings, mostly because the only thing populating it so far is the example JSONs. We might want to update this, once actual Nix code populates this. While updating write.rs, I pushed some of the Vec<String> to [&str] conversions inline, and made it a Vec<&str>, because it was annoying to juggle with. Change-Id: Ia9cd0568fe179ac22a4a636237f22ab4ad92b95b Reviewed-on: https://cl.tvl.fyi/c/depot/+/7746 Tested-by: BuildkiteCI Reviewed-by: jrhahn <mail.jhahn@gmail.com> Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
parent
468dc5cd0c
commit
77cc6a1f78
2 changed files with 35 additions and 29 deletions
|
@ -1,16 +1,23 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
// This function is required by serde to deserialize files
|
|
||||||
// with missing keys.
|
|
||||||
fn default_resource() -> String {
|
|
||||||
"".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Output {
|
pub struct Output {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
#[serde(default = "default_resource")]
|
|
||||||
pub hash_algorithm: String,
|
#[serde(flatten)]
|
||||||
#[serde(default = "default_resource")]
|
pub hash: Option<Hash>,
|
||||||
pub hash: String,
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct Hash {
|
||||||
|
#[serde(rename = "hash")]
|
||||||
|
pub digest: String,
|
||||||
|
#[serde(rename = "hash_algorithm")]
|
||||||
|
pub algo: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Output {
|
||||||
|
pub fn is_fixed(&self) -> bool {
|
||||||
|
self.hash.is_some()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ fn write_array_elements(
|
||||||
quote: bool,
|
quote: bool,
|
||||||
open: &str,
|
open: &str,
|
||||||
closing: &str,
|
closing: &str,
|
||||||
elements: &[&str],
|
elements: Vec<&str>,
|
||||||
) -> Result<(), fmt::Error> {
|
) -> Result<(), fmt::Error> {
|
||||||
writer.write_str(open)?;
|
writer.write_str(open)?;
|
||||||
|
|
||||||
|
@ -56,19 +56,25 @@ pub fn write_outputs(
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(jrhahn) option to strip output
|
// TODO(jrhahn) option to strip output
|
||||||
let elements: [&str; 4] = [
|
let mut elements: Vec<&str> = vec![output_name, &output.path];
|
||||||
&output_name,
|
|
||||||
&output.path,
|
match &output.hash {
|
||||||
&output.hash_algorithm,
|
Some(hash) => {
|
||||||
&output.hash,
|
elements.push(&hash.algo);
|
||||||
];
|
elements.push(&hash.digest);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
elements.push("");
|
||||||
|
elements.push("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
write_array_elements(
|
write_array_elements(
|
||||||
writer,
|
writer,
|
||||||
true,
|
true,
|
||||||
&PAREN_OPEN.to_string(),
|
&PAREN_OPEN.to_string(),
|
||||||
&PAREN_CLOSE.to_string(),
|
&PAREN_CLOSE.to_string(),
|
||||||
&elements,
|
elements,
|
||||||
)?
|
)?
|
||||||
}
|
}
|
||||||
writer.write_char(BRACKET_CLOSE)?;
|
writer.write_char(BRACKET_CLOSE)?;
|
||||||
|
@ -94,15 +100,12 @@ pub fn write_input_derivations(
|
||||||
writer.write_char(QUOTE)?;
|
writer.write_char(QUOTE)?;
|
||||||
writer.write_char(COMMA)?;
|
writer.write_char(COMMA)?;
|
||||||
|
|
||||||
// convert Vec<String> to [&str]
|
|
||||||
let v: Vec<&str> = input_derivation.iter().map(|x| &**x).collect();
|
|
||||||
|
|
||||||
write_array_elements(
|
write_array_elements(
|
||||||
writer,
|
writer,
|
||||||
true,
|
true,
|
||||||
&BRACKET_OPEN.to_string(),
|
&BRACKET_OPEN.to_string(),
|
||||||
&BRACKET_CLOSE.to_string(),
|
&BRACKET_CLOSE.to_string(),
|
||||||
&v,
|
input_derivation.iter().map(|s| &**s).collect(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
writer.write_char(PAREN_CLOSE)?;
|
writer.write_char(PAREN_CLOSE)?;
|
||||||
|
@ -119,14 +122,12 @@ pub fn write_input_sources(
|
||||||
) -> Result<(), fmt::Error> {
|
) -> Result<(), fmt::Error> {
|
||||||
writer.write_char(COMMA)?;
|
writer.write_char(COMMA)?;
|
||||||
|
|
||||||
// convert Vec<String> to [&str]
|
|
||||||
let v: Vec<&str> = input_sources.iter().map(|x| &**x).collect();
|
|
||||||
write_array_elements(
|
write_array_elements(
|
||||||
writer,
|
writer,
|
||||||
true,
|
true,
|
||||||
&BRACKET_OPEN.to_string(),
|
&BRACKET_OPEN.to_string(),
|
||||||
&BRACKET_CLOSE.to_string(),
|
&BRACKET_CLOSE.to_string(),
|
||||||
&v,
|
input_sources.iter().map(|s| &**s).collect(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -145,14 +146,12 @@ pub fn write_builder(writer: &mut impl Write, builder: &str) -> Result<(), fmt::
|
||||||
}
|
}
|
||||||
pub fn write_arguments(writer: &mut impl Write, arguments: &[String]) -> Result<(), fmt::Error> {
|
pub fn write_arguments(writer: &mut impl Write, arguments: &[String]) -> Result<(), fmt::Error> {
|
||||||
writer.write_char(COMMA)?;
|
writer.write_char(COMMA)?;
|
||||||
// convert Vec<String> to [&str]
|
|
||||||
let v: Vec<&str> = arguments.iter().map(|x| &**x).collect();
|
|
||||||
write_array_elements(
|
write_array_elements(
|
||||||
writer,
|
writer,
|
||||||
true,
|
true,
|
||||||
&BRACKET_OPEN.to_string(),
|
&BRACKET_OPEN.to_string(),
|
||||||
&BRACKET_CLOSE.to_string(),
|
&BRACKET_CLOSE.to_string(),
|
||||||
&v,
|
arguments.iter().map(|s| &**s).collect(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -176,7 +175,7 @@ pub fn write_enviroment(
|
||||||
false,
|
false,
|
||||||
&PAREN_OPEN.to_string(),
|
&PAREN_OPEN.to_string(),
|
||||||
&PAREN_CLOSE.to_string(),
|
&PAREN_CLOSE.to_string(),
|
||||||
&[&escape_string(key), &escape_string(environment)],
|
vec![&escape_string(key), &escape_string(environment)],
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue