diff options
author | Griffin Smith <root@gws.fyi> | 2022-10-10T04·32-0400 |
---|---|---|
committer | grfn <grfn@gws.fyi> | 2022-10-15T20·35+0000 |
commit | 5eb89be68246f1e5a8cd28e48d5cec75921ca97a (patch) | |
tree | 73a8d48a4c04e2b41ef100b18560c438e9a0832c /tvix/eval/src/errors.rs | |
parent | 277c69cbe5aac853b26d6173e07262f8cc7aff12 (diff) |
feat(tvix/eval): Implement builtins.fromJSON r/5135
Using `serde_json` for parsing JSON here, plus an `impl FromJSON for Value`. The latter is primarily to stay "dependency light" for now - likely going with an actual serde `Deserialize` impl in the future is going to be way better as it allows saving significantly on intermediary allocations. Change-Id: I152a0448ff7c87cf7ebaac927c38912b99de1c18 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6920 Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/eval/src/errors.rs')
-rw-r--r-- | tvix/eval/src/errors.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs index 1c0d71f6188f..33b12daa5d82 100644 --- a/tvix/eval/src/errors.rs +++ b/tvix/eval/src/errors.rs @@ -129,6 +129,9 @@ pub enum ErrorKind { error: Rc<io::Error>, }, + /// Errors converting JSON to a value + FromJsonError(String), + /// Tvix internal warning for features triggered by users that are /// not actually implemented yet, and without which eval can not /// proceed. @@ -176,6 +179,13 @@ impl ErrorKind { } } +impl From<serde_json::Error> for ErrorKind { + fn from(err: serde_json::Error) -> Self { + // Can't just put the `serde_json::Error` in the ErrorKind since it doesn't impl `Clone` + Self::FromJsonError(format!("Error parsing JSON: {err}")) + } +} + #[derive(Clone, Debug)] pub struct Error { pub kind: ErrorKind, @@ -343,6 +353,10 @@ to a missing value in the attribute set(s) included via `with`."#, write!(f, "{error}") } + ErrorKind::FromJsonError(msg) => { + write!(f, "Error converting JSON to a Nix value: {msg}") + } + ErrorKind::NotImplemented(feature) => { write!(f, "feature not yet implemented in Tvix: {}", feature) } @@ -621,6 +635,7 @@ impl Error { | ErrorKind::ImportParseError { .. } | ErrorKind::ImportCompilerError { .. } | ErrorKind::IO { .. } + | ErrorKind::FromJsonError(_) | ErrorKind::NotImplemented(_) => return None, }; @@ -659,6 +674,7 @@ impl Error { ErrorKind::ImportParseError { .. } => "E027", ErrorKind::ImportCompilerError { .. } => "E028", ErrorKind::IO { .. } => "E029", + ErrorKind::FromJsonError { .. } => "E030", // Placeholder error while Tvix is under construction. ErrorKind::NotImplemented(_) => "E999", |