From 66a35de3b67dd441185b7badaf559c8a25ab9967 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 9 Oct 2022 13:09:11 -0400 Subject: feat(tvix/eval): Implement builtins.concatStringsSep Change-Id: I6e46bcdbf3b5258b60edb017709fee577eb8ec74 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6907 Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 16 ++++++++++++++++ .../src/tests/tvix_tests/eval-okay-concatstringssep.exp | 1 + .../src/tests/tvix_tests/eval-okay-concatstringssep.nix | 8 ++++++++ tvix/eval/src/value/mod.rs | 6 ++++++ tvix/eval/src/value/string.rs | 15 +++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-concatstringssep.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-concatstringssep.nix diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 8929d91cc9..08b8299e48 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -169,6 +169,22 @@ fn pure_builtins() -> Vec { Ok(Value::List(res.into())) }, ), + Builtin::new( + "concatStringsSep", + &[true, true], + |args: Vec, vm: &mut VM| { + let separator = args[0].to_str()?; + let list = args[1].to_list()?; + let mut res = String::new(); + for (i, val) in list.into_iter().enumerate() { + if i != 0 { + res.push_str(&separator); + } + res.push_str(&val.force(vm)?.coerce_to_string(CoercionKind::Weak, vm)?); + } + Ok(res.into()) + }, + ), Builtin::new( "div", &[false, false], diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-concatstringssep.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-concatstringssep.exp new file mode 100644 index 0000000000..93987647ff --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-concatstringssep.exp @@ -0,0 +1 @@ +[ "" "foobarxyzzy" "foo, bar, xyzzy" "foo" "" ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-concatstringssep.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-concatstringssep.nix new file mode 100644 index 0000000000..adc4c41bd5 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-concatstringssep.nix @@ -0,0 +1,8 @@ +with builtins; + +[ (concatStringsSep "" []) + (concatStringsSep "" ["foo" "bar" "xyzzy"]) + (concatStringsSep ", " ["foo" "bar" "xyzzy"]) + (concatStringsSep ", " ["foo"]) + (concatStringsSep ", " []) +] diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs index 0f3b9cbbf4..0e9d013fdf 100644 --- a/tvix/eval/src/value/mod.rs +++ b/tvix/eval/src/value/mod.rs @@ -377,6 +377,12 @@ impl From<&str> for Value { } } +impl From for Value { + fn from(val: String) -> Self { + Self::String(val.into()) + } +} + fn type_error(expected: &'static str, actual: &Value) -> ErrorKind { ErrorKind::TypeError { expected, diff --git a/tvix/eval/src/value/string.rs b/tvix/eval/src/value/string.rs index bfbaa815db..c21f2c4e83 100644 --- a/tvix/eval/src/value/string.rs +++ b/tvix/eval/src/value/string.rs @@ -2,6 +2,7 @@ //! backing implementations. use smol_str::SmolStr; use std::hash::Hash; +use std::ops::Deref; use std::{borrow::Cow, fmt::Display, str::Chars}; #[derive(Clone, Debug)] @@ -178,6 +179,20 @@ impl Display for NixString { } } +impl AsRef for NixString { + fn as_ref(&self) -> &str { + self.as_str() + } +} + +impl Deref for NixString { + type Target = str; + + fn deref(&self) -> &Self::Target { + self.as_str() + } +} + #[cfg(test)] mod tests { use super::*; -- cgit 1.4.1