feat(tvix/eval): Implement builtins.fromJSON

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>
This commit is contained in:
Griffin Smith 2022-10-10 00:32:57 -04:00 committed by grfn
parent 277c69cbe5
commit 5eb89be682
12 changed files with 123 additions and 13 deletions

View file

@ -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",