From abd539ddb8778a19f32d8ed7e030a28670e1c580 Mon Sep 17 00:00:00 2001 From: Jürgen Hahn Date: Wed, 4 Jan 2023 12:26:37 +0100 Subject: feat(tvix/derivation) Add fmt::Display implementation for Derivation This adds the implementation of fmt::Display for Derivation so that we can easily store the formatted content as a string. Internally, we use the serialization function to generate the string. Change-Id: I6caca0d6c1bea3ca44b6c535c5b1d5d66d8413b7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7741 Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/derivation/src/derivation.rs | 18 ++++++++++++------ tvix/derivation/src/tests/mod.rs | 12 +++++++++--- tvix/derivation/src/write.rs | 34 +++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 22 deletions(-) (limited to 'tvix/derivation/src') diff --git a/tvix/derivation/src/derivation.rs b/tvix/derivation/src/derivation.rs index 142fa3c9f3..406d2e0a57 100644 --- a/tvix/derivation/src/derivation.rs +++ b/tvix/derivation/src/derivation.rs @@ -15,20 +15,26 @@ pub struct Derivation { } impl Derivation { - pub fn serialize(self: Self, writer: &mut impl Write) -> Result<(), fmt::Error> { + pub fn serialize(&self, writer: &mut impl Write) -> Result<(), fmt::Error> { writer.write_str(write::DERIVATION_PREFIX)?; writer.write_char(write::PAREN_OPEN)?; - write::write_outputs(writer, self.outputs)?; - write::write_input_derivations(writer, self.input_derivations)?; - write::write_input_sources(writer, self.input_sources)?; + write::write_outputs(writer, &self.outputs)?; + write::write_input_derivations(writer, &self.input_derivations)?; + write::write_input_sources(writer, &self.input_sources)?; write::write_platfrom(writer, &self.platform)?; write::write_builder(writer, &self.builder)?; - write::write_arguments(writer, self.arguments)?; - write::write_enviroment(writer, self.environment)?; + write::write_arguments(writer, &self.arguments)?; + write::write_enviroment(writer, &self.environment)?; writer.write_char(write::PAREN_CLOSE)?; Ok(()) } } + +impl fmt::Display for Derivation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.serialize(f) + } +} diff --git a/tvix/derivation/src/tests/mod.rs b/tvix/derivation/src/tests/mod.rs index 8008069269..3d4aae3fe4 100644 --- a/tvix/derivation/src/tests/mod.rs +++ b/tvix/derivation/src/tests/mod.rs @@ -14,7 +14,8 @@ fn read_file(path: &str) -> String { return data; } -fn assert_derivation_ok(path_to_drv_file: &str) { +#[test_resources("src/tests/derivation_tests/*.drv")] +fn check_serizaliation(path_to_drv_file: &str) { let data = read_file(&format!("{}.json", path_to_drv_file)); let derivation: Derivation = serde_json::from_str(&data).expect("JSON was not well-formatted"); @@ -27,6 +28,11 @@ fn assert_derivation_ok(path_to_drv_file: &str) { } #[test_resources("src/tests/derivation_tests/*.drv")] -fn derivation_files_ok(path: &str) { - assert_derivation_ok(path); +fn check_to_string(path_to_drv_file: &str) { + let data = read_file(&format!("{}.json", path_to_drv_file)); + let derivation: Derivation = serde_json::from_str(&data).expect("JSON was not well-formatted"); + + let expected = read_file(path_to_drv_file); + + assert_eq!(expected, derivation.to_string()); } diff --git a/tvix/derivation/src/write.rs b/tvix/derivation/src/write.rs index 8e5899b9ec..987c924fae 100644 --- a/tvix/derivation/src/write.rs +++ b/tvix/derivation/src/write.rs @@ -15,7 +15,7 @@ fn write_array_elements( quote: bool, open: &str, closing: &str, - elements: Vec<&String>, + elements: &[&str], ) -> Result<(), fmt::Error> { writer.write_str(open)?; @@ -42,7 +42,7 @@ fn write_array_elements( pub fn write_outputs( writer: &mut impl Write, - outputs: BTreeMap, + outputs: &BTreeMap, ) -> Result<(), fmt::Error> { writer.write_char(BRACKET_OPEN)?; for (ii, (output_name, output)) in outputs.iter().enumerate() { @@ -51,8 +51,8 @@ pub fn write_outputs( } // TODO(jrhahn) option to strip output - let elements = vec![ - output_name, + let elements: [&str; 4] = [ + &output_name, &output.path, &output.hash_algorithm, &output.hash, @@ -63,7 +63,7 @@ pub fn write_outputs( true, &PAREN_OPEN.to_string(), &PAREN_CLOSE.to_string(), - elements, + &elements, )? } writer.write_char(BRACKET_CLOSE)?; @@ -73,7 +73,7 @@ pub fn write_outputs( pub fn write_input_derivations( writer: &mut impl Write, - input_derivations: BTreeMap>, + input_derivations: &BTreeMap>, ) -> Result<(), fmt::Error> { writer.write_char(COMMA)?; writer.write_char(BRACKET_OPEN)?; @@ -89,12 +89,15 @@ pub fn write_input_derivations( writer.write_char(QUOTE)?; writer.write_char(COMMA)?; + // convert Vec to [&str] + let v: Vec<&str> = input_derivation.iter().map(|x| &**x).collect(); + write_array_elements( writer, true, &BRACKET_OPEN.to_string(), &BRACKET_CLOSE.to_string(), - input_derivation.iter().collect(), + &v, )?; writer.write_char(PAREN_CLOSE)?; @@ -107,15 +110,18 @@ pub fn write_input_derivations( pub fn write_input_sources( writer: &mut impl Write, - input_sources: Vec, + input_sources: &Vec, ) -> Result<(), fmt::Error> { writer.write_char(COMMA)?; + + // convert Vec to [&str] + let v: Vec<&str> = input_sources.iter().map(|x| &**x).collect(); write_array_elements( writer, true, &BRACKET_OPEN.to_string(), &BRACKET_CLOSE.to_string(), - input_sources.iter().collect(), + &v, )?; Ok(()) @@ -133,14 +139,16 @@ pub fn write_builder(writer: &mut impl Write, builder: &str) -> Result<(), fmt:: Ok(()) } -pub fn write_arguments(writer: &mut impl Write, arguments: Vec) -> Result<(), fmt::Error> { +pub fn write_arguments(writer: &mut impl Write, arguments: &Vec) -> Result<(), fmt::Error> { writer.write_char(COMMA)?; + // convert Vec to [&str] + let v: Vec<&str> = arguments.iter().map(|x| &**x).collect(); write_array_elements( writer, true, &BRACKET_OPEN.to_string(), &BRACKET_CLOSE.to_string(), - arguments.iter().collect(), + &v, )?; Ok(()) @@ -148,7 +156,7 @@ pub fn write_arguments(writer: &mut impl Write, arguments: Vec) -> Resul pub fn write_enviroment( writer: &mut impl Write, - environment: BTreeMap, + environment: &BTreeMap, ) -> Result<(), fmt::Error> { writer.write_char(COMMA)?; writer.write_char(BRACKET_OPEN)?; @@ -164,7 +172,7 @@ pub fn write_enviroment( false, &PAREN_OPEN.to_string(), &PAREN_CLOSE.to_string(), - vec![&escape_string(key), &escape_string(environment)], + &[&escape_string(key), &escape_string(environment)], )?; } -- cgit 1.4.1