Skip to content

Commit dd15797

Browse files
committed
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 <[email protected]>
1 parent 79ce8cb commit dd15797

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ async = ["async-trait"]
2727
[dependencies]
2828
lazy_static = "1.0"
2929
serde = "1.0.8"
30+
serde_with = "3"
3031
nom = "7"
3132

3233
async-trait = { version = "0.1.50", optional = true }

src/format.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::error::{ConfigError, Unexpected};
44
use crate::map::Map;
55
use crate::value::{Value, ValueKind};
66
use serde::Deserialize;
7+
use serde_with::rust::deserialize_ignore_any;
78

89
/// Describes a format of configuration source data
910
///
@@ -47,11 +48,12 @@ pub fn extract_root_table(
4748
}
4849

4950
// Equivalent to ValueKind, except Table + Array store the same enum
50-
// Useful for serde to serialize values into, then convert to Value
51+
// Useful for serde to serialize values into, then convert to Value.
52+
// NOTE: Order of variants is important. Serde will use whichever
53+
// the input successfully deserializes into first.
5154
#[derive(serde::Deserialize, Debug)]
5255
#[serde(untagged)]
5356
pub enum ParsedValue {
54-
Nil,
5557
Boolean(bool),
5658
I64(i64),
5759
I128(i128),
@@ -62,6 +64,9 @@ pub enum ParsedValue {
6264
String(String),
6365
Table(Map<String, Self>),
6466
Array(Vec<Self>),
67+
// If nothing else above matched, use Nil:
68+
#[serde(deserialize_with = "deserialize_ignore_any")]
69+
Nil,
6570
}
6671

6772
// Value wrap ValueKind values, with optional uri (origin)

0 commit comments

Comments
 (0)