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 <sternenseemann@systemli.org>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
This commit is contained in:
sterni 2022-09-13 20:11:07 +02:00 committed by clbot
parent e834a2cbc4
commit 067f2b16f6
4 changed files with 48 additions and 1 deletions

View file

@ -1,5 +1,6 @@
use crate::value::CoercionKind;
use std::fmt::Display;
use std::path::PathBuf;
use codemap::{CodeMap, Span};
use codemap_diagnostic::{Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
@ -73,6 +74,9 @@ pub enum ErrorKind {
kind: CoercionKind,
},
/// The given string doesn't represent an absolute path
NotAnAbsolutePath(PathBuf),
/// Tvix internal warning for features triggered by users that are
/// not actually implemented yet, and without which eval can not
/// proceed.
@ -189,6 +193,13 @@ to a missing value in the attribute set(s) included via `with`."#,
format!("cannot ({kindly}) coerce {from} to a string{hint}")
}
ErrorKind::NotAnAbsolutePath(given) => {
format!(
"string {} doesn't represent an absolute path",
given.to_string_lossy()
)
}
ErrorKind::NotImplemented(feature) => {
format!("feature not yet implemented in Tvix: {}", feature)
}
@ -218,6 +229,7 @@ to a missing value in the attribute set(s) included via `with`."#,
ErrorKind::ThunkForce(_) => "E017",
ErrorKind::NotCoercibleToString { .. } => "E018",
ErrorKind::IndexOutOfBounds { .. } => "E019",
ErrorKind::NotAnAbsolutePath(_) => "E020",
ErrorKind::NotImplemented(_) => "E999",
}
}