2022-12-31 18:13:59 +03:00
|
|
|
//! Deserialisation from Nix to Rust values.
|
|
|
|
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 17:25:52 -05:00
|
|
|
use bstr::ByteSlice;
|
2023-01-01 15:32:14 +03:00
|
|
|
use serde::de::value::{MapDeserializer, SeqDeserializer};
|
2023-01-02 13:39:42 +03:00
|
|
|
use serde::de::{self, EnumAccess, VariantAccess};
|
2023-06-16 14:49:58 +03:00
|
|
|
pub use tvix_eval::Evaluation;
|
2024-01-16 14:19:16 +02:00
|
|
|
use tvix_eval::{EvalIO, Value};
|
2022-12-31 18:13:59 +03:00
|
|
|
|
|
|
|
use crate::error::Error;
|
|
|
|
|
2023-01-01 15:32:14 +03:00
|
|
|
struct NixDeserializer {
|
2022-12-31 18:13:59 +03:00
|
|
|
value: tvix_eval::Value,
|
|
|
|
}
|
|
|
|
|
2023-01-01 15:32:14 +03:00
|
|
|
impl NixDeserializer {
|
|
|
|
fn new(value: Value) -> Self {
|
|
|
|
if let Value::Thunk(thunk) = value {
|
|
|
|
Self::new(thunk.value().clone())
|
|
|
|
} else {
|
|
|
|
Self { value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl de::IntoDeserializer<'_, Error> for NixDeserializer {
|
|
|
|
type Deserializer = Self;
|
|
|
|
|
|
|
|
fn into_deserializer(self) -> Self::Deserializer {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 14:49:58 +03:00
|
|
|
/// Evaluate the Nix code in `src` and attempt to deserialise the
|
|
|
|
/// value it returns to `T`.
|
2022-12-31 18:13:59 +03:00
|
|
|
pub fn from_str<'code, T>(src: &'code str) -> Result<T, Error>
|
|
|
|
where
|
|
|
|
T: serde::Deserialize<'code>,
|
2023-06-16 14:49:58 +03:00
|
|
|
{
|
|
|
|
from_str_with_config(src, |_| /* no extra config */ ())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate the Nix code in `src`, with extra configuration for the
|
|
|
|
/// `tvix_eval::Evaluation` provided by the given closure.
|
|
|
|
pub fn from_str_with_config<'code, T, F>(src: &'code str, config: F) -> Result<T, Error>
|
|
|
|
where
|
|
|
|
T: serde::Deserialize<'code>,
|
2024-01-16 14:19:16 +02:00
|
|
|
F: FnOnce(&mut Evaluation<Box<dyn EvalIO>>),
|
2022-12-31 18:13:59 +03:00
|
|
|
{
|
|
|
|
// First step is to evaluate the Nix code ...
|
2024-01-16 15:35:44 +02:00
|
|
|
let mut eval = Evaluation::new_pure();
|
2023-06-16 14:49:58 +03:00
|
|
|
config(&mut eval);
|
|
|
|
|
2023-03-18 00:10:29 +03:00
|
|
|
eval.strict = true;
|
2023-12-30 21:36:48 +01:00
|
|
|
let result = eval.evaluate(src, None);
|
2022-12-31 18:13:59 +03:00
|
|
|
|
|
|
|
if !result.errors.is_empty() {
|
|
|
|
return Err(Error::NixErrors {
|
|
|
|
errors: result.errors,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-01 15:32:14 +03:00
|
|
|
let de = NixDeserializer::new(result.value.expect("value should be present on success"));
|
2022-12-31 18:13:59 +03:00
|
|
|
|
|
|
|
T::deserialize(de)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unexpected(expected: &'static str, got: &Value) -> Error {
|
|
|
|
Error::UnexpectedType {
|
|
|
|
expected,
|
|
|
|
got: got.type_of(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_integer<I: TryFrom<i64>>(v: &Value) -> Result<I, Error> {
|
|
|
|
match v {
|
|
|
|
Value::Integer(i) => I::try_from(*i).map_err(|_| Error::IntegerConversion {
|
|
|
|
got: *i,
|
|
|
|
need: std::any::type_name::<I>(),
|
|
|
|
}),
|
|
|
|
|
|
|
|
_ => Err(unexpected("integer", v)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-01 15:32:14 +03:00
|
|
|
impl<'de> de::Deserializer<'de> for NixDeserializer {
|
2022-12-31 18:13:59 +03:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
match self.value {
|
|
|
|
Value::Null => visitor.visit_unit(),
|
|
|
|
Value::Bool(b) => visitor.visit_bool(b),
|
|
|
|
Value::Integer(i) => visitor.visit_i64(i),
|
|
|
|
Value::Float(f) => visitor.visit_f64(f),
|
|
|
|
Value::String(s) => visitor.visit_string(s.to_string()),
|
|
|
|
Value::Path(p) => visitor.visit_string(p.to_string_lossy().into()), // TODO: hmm
|
|
|
|
Value::Attrs(_) => self.deserialize_map(visitor),
|
|
|
|
Value::List(_) => self.deserialize_seq(visitor),
|
|
|
|
|
|
|
|
// tvix-eval types that can not be deserialized through serde.
|
|
|
|
Value::Closure(_)
|
|
|
|
| Value::Builtin(_)
|
|
|
|
| Value::Thunk(_)
|
|
|
|
| Value::AttrNotFound
|
|
|
|
| Value::Blueprint(_)
|
|
|
|
| Value::DeferredUpvalue(_)
|
2023-03-04 02:12:06 +03:00
|
|
|
| Value::UnresolvedPath(_)
|
2024-03-25 03:36:32 +01:00
|
|
|
| Value::Json(..)
|
2023-09-09 22:02:56 -07:00
|
|
|
| Value::Catchable(_)
|
fix(tvix/eval): only finalise formal arguments if defaulting
When dealing with a formal argument in a function argument pattern that
has a default expression, there are two different things that can happen
at runtime: Either we select its value from the passed attribute
successfully or we need to use the default expression. Both of these may
be thunks and both of these may need finalisers. However, in the former
case this is taken care of elsewhere, the value will always be finalised
already if necessary. In the latter case we may need to finalise the
thunk resulting from the default expression. However, the thunk
corresponding to the expression may never end up in the local's stack
slot. Since finalisation goes by stack slot (and not constants), we need
to prevent a case where we don't fall back to the default expression,
but finalise anyways.
Previously, we worked around this by making `OpFinalise` ignore
non-thunks. Since finalisation of already evaluated thunks still
crashed, the faulty compilation of function pattern arguments could
still cause a crash.
As a new approach, we reinstate the old behavior of `OpFinalise` to
crash whenever encountering something that is either not a thunk or
doesn't need finalisation. This can also help catching (similar)
miscompilations in the future. To then prevent the crash, we need to
track whether we have fallen back or not at runtime. This is done using
an additional phantom on the stack that holds a new `FinaliseRequest`
value. When it comes to finalisation we check this value and
conditionally execute `OpFinalise` based on its value.
Resolves b/261 and b/265 (partially).
Change-Id: Ic04fb80ec671a2ba11fa645090769c335fb7f58b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8705
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Autosubmit: sterni <sternenseemann@systemli.org>
2023-06-03 02:10:31 +02:00
|
|
|
| Value::FinaliseRequest(_) => Err(Error::Unserializable {
|
2022-12-31 18:13:59 +03:00
|
|
|
value_type: self.value.type_of(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
match self.value {
|
|
|
|
Value::Bool(b) => visitor.visit_bool(b),
|
|
|
|
_ => Err(unexpected("bool", &self.value)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_i8(visit_integer(&self.value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_i16(visit_integer(&self.value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_i32(visit_integer(&self.value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_i64(visit_integer(&self.value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_u8(visit_integer(&self.value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_u16(visit_integer(&self.value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_u32(visit_integer(&self.value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_u64(visit_integer(&self.value)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if let Value::Float(f) = self.value {
|
|
|
|
return visitor.visit_f32(f as f32);
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(unexpected("float", &self.value))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if let Value::Float(f) = self.value {
|
|
|
|
return visitor.visit_f64(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(unexpected("float", &self.value))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if let Value::String(s) = &self.value {
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 17:25:52 -05:00
|
|
|
let chars = s.chars().collect::<Vec<_>>();
|
2022-12-31 18:13:59 +03:00
|
|
|
if chars.len() == 1 {
|
|
|
|
return visitor.visit_char(chars[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(unexpected("char", &self.value))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if let Value::String(s) = &self.value {
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 17:25:52 -05:00
|
|
|
if let Ok(s) = s.to_str() {
|
|
|
|
return visitor.visit_str(s);
|
|
|
|
}
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Err(unexpected("string", &self.value))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if let Value::String(s) = &self.value {
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 17:25:52 -05:00
|
|
|
if let Ok(s) = s.to_str() {
|
|
|
|
return visitor.visit_str(s);
|
|
|
|
}
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Err(unexpected("string", &self.value))
|
|
|
|
}
|
|
|
|
|
2023-01-02 13:39:42 +03:00
|
|
|
fn deserialize_bytes<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
2022-12-31 18:13:59 +03:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 21:39:12 +03:00
|
|
|
unimplemented!()
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:39:42 +03:00
|
|
|
fn deserialize_byte_buf<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
|
2022-12-31 18:13:59 +03:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 21:39:12 +03:00
|
|
|
unimplemented!()
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
2023-01-01 15:32:14 +03:00
|
|
|
// Note that this can not distinguish between a serialisation of
|
|
|
|
// `Some(())` and `None`.
|
2022-12-31 18:13:59 +03:00
|
|
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 15:32:14 +03:00
|
|
|
if let Value::Null = self.value {
|
|
|
|
visitor.visit_none()
|
|
|
|
} else {
|
|
|
|
visitor.visit_some(self)
|
|
|
|
}
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
if let Value::Null = self.value {
|
|
|
|
return visitor.visit_unit();
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(unexpected("null", &self.value))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_unit_struct<V>(
|
|
|
|
self,
|
2023-01-01 21:39:12 +03:00
|
|
|
_name: &'static str,
|
2022-12-31 18:13:59 +03:00
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 21:39:12 +03:00
|
|
|
self.deserialize_unit(visitor)
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_newtype_struct<V>(
|
|
|
|
self,
|
2023-01-01 21:39:12 +03:00
|
|
|
_name: &'static str,
|
2022-12-31 18:13:59 +03:00
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 21:39:12 +03:00
|
|
|
visitor.visit_newtype_struct(self)
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 15:32:14 +03:00
|
|
|
if let Value::List(list) = self.value {
|
2023-08-19 16:38:31 +02:00
|
|
|
let mut seq = SeqDeserializer::new(list.into_iter().map(NixDeserializer::new));
|
2023-01-01 15:32:14 +03:00
|
|
|
let result = visitor.visit_seq(&mut seq)?;
|
|
|
|
seq.end()?;
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(unexpected("list", &self.value))
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
2023-01-01 21:39:12 +03:00
|
|
|
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
2022-12-31 18:13:59 +03:00
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 21:39:12 +03:00
|
|
|
// just represent tuples as lists ...
|
|
|
|
self.deserialize_seq(visitor)
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_tuple_struct<V>(
|
|
|
|
self,
|
2023-01-01 21:39:12 +03:00
|
|
|
_name: &'static str,
|
2023-01-02 13:39:42 +03:00
|
|
|
_len: usize,
|
2022-12-31 18:13:59 +03:00
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 21:39:12 +03:00
|
|
|
// same as above
|
|
|
|
self.deserialize_seq(visitor)
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 15:32:14 +03:00
|
|
|
if let Value::Attrs(attrs) = self.value {
|
|
|
|
let mut map = MapDeserializer::new(attrs.into_iter().map(|(k, v)| {
|
|
|
|
(
|
2024-02-01 12:28:29 -05:00
|
|
|
NixDeserializer::new(Value::from(k)),
|
2023-01-01 15:32:14 +03:00
|
|
|
NixDeserializer::new(v),
|
|
|
|
)
|
|
|
|
}));
|
|
|
|
let result = visitor.visit_map(&mut map)?;
|
|
|
|
map.end()?;
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(unexpected("map", &self.value))
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_struct<V>(
|
|
|
|
self,
|
2023-01-01 15:32:14 +03:00
|
|
|
_name: &'static str,
|
|
|
|
_fields: &'static [&'static str],
|
2022-12-31 18:13:59 +03:00
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 15:32:14 +03:00
|
|
|
self.deserialize_map(visitor)
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:39:42 +03:00
|
|
|
// This method is responsible for deserializing the externally
|
|
|
|
// tagged enum variant serialisation.
|
2022-12-31 18:13:59 +03:00
|
|
|
fn deserialize_enum<V>(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
2023-01-02 13:39:42 +03:00
|
|
|
_variants: &'static [&'static str],
|
2022-12-31 18:13:59 +03:00
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-02 13:39:42 +03:00
|
|
|
match self.value {
|
|
|
|
// a string represents a unit variant
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 17:25:52 -05:00
|
|
|
Value::String(ref s) => {
|
|
|
|
if let Ok(s) = s.to_str() {
|
|
|
|
visitor.visit_enum(de::value::StrDeserializer::new(s))
|
|
|
|
} else {
|
|
|
|
Err(unexpected(name, &self.value))
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 13:39:42 +03:00
|
|
|
|
|
|
|
// an attribute set however represents an externally
|
|
|
|
// tagged enum with content
|
|
|
|
Value::Attrs(attrs) => visitor.visit_enum(Enum(*attrs)),
|
|
|
|
|
|
|
|
_ => Err(unexpected(name, &self.value)),
|
|
|
|
}
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
2023-01-01 15:32:14 +03:00
|
|
|
self.deserialize_str(visitor)
|
2022-12-31 18:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
visitor.visit_unit()
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 13:39:42 +03:00
|
|
|
|
|
|
|
struct Enum(tvix_eval::NixAttrs);
|
|
|
|
|
|
|
|
impl<'de> EnumAccess<'de> for Enum {
|
|
|
|
type Error = Error;
|
|
|
|
type Variant = NixDeserializer;
|
|
|
|
|
|
|
|
// TODO: pass the known variants down here and check against them
|
|
|
|
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
|
|
|
|
where
|
|
|
|
V: de::DeserializeSeed<'de>,
|
|
|
|
{
|
|
|
|
if self.0.len() != 1 {
|
|
|
|
return Err(Error::AmbiguousEnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (key, value) = self.0.into_iter().next().expect("length asserted above");
|
fix(tvix): Represent strings as byte arrays
C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.
To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.
Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.
To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.
Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
2023-12-05 17:25:52 -05:00
|
|
|
if let Ok(k) = key.to_str() {
|
|
|
|
let val = seed.deserialize(de::value::StrDeserializer::<Error>::new(k))?;
|
|
|
|
Ok((val, NixDeserializer::new(value)))
|
|
|
|
} else {
|
|
|
|
Err(unexpected("string", &key.clone().into()))
|
|
|
|
}
|
2023-01-02 13:39:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> VariantAccess<'de> for NixDeserializer {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn unit_variant(self) -> Result<(), Self::Error> {
|
|
|
|
// If this case is hit, a user specified the name of a unit
|
|
|
|
// enum variant but gave it content. Unit enum deserialisation
|
|
|
|
// is handled in `deserialize_enum` above.
|
|
|
|
Err(Error::UnitEnumContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
|
|
|
|
where
|
|
|
|
T: de::DeserializeSeed<'de>,
|
|
|
|
{
|
|
|
|
seed.deserialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
de::Deserializer::deserialize_seq(self, visitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn struct_variant<V>(
|
|
|
|
self,
|
|
|
|
_fields: &'static [&'static str],
|
|
|
|
visitor: V,
|
|
|
|
) -> Result<V::Value, Self::Error>
|
|
|
|
where
|
|
|
|
V: de::Visitor<'de>,
|
|
|
|
{
|
|
|
|
de::Deserializer::deserialize_map(self, visitor)
|
|
|
|
}
|
|
|
|
}
|