diff options
author | Florian Klink <flokli@flokli.de> | 2024-05-23T09·06+0200 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-05-23T14·49+0000 |
commit | ec8d79f3db2fc3a5ab7af048209cc2cc5ab14bd3 (patch) | |
tree | 375a80f1d5a013f1d3299f830284a5fc812e29d9 /tvix/eval/src/builtins/to_xml.rs | |
parent | a4a313cdd28b30eff54b8455f11df9de32640548 (diff) |
feat(tvix/eval): teach builtins.toXML context r/8163
XmlEmitter gains a NixContext field, and `write_typed_value` extends it with all context elements present in the passed value. Once all serialization is done, a into_context() function returns the collected context, so we can construct a NixString with context. Tests for this live in tvix-glue, as we use builtins.derivation, which is not present in the tvix-eval crate. Fixes b/398. Change-Id: I85feaaa17b753885f8a017a54e419ec4e602af21 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11704 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de> Autosubmit: flokli <flokli@flokli.de> Reviewed-by: Alyssa Ross <hi@alyssa.is>
Diffstat (limited to 'tvix/eval/src/builtins/to_xml.rs')
-rw-r--r-- | tvix/eval/src/builtins/to_xml.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/tvix/eval/src/builtins/to_xml.rs b/tvix/eval/src/builtins/to_xml.rs index 10a6d8cdb358..093e127fe25e 100644 --- a/tvix/eval/src/builtins/to_xml.rs +++ b/tvix/eval/src/builtins/to_xml.rs @@ -6,11 +6,12 @@ use bstr::ByteSlice; use std::borrow::Cow; use std::{io::Write, rc::Rc}; -use crate::{ErrorKind, Value}; +use crate::{ErrorKind, NixContext, NixContextElement, Value}; /// Recursively serialise a value to XML. The value *must* have been /// deep-forced before being passed to this function. -pub fn value_to_xml<W: Write>(mut writer: W, value: &Value) -> Result<(), ErrorKind> { +/// On success, returns the NixContext. +pub fn value_to_xml<W: Write>(mut writer: W, value: &Value) -> Result<NixContext, ErrorKind> { // Write a literal document declaration, using C++-Nix-style // single quotes. writeln!(writer, "<?xml version='1.0' encoding='utf-8'?>")?; @@ -21,7 +22,7 @@ pub fn value_to_xml<W: Write>(mut writer: W, value: &Value) -> Result<(), ErrorK value_variant_to_xml(&mut emitter, value)?; emitter.write_closing_tag("expr")?; - Ok(()) + Ok(emitter.into_context()) } fn write_typed_value<W: Write, V: ToString>( @@ -45,7 +46,12 @@ fn value_variant_to_xml<W: Write>(w: &mut XmlEmitter<W>, value: &Value) -> Resul Value::Bool(b) => return write_typed_value(w, "bool", b), Value::Integer(i) => return write_typed_value(w, "int", i), Value::Float(f) => return write_typed_value(w, "float", f), - Value::String(s) => return write_typed_value(w, "string", s.to_str()?), + Value::String(s) => { + if let Some(context) = s.context() { + w.extend_context(context.iter().cloned()); + } + return write_typed_value(w, "string", s.to_str()?); + } Value::Path(p) => return write_typed_value(w, "path", p.to_string_lossy()), Value::List(list) => { @@ -137,6 +143,7 @@ struct XmlEmitter<W> { /// The current indentation cur_indent: usize, writer: W, + context: NixContext, } impl<W: Write> XmlEmitter<W> { @@ -144,6 +151,7 @@ impl<W: Write> XmlEmitter<W> { XmlEmitter { cur_indent: 0, writer, + context: Default::default(), } } @@ -245,6 +253,19 @@ impl<W: Write> XmlEmitter<W> { _ => None, } } + + /// Extends the existing context with more context elements. + fn extend_context<T>(&mut self, iter: T) + where + T: IntoIterator<Item = NixContextElement>, + { + self.context.extend(iter) + } + + /// Consumes [Self] and returns the [NixContext] collected. + fn into_context(self) -> NixContext { + self.context + } } #[cfg(test)] |