feat(tvix/eval): implement serde::Deserialize for Value

Co-Authored-By: Vincent Ambo <tazjin@tvl.su>

Change-Id: Ib6f7d1f4f4faac36b44f5f75cccc57bf912cf606
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7626
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
This commit is contained in:
Ryan Lahfa 2022-12-24 18:18:26 +01:00 committed by tazjin
parent c011a6130c
commit 805219a2fa
10 changed files with 114 additions and 50 deletions

View file

@ -8,6 +8,9 @@ use std::ops::Deref;
use std::path::Path;
use std::{borrow::Cow, fmt::Display, str::Chars};
use serde::de::{Deserializer, Visitor};
use serde::Deserialize;
#[derive(Clone, Debug)]
enum StringRepr {
Smol(SmolStr),
@ -68,6 +71,39 @@ impl Hash for NixString {
}
}
impl<'de> Deserialize<'de> for NixString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct StringVisitor;
impl<'de> Visitor<'de> for StringVisitor {
type Value = NixString;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a valid Nix string")
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.into())
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.into())
}
}
deserializer.deserialize_string(StringVisitor)
}
}
#[cfg(feature = "arbitrary")]
mod arbitrary {
use super::*;