@@ -17,9 +17,9 @@ use simplelog::*;
17
17
use usb_gadget:: uevent_listener;
18
18
use usb_gadget:: UsbGadgetState ;
19
19
20
- use serde:: de:: { self , Deserializer , Visitor } ;
20
+ use serde:: de:: { self , Deserializer , Error as DeError , Visitor } ;
21
21
use serde:: Deserialize ;
22
- use std:: fmt;
22
+ use std:: fmt:: { self , Display } ;
23
23
use std:: fs:: OpenOptions ;
24
24
use std:: path:: PathBuf ;
25
25
use std:: str:: FromStr ;
@@ -112,6 +112,20 @@ struct Args {
112
112
config : PathBuf ,
113
113
}
114
114
115
+ pub fn empty_string_as_none < ' de , T , D > ( deserializer : D ) -> Result < Option < T > , D :: Error >
116
+ where
117
+ T : FromStr ,
118
+ T :: Err : Display ,
119
+ D : Deserializer < ' de > ,
120
+ {
121
+ let s: String = Deserialize :: deserialize ( deserializer) ?;
122
+ if s. trim ( ) . is_empty ( ) {
123
+ Ok ( None )
124
+ } else {
125
+ T :: from_str ( & s) . map ( Some ) . map_err ( DeError :: custom)
126
+ }
127
+ }
128
+
115
129
#[ derive( Debug , Clone , Deserialize ) ]
116
130
#[ serde( default ) ]
117
131
pub struct AppConfig {
@@ -120,12 +134,15 @@ pub struct AppConfig {
120
134
hexdump_level : HexdumpLevel ,
121
135
disable_console_debug : bool ,
122
136
legacy : bool ,
137
+ #[ serde( default , deserialize_with = "empty_string_as_none" ) ]
123
138
connect : Option < Address > ,
124
139
logfile : PathBuf ,
125
140
stats_interval : u16 ,
141
+ #[ serde( default , deserialize_with = "empty_string_as_none" ) ]
126
142
udc : Option < String > ,
127
143
iface : String ,
128
144
hostapd_conf : PathBuf ,
145
+ #[ serde( default , deserialize_with = "empty_string_as_none" ) ]
129
146
btalias : Option < String > ,
130
147
keepalive : bool ,
131
148
timeout_secs : u16 ,
@@ -137,9 +154,11 @@ pub struct AppConfig {
137
154
disable_media_sink : bool ,
138
155
disable_tts_sink : bool ,
139
156
developer_mode : bool ,
157
+ #[ serde( default , deserialize_with = "empty_string_as_none" ) ]
140
158
wired : Option < UsbId > ,
141
159
dhu : bool ,
142
160
ev : bool ,
161
+ #[ serde( default , deserialize_with = "empty_string_as_none" ) ]
143
162
ev_battery_logger : Option < PathBuf > ,
144
163
ev_battery_capacity : u64 ,
145
164
ev_factor : f32 ,
0 commit comments