@@ -12,6 +12,7 @@ enum Input {
12
12
Yaml ,
13
13
Json ,
14
14
Toml ,
15
+ Kdl ,
15
16
}
16
17
17
18
#[ derive( Copy , Debug , Default , Clone , PartialEq , Eq , PartialOrd , Ord , ValueEnum ) ]
@@ -187,6 +188,28 @@ impl Args {
187
188
Ok ( serde_json:: to_vec ( & doc_as) ?)
188
189
}
189
190
191
+ fn read_kdl ( & mut self ) -> Result < Vec < u8 > > {
192
+ use kdl:: KdlDocument ;
193
+ let mut buf = String :: new ( ) ;
194
+ let kdl_str = if let Some ( f) = & self . file {
195
+ if !std:: path:: Path :: new ( & f) . exists ( ) {
196
+ Self :: try_parse_from ( [ "cmd" , "-h" ] ) ?;
197
+ std:: process:: exit ( 2 ) ;
198
+ }
199
+ std:: fs:: read_to_string ( f) ?
200
+ } else if !stdin ( ) . is_terminal ( ) && !cfg ! ( test) {
201
+ debug ! ( "reading from stdin" ) ;
202
+ stdin ( ) . read_to_string ( & mut buf) ?;
203
+ buf
204
+ } else {
205
+ Self :: try_parse_from ( [ "cmd" , "-h" ] ) ?;
206
+ std:: process:: exit ( 2 ) ;
207
+ } ;
208
+ let doc: KdlDocument = kdl_str. parse ( ) ?;
209
+ let doc_as: serde_json:: Value = doc. try_into ( ) ?;
210
+ Ok ( serde_json:: to_vec ( & doc_as) ?)
211
+ }
212
+
190
213
fn read_json ( & mut self ) -> Result < Vec < u8 > > {
191
214
let json_value: serde_json:: Value = if let Some ( f) = & self . file {
192
215
if !std:: path:: Path :: new ( & f) . exists ( ) {
@@ -209,6 +232,7 @@ impl Args {
209
232
let ser = match self . input {
210
233
Input :: Yaml => self . read_yaml ( ) ?,
211
234
Input :: Toml => self . read_toml ( ) ?,
235
+ Input :: Kdl => self . read_kdl ( ) ?,
212
236
Input :: Json => self . read_json ( ) ?,
213
237
} ;
214
238
debug ! ( "input decoded as json: {}" , String :: from_utf8_lossy( & ser) ) ;
0 commit comments