Skip to content

Commit a4f8c86

Browse files
committed
refactor: Simplify ParsedString deserializer
The enum is only intended as a helper for this deserializer into `String` type, it can be bundled inside. Likewise, no need to `impl Display`. Signed-off-by: Brennan Kinney <[email protected]>
1 parent 6f63595 commit a4f8c86

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

src/format.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::error::Error;
33
use crate::error::{ConfigError, Unexpected};
44
use crate::map::Map;
55
use crate::value::{Value, ValueKind};
6+
use serde::Deserialize;
67

78
/// Describes a format of configuration source data
89
///
@@ -63,34 +64,6 @@ pub enum ParsedValue {
6364
Array(Vec<Self>),
6465
}
6566

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-
9467
// Value wrap ValueKind values, with optional uri (origin)
9568
pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value {
9669
let vk = match value {
@@ -123,3 +96,25 @@ pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value {
12396

12497
Value::new(uri, vk)
12598
}
99+
100+
// Deserialization support for TOML `Datetime` value type into `String`
101+
fn deserialize_parsed_string<'de, D>(deserializer: D) -> Result<String, D::Error>
102+
where
103+
D: serde::de::Deserializer<'de>,
104+
{
105+
#[derive(serde::Deserialize)]
106+
#[serde(untagged)]
107+
enum ParsedString {
108+
// Anything that can deserialize into a string successfully:
109+
String(String),
110+
// Config specific support for types that need string conversion:
111+
#[cfg(feature = "toml")]
112+
TomlDateTime(toml::value::Datetime),
113+
}
114+
115+
Ok(match ParsedString::deserialize(deserializer)? {
116+
ParsedString::String(v) => v,
117+
#[cfg(feature = "toml")]
118+
ParsedString::TomlDateTime(v) => v.to_string(),
119+
})
120+
}

0 commit comments

Comments
 (0)