diff options
Diffstat (limited to 'tvix')
-rw-r--r-- | tvix/eval/src/builtins/mod.rs | 11 | ||||
-rw-r--r-- | tvix/eval/src/errors.rs | 2 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-literals.exp | 1 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-literals.nix | 11 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-thunks.exp | 1 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-thunks.nix | 9 | ||||
-rw-r--r-- | tvix/eval/src/value/attrs.rs | 21 | ||||
-rw-r--r-- | tvix/eval/src/value/list.rs | 4 | ||||
-rw-r--r-- | tvix/eval/src/value/mod.rs | 7 | ||||
-rw-r--r-- | tvix/eval/src/value/string.rs | 7 | ||||
-rw-r--r-- | tvix/eval/src/value/thunk.rs | 11 |
11 files changed, 75 insertions, 10 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 9c9d171a2102..331dcbcd419c 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -343,6 +343,17 @@ mod pure_builtins { serde_json::from_str(&json_str).map_err(|err| err.into()) } + #[builtin("toJSON")] + fn builtin_to_json(vm: &mut VM, val: Value) -> Result<Value, ErrorKind> { + // All thunks need to be evaluated before serialising, as the + // data structure is fully traversed by the Serializer (which + // does not have a `VM` available). + val.deep_force(vm, &mut Default::default())?; + + let json_str = serde_json::to_string(&val)?; + Ok(json_str.into()) + } + #[builtin("genericClosure")] fn builtin_generic_closure(vm: &mut VM, input: Value) -> Result<Value, ErrorKind> { let attrs = input.to_attrs()?; diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs index 416a4b23c0d1..6a463d9f96df 100644 --- a/tvix/eval/src/errors.rs +++ b/tvix/eval/src/errors.rs @@ -213,7 +213,7 @@ impl ErrorKind { impl From<serde_json::Error> for ErrorKind { fn from(err: serde_json::Error) -> Self { // Can't just put the `serde_json::Error` in the ErrorKind since it doesn't impl `Clone` - Self::FromJsonError(format!("Error parsing JSON: {err}")) + Self::FromJsonError(format!("error in JSON serialization: {err}")) } } diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-literals.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-literals.exp new file mode 100644 index 000000000000..0a274c201fa8 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-literals.exp @@ -0,0 +1 @@ +"[42,\"hello\",13.37,[],[1,2,3],{},{\"name\":\"foo\",\"value\":42},{\"foo\":42}]" diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-literals.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-literals.nix new file mode 100644 index 000000000000..12e8c03b171d --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-literals.nix @@ -0,0 +1,11 @@ +# tests serialisation of literal data +builtins.toJSON [ + 42 + "hello" + 13.37 + [ ] + [ 1 2 3 ] + { } + { name = "foo"; value = 42; } + { foo = 42; } +] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-thunks.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-thunks.exp new file mode 100644 index 000000000000..9ccd94224ba8 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-thunks.exp @@ -0,0 +1 @@ +"[42,42,\"42\"]" diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-thunks.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-thunks.nix new file mode 100644 index 000000000000..16234ab4514a --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tojson-thunks.nix @@ -0,0 +1,9 @@ +let + a = b * 2; + b = 21; +in +builtins.toJSON [ + a + ((n: n * 2) 21) + (builtins.toJSON a) +] diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs index c6b274f0b70e..d413f0073f26 100644 --- a/tvix/eval/src/value/attrs.rs +++ b/tvix/eval/src/value/attrs.rs @@ -9,7 +9,8 @@ use std::iter::FromIterator; use imbl::{ordmap, OrdMap}; use serde::de::{Deserializer, Error, Visitor}; -use serde::Deserialize; +use serde::ser::SerializeMap; +use serde::{Deserialize, Serialize}; use crate::errors::ErrorKind; use crate::vm::VM; @@ -140,6 +141,24 @@ impl TotalDisplay for NixAttrs { } } +impl Serialize for NixAttrs { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: serde::Serializer, + { + match &self.0 { + AttrsRep::Empty => serializer.serialize_map(Some(0))?.end(), + AttrsRep::KV { name, value } => { + let mut map = serializer.serialize_map(Some(2))?; + map.serialize_entry("name", name)?; + map.serialize_entry("value", value)?; + map.end() + } + AttrsRep::Im(map) => map.serialize(serializer), + } + } +} + impl<'de> Deserialize<'de> for NixAttrs { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where diff --git a/tvix/eval/src/value/list.rs b/tvix/eval/src/value/list.rs index 744130d2ac48..70952419abea 100644 --- a/tvix/eval/src/value/list.rs +++ b/tvix/eval/src/value/list.rs @@ -3,7 +3,7 @@ use std::ops::Index; use imbl::{vector, Vector}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use crate::errors::ErrorKind; use crate::vm::VM; @@ -13,7 +13,7 @@ use super::TotalDisplay; use super::Value; #[repr(transparent)] -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct NixList(Vector<Value>); impl TotalDisplay for NixList { diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs index 89e8fdd0937a..357ffa6161ff 100644 --- a/tvix/eval/src/value/mod.rs +++ b/tvix/eval/src/value/mod.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use std::rc::Rc; use std::{cell::Ref, fmt::Display}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; #[cfg(feature = "arbitrary")] mod arbitrary; @@ -33,7 +33,7 @@ pub use thunk::Thunk; use self::thunk::ThunkSet; #[warn(variant_size_differences)] -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[serde(untagged)] pub enum Value { Null, @@ -49,12 +49,13 @@ pub enum Value { #[serde(skip)] Closure(Rc<Closure>), // must use Rc<Closure> here in order to get proper pointer equality + #[serde(skip)] Builtin(Builtin), // Internal values that, while they technically exist at runtime, // are never returned to or created directly by users. - #[serde(skip)] + #[serde(skip_deserializing)] Thunk(Thunk), // See [`compiler::compile_select_or()`] for explanation diff --git a/tvix/eval/src/value/string.rs b/tvix/eval/src/value/string.rs index 93cbc98dab9c..5962c94ea51f 100644 --- a/tvix/eval/src/value/string.rs +++ b/tvix/eval/src/value/string.rs @@ -9,16 +9,17 @@ use std::path::Path; use std::{borrow::Cow, fmt::Display, str::Chars}; use serde::de::{Deserializer, Visitor}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize)] +#[serde(untagged)] enum StringRepr { Smol(SmolStr), Heap(String), } #[repr(transparent)] -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Serialize)] pub struct NixString(StringRepr); impl PartialEq for NixString { diff --git a/tvix/eval/src/value/thunk.rs b/tvix/eval/src/value/thunk.rs index 8813b0039888..a820e73307ad 100644 --- a/tvix/eval/src/value/thunk.rs +++ b/tvix/eval/src/value/thunk.rs @@ -24,6 +24,8 @@ use std::{ rc::Rc, }; +use serde::Serialize; + use crate::{ chunk::Chunk, errors::{Error, ErrorKind}, @@ -329,6 +331,15 @@ impl TotalDisplay for Thunk { } } +impl Serialize for Thunk { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: serde::Serializer, + { + self.value().serialize(serializer) + } +} + /// A wrapper type for tracking which thunks have already been seen in a /// context. This is necessary for cycle detection. /// |