2023-01-01 15:32:32 +03:00
|
|
|
use serde::Deserialize;
|
|
|
|
use std::collections::HashMap;
|
2023-06-22 17:57:50 +02:00
|
|
|
use tvix_eval::builtin_macros::builtins;
|
2023-01-01 15:32:32 +03:00
|
|
|
|
2023-06-16 15:07:29 +03:00
|
|
|
use crate::de::{from_str, from_str_with_config};
|
2023-01-01 15:32:32 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_none() {
|
|
|
|
let result: Option<usize> = from_str("null").expect("should deserialize");
|
|
|
|
assert_eq!(None, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_some() {
|
|
|
|
let result: Option<usize> = from_str("40 + 2").expect("should deserialize");
|
|
|
|
assert_eq!(Some(42), result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_string() {
|
|
|
|
let result: String = from_str(
|
|
|
|
r#"
|
|
|
|
let greeter = name: "Hello ${name}!";
|
|
|
|
in greeter "Slartibartfast"
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.expect("should deserialize");
|
|
|
|
|
|
|
|
assert_eq!(result, "Hello Slartibartfast!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_empty_list() {
|
|
|
|
let result: Vec<usize> = from_str("[ ]").expect("should deserialize");
|
|
|
|
assert!(result.is_empty())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_integer_list() {
|
|
|
|
let result: Vec<usize> =
|
|
|
|
from_str("builtins.map (n: n + 2) [ 21 40 67 ]").expect("should deserialize");
|
|
|
|
assert_eq!(result, vec![23, 42, 69]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_empty_map() {
|
|
|
|
let result: HashMap<String, usize> = from_str("{ }").expect("should deserialize");
|
|
|
|
assert!(result.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_integer_map() {
|
|
|
|
let result: HashMap<String, usize> = from_str("{ age = 40 + 2; }").expect("should deserialize");
|
|
|
|
assert_eq!(result.len(), 1);
|
|
|
|
assert_eq!(*result.get("age").unwrap(), 42);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_struct() {
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
struct Person {
|
|
|
|
name: String,
|
|
|
|
age: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
let result: Person = from_str(
|
|
|
|
r#"
|
|
|
|
{
|
|
|
|
name = "Slartibartfast";
|
|
|
|
age = 42;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.expect("should deserialize");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Person {
|
|
|
|
name: "Slartibartfast".into(),
|
|
|
|
age: 42,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-01-01 21:39:12 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_newtype() {
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
struct Number(usize);
|
|
|
|
|
|
|
|
let result: Number = from_str("42").expect("should deserialize");
|
|
|
|
assert_eq!(result, Number(42));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_tuple() {
|
|
|
|
let result: (String, usize) = from_str(r#" [ "foo" 42 ] "#).expect("should deserialize");
|
|
|
|
assert_eq!(result, ("foo".into(), 42));
|
|
|
|
}
|
2023-01-02 13:39:42 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_unit_enum() {
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
enum Foo {
|
|
|
|
Bar,
|
|
|
|
Baz,
|
|
|
|
}
|
|
|
|
|
|
|
|
let result: Foo = from_str("\"Baz\"").expect("should deserialize");
|
|
|
|
assert_eq!(result, Foo::Baz);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_tuple_enum() {
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
enum Foo {
|
|
|
|
Bar,
|
|
|
|
Baz(String, usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
let result: Foo = from_str(
|
|
|
|
r#"
|
|
|
|
{
|
|
|
|
Baz = [ "Slartibartfast" 42 ];
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.expect("should deserialize");
|
|
|
|
|
|
|
|
assert_eq!(result, Foo::Baz("Slartibartfast".into(), 42));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_struct_enum() {
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
enum Foo {
|
|
|
|
Bar,
|
|
|
|
Baz { name: String, age: usize },
|
|
|
|
}
|
|
|
|
|
|
|
|
let result: Foo = from_str(
|
|
|
|
r#"
|
|
|
|
{
|
|
|
|
Baz.name = "Slartibartfast";
|
|
|
|
Baz.age = 42;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.expect("should deserialize");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Foo::Baz {
|
|
|
|
name: "Slartibartfast".into(),
|
|
|
|
age: 42
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_enum_all() {
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
enum TestEnum {
|
2023-08-19 16:38:31 +02:00
|
|
|
Unit,
|
|
|
|
Tuple(String, String),
|
|
|
|
Struct { name: String, age: usize },
|
2023-01-02 13:39:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let result: Vec<TestEnum> = from_str(
|
|
|
|
r#"
|
|
|
|
let
|
2023-08-19 16:38:31 +02:00
|
|
|
mkTuple = country: drink: { tuple = [ country drink ]; };
|
2023-01-02 13:39:42 +03:00
|
|
|
in
|
|
|
|
[
|
|
|
|
(mkTuple "UK" "cask ale")
|
|
|
|
|
2023-08-19 16:38:31 +02:00
|
|
|
"unit"
|
2023-01-02 13:39:42 +03:00
|
|
|
|
|
|
|
{
|
2023-08-19 16:38:31 +02:00
|
|
|
struct.name = "Slartibartfast";
|
|
|
|
struct.age = 42;
|
2023-01-02 13:39:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(mkTuple "Russia" "квас")
|
|
|
|
]
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.expect("should deserialize");
|
|
|
|
|
|
|
|
let expected = vec![
|
2023-08-19 16:38:31 +02:00
|
|
|
TestEnum::Tuple("UK".into(), "cask ale".into()),
|
|
|
|
TestEnum::Unit,
|
|
|
|
TestEnum::Struct {
|
2023-01-02 13:39:42 +03:00
|
|
|
name: "Slartibartfast".into(),
|
|
|
|
age: 42,
|
|
|
|
},
|
2023-08-19 16:38:31 +02:00
|
|
|
TestEnum::Tuple("Russia".into(), "квас".into()),
|
2023-01-02 13:39:42 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
assert_eq!(result, expected);
|
|
|
|
}
|
2023-06-16 15:07:29 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_with_config() {
|
|
|
|
let result: String = from_str_with_config("builtins.testWithConfig", |eval| {
|
|
|
|
// Add a literal string builtin that just returns `"ok"`.
|
|
|
|
eval.src_builtins.push(("testWithConfig", "\"ok\""));
|
|
|
|
})
|
|
|
|
.expect("should deserialize");
|
|
|
|
|
|
|
|
assert_eq!(result, "ok");
|
|
|
|
}
|
2023-06-22 17:57:50 +02:00
|
|
|
|
|
|
|
#[builtins]
|
|
|
|
mod test_builtins {
|
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-06-22 22:24:40 +02:00
|
|
|
use tvix_eval::generators::{Gen, GenCo};
|
2023-06-22 17:57:50 +02:00
|
|
|
use tvix_eval::{ErrorKind, NixString, Value};
|
|
|
|
|
|
|
|
#[builtin("prependHello")]
|
|
|
|
pub async fn builtin_prepend_hello(co: GenCo, x: Value) -> Result<Value, ErrorKind> {
|
|
|
|
match x {
|
|
|
|
Value::String(s) => {
|
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 new_string = NixString::from(format!("hello {}", s.to_str().unwrap()));
|
2024-02-01 12:28:29 -05:00
|
|
|
Ok(Value::from(new_string))
|
2023-06-22 17:57:50 +02:00
|
|
|
}
|
|
|
|
_ => Err(ErrorKind::TypeError {
|
|
|
|
expected: "string",
|
|
|
|
actual: "not string",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_with_extra_builtin() {
|
|
|
|
let code = "builtins.prependHello \"world\"";
|
|
|
|
|
|
|
|
let result: String = from_str_with_config(code, |eval| {
|
|
|
|
eval.builtins.append(&mut test_builtins::builtins());
|
|
|
|
})
|
|
|
|
.expect("should deserialize");
|
|
|
|
|
|
|
|
assert_eq!(result, "hello world");
|
|
|
|
}
|