From 067f2b16f6f5d8fce73b8420a53b51a15c90f589 Mon Sep 17 00:00:00 2001 From: sterni Date: Tue, 13 Sep 2022 20:11:07 +0200 Subject: feat(tvix/eval): implement Value::coerce_to_path() This function is necessary for all builtins that expect some form of path as an argument. It is merely a wrapper around coerce_to_string that can shortcut if we already have a path. The absolute path check is done in the same way as in C++ Nix for compatibility, although it should probably be revised in the long term (think about Windows, for example). Since coercing to a path is not an operation possible in the language directly, this function can live in the builtins module as the only place it is required. Change-Id: I69ed5455c00d193fea88b8fa83e28907a761cab5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6574 Autosubmit: sterni Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/value/string.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'tvix/eval/src/value/string.rs') diff --git a/tvix/eval/src/value/string.rs b/tvix/eval/src/value/string.rs index aa542181f9..058c7f87dd 100644 --- a/tvix/eval/src/value/string.rs +++ b/tvix/eval/src/value/string.rs @@ -2,7 +2,7 @@ //! backing implementations. use smol_str::SmolStr; use std::hash::Hash; -use std::{borrow::Cow, fmt::Display}; +use std::{borrow::Cow, fmt::Display, str::Chars}; #[derive(Clone, Debug)] enum StringRepr { @@ -97,6 +97,13 @@ impl NixString { s.push_str(other.as_str()); NixString(StringRepr::Heap(s)) } + + pub fn chars(&self) -> Chars<'_> { + match &self.0 { + StringRepr::Heap(h) => h.chars(), + StringRepr::Smol(s) => s.chars(), + } + } } fn nix_escape_char(ch: char, next: Option<&char>) -> Option<&'static str> { -- cgit 1.4.1