@@ -3,6 +3,7 @@ use std::error::Error;
33use crate :: error:: { ConfigError , Unexpected } ;
44use crate :: map:: Map ;
55use 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)
9568pub 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