Skip to content

Commit

Permalink
chore: Have ParsedValue deserialize to Nil as fallback
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
polarathene committed Oct 9, 2023
1 parent 79ce8cb commit dd15797
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
9 changes: 7 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -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),
Expand All @@ -62,6 +64,9 @@ pub enum ParsedValue {
String(String),
Table(Map<String, Self>),
Array(Vec<Self>),
// If nothing else above matched, use Nil:
#[serde(deserialize_with = "deserialize_ignore_any")]
Nil,
}

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

0 comments on commit dd15797

Please sign in to comment.