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-07-29 21:14:44 +02:00
|
|
|
use crate::derivation::escape::escape_bstr;
|
2023-01-31 14:45:42 +01:00
|
|
|
use crate::derivation::output::Output;
|
2023-07-29 21:14:44 +02:00
|
|
|
use bstr::BString;
|
2023-01-13 18:19:48 +01:00
|
|
|
use std::collections::BTreeSet;
|
2023-07-29 21:14:44 +02:00
|
|
|
use std::io::Cursor;
|
|
|
|
use std::{collections::BTreeMap, io, io::Error, io::Write};
|
2023-01-02 21:00:59 +01:00
|
|
|
|
|
|
|
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 = '"';
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
// Writes a character to the writer.
|
|
|
|
pub(crate) fn write_char(writer: &mut impl Write, c: char) -> io::Result<()> {
|
|
|
|
let mut buf = [0; 4];
|
|
|
|
let b = c.encode_utf8(&mut buf).as_bytes();
|
|
|
|
io::copy(&mut Cursor::new(b), writer)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-30 09:05:28 +02:00
|
|
|
// Write a string `s` as a quoted field to the writer.
|
|
|
|
// The `escape` argument controls whether escaping will be skipped.
|
|
|
|
// This is the case if `s` is known to only contain characters that need no
|
|
|
|
// escaping.
|
|
|
|
pub(crate) fn write_field<S: AsRef<[u8]>>(
|
|
|
|
writer: &mut impl Write,
|
|
|
|
s: S,
|
|
|
|
escape: bool,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
write_char(writer, QUOTE)?;
|
|
|
|
|
|
|
|
if !escape {
|
|
|
|
io::copy(&mut Cursor::new(s), writer)?;
|
|
|
|
} else {
|
|
|
|
io::copy(&mut Cursor::new(escape_bstr(s.as_ref())), writer)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_char(writer, QUOTE)?;
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-30 09:05:28 +02:00
|
|
|
fn write_array_elements(writer: &mut impl Write, elements: &[BString]) -> Result<(), io::Error> {
|
2023-01-02 21:00:59 +01:00
|
|
|
for (index, element) in elements.iter().enumerate() {
|
|
|
|
if index > 0 {
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, COMMA)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
}
|
|
|
|
|
2023-07-30 09:05:28 +02:00
|
|
|
write_field(writer, element, true)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_outputs(
|
|
|
|
writer: &mut impl Write,
|
2023-01-04 12:26:37 +01:00
|
|
|
outputs: &BTreeMap<String, Output>,
|
2023-07-29 21:14:44 +02:00
|
|
|
) -> Result<(), io::Error> {
|
|
|
|
write_char(writer, BRACKET_OPEN)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
for (ii, (output_name, output)) in outputs.iter().enumerate() {
|
|
|
|
if ii > 0 {
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, COMMA)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
}
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
let mut elements: Vec<BString> = vec![
|
|
|
|
output_name.as_bytes().to_vec().into(),
|
|
|
|
output.path.as_bytes().to_vec().into(),
|
|
|
|
];
|
2023-01-04 13:36:27 +01:00
|
|
|
|
2023-03-14 17:16:05 +01:00
|
|
|
let (e2, e3) = match &output.hash_with_mode {
|
|
|
|
Some(hash) => match hash {
|
|
|
|
crate::nixhash::NixHashWithMode::Flat(h) => (
|
2023-07-29 21:14:44 +02:00
|
|
|
h.algo.to_string().as_bytes().to_vec(),
|
|
|
|
data_encoding::HEXLOWER.encode(&h.digest).as_bytes().into(),
|
2023-03-14 17:16:05 +01:00
|
|
|
),
|
|
|
|
crate::nixhash::NixHashWithMode::Recursive(h) => (
|
2023-07-29 21:14:44 +02:00
|
|
|
format!("r:{}", h.algo).as_bytes().to_vec(),
|
|
|
|
data_encoding::HEXLOWER.encode(&h.digest).as_bytes().into(),
|
2023-03-14 17:16:05 +01:00
|
|
|
),
|
|
|
|
},
|
2023-07-29 21:14:44 +02:00
|
|
|
None => (vec![], vec![]),
|
2023-03-14 17:16:05 +01:00
|
|
|
};
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
elements.push(e2.into());
|
|
|
|
elements.push(e3.into());
|
2023-01-02 21:00:59 +01:00
|
|
|
|
2023-07-30 09:05:28 +02:00
|
|
|
write_char(writer, PAREN_OPEN)?;
|
|
|
|
|
|
|
|
write_array_elements(writer, &elements)?;
|
|
|
|
|
|
|
|
write_char(writer, PAREN_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
}
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, BRACKET_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
|
|
|
|
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-07-29 21:14:44 +02:00
|
|
|
) -> Result<(), io::Error> {
|
|
|
|
write_char(writer, COMMA)?;
|
|
|
|
write_char(writer, BRACKET_OPEN)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
for (ii, (input_derivation_path, input_derivation)) in input_derivations.into_iter().enumerate()
|
|
|
|
{
|
2023-01-02 21:00:59 +01:00
|
|
|
if ii > 0 {
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, COMMA)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
}
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, PAREN_OPEN)?;
|
2023-07-30 09:05:28 +02:00
|
|
|
write_field(writer, input_derivation_path.as_str(), false)?;
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, COMMA)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
|
2023-07-30 09:05:28 +02:00
|
|
|
write_char(writer, BRACKET_OPEN)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
write_array_elements(
|
|
|
|
writer,
|
2023-07-29 21:14:44 +02:00
|
|
|
&input_derivation
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.as_bytes().to_vec().into())
|
|
|
|
.collect::<Vec<BString>>(),
|
2023-01-02 21:00:59 +01:00
|
|
|
)?;
|
2023-07-30 09:05:28 +02:00
|
|
|
write_char(writer, BRACKET_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, PAREN_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
}
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, BRACKET_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_input_sources(
|
|
|
|
writer: &mut impl Write,
|
2023-01-16 15:25:08 +01:00
|
|
|
input_sources: &BTreeSet<String>,
|
2023-07-29 21:14:44 +02:00
|
|
|
) -> Result<(), io::Error> {
|
|
|
|
write_char(writer, COMMA)?;
|
2023-01-04 12:26:37 +01:00
|
|
|
|
2023-07-30 09:05:28 +02:00
|
|
|
write_char(writer, BRACKET_OPEN)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
write_array_elements(
|
|
|
|
writer,
|
2023-07-29 21:14:44 +02:00
|
|
|
&input_sources
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.as_bytes().to_vec().into())
|
|
|
|
.collect::<Vec<BString>>(),
|
2023-01-02 21:00:59 +01:00
|
|
|
)?;
|
2023-07-30 09:05:28 +02:00
|
|
|
write_char(writer, BRACKET_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
pub fn write_system(writer: &mut impl Write, platform: &str) -> Result<(), Error> {
|
|
|
|
write_char(writer, COMMA)?;
|
2023-07-30 09:05:28 +02:00
|
|
|
write_field(writer, platform, true)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
pub fn write_builder(writer: &mut impl Write, builder: &str) -> Result<(), Error> {
|
|
|
|
write_char(writer, COMMA)?;
|
2023-07-30 09:05:28 +02:00
|
|
|
write_field(writer, builder, true)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-29 21:14:44 +02:00
|
|
|
pub fn write_arguments(writer: &mut impl Write, arguments: &[String]) -> Result<(), io::Error> {
|
|
|
|
write_char(writer, COMMA)?;
|
2023-07-30 09:05:28 +02:00
|
|
|
|
|
|
|
write_char(writer, BRACKET_OPEN)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
write_array_elements(
|
|
|
|
writer,
|
2023-07-29 21:14:44 +02:00
|
|
|
&arguments
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.as_bytes().to_vec().into())
|
|
|
|
.collect::<Vec<BString>>(),
|
2023-01-02 21:00:59 +01:00
|
|
|
)?;
|
2023-07-30 09:05:28 +02:00
|
|
|
write_char(writer, BRACKET_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_enviroment(
|
|
|
|
writer: &mut impl Write,
|
2023-07-29 21:14:44 +02:00
|
|
|
environment: &BTreeMap<String, BString>,
|
|
|
|
) -> Result<(), io::Error> {
|
|
|
|
write_char(writer, COMMA)?;
|
|
|
|
write_char(writer, BRACKET_OPEN)?;
|
|
|
|
|
|
|
|
for (i, (k, v)) in environment.into_iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
write_char(writer, COMMA)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
}
|
|
|
|
|
2023-07-30 09:05:28 +02:00
|
|
|
write_char(writer, PAREN_OPEN)?;
|
|
|
|
write_field(writer, k, false)?;
|
|
|
|
write_char(writer, COMMA)?;
|
|
|
|
write_field(writer, v, true)?;
|
|
|
|
write_char(writer, PAREN_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
}
|
|
|
|
|
2023-07-29 21:14:44 +02:00
|
|
|
write_char(writer, BRACKET_CLOSE)?;
|
2023-01-02 21:00:59 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|