From dd157976b20e434585268af06bd30e75f1b9ffb9 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:14:26 +1300 Subject: [PATCH] chore: Have `ParsedValue` deserialize to `Nil` as fallback If no other variant could be deserialized into successfully, the type is not supported and treated as the `Nil` type. This better communicates failures within tests when a type is compared and is not expected to be `Nil`. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- Cargo.toml | 1 + src/format.rs | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8b08f5be..0277d2ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ async = ["async-trait"] [dependencies] lazy_static = "1.0" serde = "1.0.8" +serde_with = "3" nom = "7" async-trait = { version = "0.1.50", optional = true } diff --git a/src/format.rs b/src/format.rs index 63cd7cf1..a691ec92 100644 --- a/src/format.rs +++ b/src/format.rs @@ -4,6 +4,7 @@ use crate::error::{ConfigError, Unexpected}; use crate::map::Map; use crate::value::{Value, ValueKind}; use serde::Deserialize; +use serde_with::rust::deserialize_ignore_any; /// Describes a format of configuration source data /// @@ -47,11 +48,12 @@ pub fn extract_root_table( } // Equivalent to ValueKind, except Table + Array store the same enum -// Useful for serde to serialize values into, then convert to Value +// Useful for serde to serialize values into, then convert to Value. +// NOTE: Order of variants is important. Serde will use whichever +// the input successfully deserializes into first. #[derive(serde::Deserialize, Debug)] #[serde(untagged)] pub enum ParsedValue { - Nil, Boolean(bool), I64(i64), I128(i128), @@ -62,6 +64,9 @@ pub enum ParsedValue { String(String), Table(Map), Array(Vec), + // If nothing else above matched, use Nil: + #[serde(deserialize_with = "deserialize_ignore_any")] + Nil, } // Value wrap ValueKind values, with optional uri (origin)