2023-01-23 13:19:27 +01:00
|
|
|
//! This module implements the serialisation of derivations into the
|
|
|
|
//! [ATerm][] format used by C++ Nix.
|
|
|
|
//!
|
|
|
|
//! [ATerm]: http://program-transformation.org/Tools/ATermFormat.html
|
|
|
|
|
2023-01-02 21:00:59 +01:00
|
|
|
use crate::output::Output;
|
|
|
|
use crate::string_escape::escape_string;
|
2023-01-13 18:19:48 +01:00
|
|
|
use std::collections::BTreeSet;
|
2023-01-02 21:00:59 +01:00
|
|
|
use std::{collections::BTreeMap, fmt, fmt::Write};
|
|
|
|
|
|
|
|
pub const DERIVATION_PREFIX: &str = "Derive";
|
|
|
|
pub const PAREN_OPEN: char = '(';
|
|
|
|
pub const PAREN_CLOSE: char = ')';
|
|
|
|
pub const BRACKET_OPEN: char = '[';
|
|
|
|
pub const BRACKET_CLOSE: char = ']';
|
|
|
|
pub const COMMA: char = ',';
|
|
|
|
pub const QUOTE: char = '"';
|
|
|
|
|
|
|
|
fn write_array_elements(
|
|
|
|
writer: &mut impl Write,
|
|
|
|
quote: bool,
|
|
|
|
open: &str,
|
|
|
|
closing: &str,
|
2023-01-04 13:36:27 +01:00
|
|
|
elements: Vec<&str>,
|
2023-01-02 21:00:59 +01:00
|
|
|
) -> Result<(), fmt::Error> {
|
|
|
|
writer.write_str(open)?;
|
|
|
|
|
|
|
|
for (index, element) in elements.iter().enumerate() {
|
|
|
|
if index > 0 {
|
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if quote {
|
|
|
|
writer.write_char(QUOTE)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.write_str(element)?;
|
|
|
|
|
|
|
|
if quote {
|
|
|
|
writer.write_char(QUOTE)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.write_str(closing)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_outputs(
|
|
|
|
writer: &mut impl Write,
|
2023-01-04 12:26:37 +01:00
|
|
|
outputs: &BTreeMap<String, Output>,
|
2023-01-02 21:00:59 +01:00
|
|
|
) -> Result<(), fmt::Error> {
|
|
|
|
writer.write_char(BRACKET_OPEN)?;
|
|
|
|
for (ii, (output_name, output)) in outputs.iter().enumerate() {
|
|
|
|
if ii > 0 {
|
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:36:27 +01:00
|
|
|
let mut elements: Vec<&str> = vec![output_name, &output.path];
|
|
|
|
|
|
|
|
match &output.hash {
|
|
|
|
Some(hash) => {
|
|
|
|
elements.push(&hash.algo);
|
|
|
|
elements.push(&hash.digest);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
elements.push("");
|
|
|
|
elements.push("");
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 21:00:59 +01:00
|
|
|
|
|
|
|
write_array_elements(
|
|
|
|
writer,
|
|
|
|
true,
|
|
|
|
&PAREN_OPEN.to_string(),
|
|
|
|
&PAREN_CLOSE.to_string(),
|
2023-01-04 13:36:27 +01:00
|
|
|
elements,
|
2023-01-02 21:00:59 +01:00
|
|
|
)?
|
|
|
|
}
|
|
|
|
writer.write_char(BRACKET_CLOSE)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_input_derivations(
|
|
|
|
writer: &mut impl Write,
|
2023-01-13 18:19:48 +01:00
|
|
|
input_derivations: &BTreeMap<String, BTreeSet<String>>,
|
2023-01-02 21:00:59 +01:00
|
|
|
) -> Result<(), fmt::Error> {
|
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
writer.write_char(BRACKET_OPEN)?;
|
|
|
|
|
|
|
|
for (ii, (input_derivation_path, input_derivation)) in input_derivations.iter().enumerate() {
|
|
|
|
if ii > 0 {
|
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.write_char(PAREN_OPEN)?;
|
|
|
|
writer.write_char(QUOTE)?;
|
|
|
|
writer.write_str(input_derivation_path.as_str())?;
|
|
|
|
writer.write_char(QUOTE)?;
|
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
|
|
|
|
write_array_elements(
|
|
|
|
writer,
|
|
|
|
true,
|
|
|
|
&BRACKET_OPEN.to_string(),
|
|
|
|
&BRACKET_CLOSE.to_string(),
|
2023-01-04 13:36:27 +01:00
|
|
|
input_derivation.iter().map(|s| &**s).collect(),
|
2023-01-02 21:00:59 +01:00
|
|
|
)?;
|
|
|
|
|
|
|
|
writer.write_char(PAREN_CLOSE)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.write_char(BRACKET_CLOSE)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_input_sources(
|
|
|
|
writer: &mut impl Write,
|
2023-01-16 15:25:08 +01:00
|
|
|
input_sources: &BTreeSet<String>,
|
2023-01-02 21:00:59 +01:00
|
|
|
) -> Result<(), fmt::Error> {
|
|
|
|
writer.write_char(COMMA)?;
|
2023-01-04 12:26:37 +01:00
|
|
|
|
2023-01-02 21:00:59 +01:00
|
|
|
write_array_elements(
|
|
|
|
writer,
|
|
|
|
true,
|
|
|
|
&BRACKET_OPEN.to_string(),
|
|
|
|
&BRACKET_CLOSE.to_string(),
|
2023-01-04 13:36:27 +01:00
|
|
|
input_sources.iter().map(|s| &**s).collect(),
|
2023-01-02 21:00:59 +01:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:26:05 +01:00
|
|
|
pub fn write_system(writer: &mut impl Write, platform: &str) -> Result<(), fmt::Error> {
|
2023-01-02 21:00:59 +01:00
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
writer.write_str(escape_string(platform).as_str())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_builder(writer: &mut impl Write, builder: &str) -> Result<(), fmt::Error> {
|
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
writer.write_str(escape_string(builder).as_str())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-02 16:09:18 +01:00
|
|
|
pub fn write_arguments(writer: &mut impl Write, arguments: &[String]) -> Result<(), fmt::Error> {
|
2023-01-02 21:00:59 +01:00
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
write_array_elements(
|
|
|
|
writer,
|
|
|
|
true,
|
|
|
|
&BRACKET_OPEN.to_string(),
|
|
|
|
&BRACKET_CLOSE.to_string(),
|
2023-01-04 13:36:27 +01:00
|
|
|
arguments.iter().map(|s| &**s).collect(),
|
2023-01-02 21:00:59 +01:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_enviroment(
|
|
|
|
writer: &mut impl Write,
|
2023-01-04 12:26:37 +01:00
|
|
|
environment: &BTreeMap<String, String>,
|
2023-01-02 21:00:59 +01:00
|
|
|
) -> Result<(), fmt::Error> {
|
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
writer.write_char(BRACKET_OPEN)?;
|
|
|
|
|
|
|
|
for (ii, (key, environment)) in environment.iter().enumerate() {
|
|
|
|
if ii > 0 {
|
|
|
|
writer.write_char(COMMA)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_array_elements(
|
|
|
|
writer,
|
|
|
|
false,
|
|
|
|
&PAREN_OPEN.to_string(),
|
|
|
|
&PAREN_CLOSE.to_string(),
|
2023-01-04 13:36:27 +01:00
|
|
|
vec![&escape_string(key), &escape_string(environment)],
|
2023-01-02 21:00:59 +01:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.write_char(BRACKET_CLOSE)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|