Skip to content

Commit 6f63595

Browse files
committed
fix: Support TOML Datetime value
- The enum did not properly handle the `Datetime` TOML value which needed to be converted into a `String` type. - This workaround approach avoids duplicating `from_parsed_value()` logic to support one enum variant. Signed-off-by: Brennan Kinney <[email protected]>
1 parent c9575a7 commit 6f63595

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/format.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,40 @@ pub enum ParsedValue {
5757
U64(u64),
5858
U128(u128),
5959
Float(f64),
60+
#[serde(deserialize_with = "deserialize_parsed_string")]
6061
String(String),
6162
Table(Map<String, Self>),
6263
Array(Vec<Self>),
6364
}
6465

66+
// Deserialization support for TOML `Datetime` value type into `String`
67+
#[derive(serde::Deserialize, Debug)]
68+
#[serde(untagged)]
69+
enum ParsedString {
70+
String(String),
71+
#[cfg(feature = "toml")]
72+
DateTime(toml::value::Datetime),
73+
}
74+
75+
fn deserialize_parsed_string<'de, D>(deserializer: D) -> Result<String, D::Error>
76+
where
77+
D: serde::de::Deserializer<'de>,
78+
{
79+
let s: ParsedString = serde::Deserialize::deserialize(deserializer)?;
80+
Ok(s.to_string())
81+
}
82+
83+
impl std::fmt::Display for ParsedString {
84+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
85+
let s = match self {
86+
ParsedString::String(s) => s.to_string(),
87+
#[cfg(feature = "toml")]
88+
ParsedString::DateTime(dt) => dt.to_string()
89+
};
90+
write!(f, "{}", s)
91+
}
92+
}
93+
6594
// Value wrap ValueKind values, with optional uri (origin)
6695
pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value {
6796
let vk = match value {

0 commit comments

Comments
 (0)