@@ -6,12 +6,12 @@ use clap::builder::PossibleValuesParser;
6
6
use clap:: builder:: TypedValueParser as _;
7
7
use clap:: { value_parser, Args , Command , Parser , ValueHint } ;
8
8
use clap_complete:: { generate, Generator , Shell } ;
9
+ use merge:: Merge ;
9
10
use num_format:: CustomFormat ;
10
11
use onefetch_image:: ImageProtocol ;
11
12
use onefetch_manifest:: ManifestType ;
12
13
use regex:: Regex ;
13
- use serde:: Serialize ;
14
- use merge:: Merge ;
14
+ use serde:: { de:: Visitor , Serializer , Deserialize , Serialize } ;
15
15
use std:: env;
16
16
use std:: io;
17
17
use std:: path:: PathBuf ;
@@ -21,17 +21,19 @@ use strum::IntoEnumIterator;
21
21
const COLOR_RESOLUTIONS : [ & str ; 5 ] = [ "16" , "32" , "64" , "128" , "256" ] ;
22
22
pub const NO_BOTS_DEFAULT_REGEX_PATTERN : & str = r"(?:-|\s)[Bb]ot$|\[[Bb]ot\]" ;
23
23
24
- #[ derive( Clone , Debug , Parser , PartialEq , Eq , Merge ) ]
24
+ #[ derive( Clone , Debug , Parser , PartialEq , Eq , Merge , Serialize , Deserialize ) ]
25
25
#[ command( version, about) ]
26
26
pub struct CliOptions {
27
27
/// Run as if onefetch was started in <input> instead of the current working directory
28
28
#[ arg( default_value = "." , hide_default_value = true , value_hint = ValueHint :: DirPath ) ]
29
29
#[ merge( skip) ]
30
+ #[ serde( skip) ]
30
31
pub input : PathBuf ,
31
32
/// Specify a custom path to a config file.
32
33
/// Default config is located at ${HOME}/.config/onefetch/config.conf.
33
34
#[ arg( long, value_hint = ValueHint :: AnyPath ) ]
34
35
#[ merge( skip) ]
36
+ #[ serde( skip) ]
35
37
pub config_path : Option < PathBuf > ,
36
38
#[ command( flatten) ]
37
39
pub info : InfoCliOptions ,
@@ -49,7 +51,7 @@ pub struct CliOptions {
49
51
pub other : OtherCliOptions ,
50
52
}
51
53
52
- #[ derive( Clone , Debug , Args , PartialEq , Eq , Merge ) ]
54
+ #[ derive( Clone , Debug , Args , PartialEq , Eq , Merge , Serialize , Deserialize ) ]
53
55
#[ command( next_help_heading = "INFO" ) ]
54
56
pub struct InfoCliOptions {
55
57
/// Allows you to disable FIELD(s) from appearing in the output
@@ -132,7 +134,7 @@ pub struct InfoCliOptions {
132
134
pub r#type : Vec < LanguageType > ,
133
135
}
134
136
135
- #[ derive( Clone , Debug , Args , PartialEq , Eq , Merge ) ]
137
+ #[ derive( Clone , Debug , Args , PartialEq , Eq , Merge , Serialize , Deserialize ) ]
136
138
#[ command( next_help_heading = "ASCII" ) ]
137
139
pub struct AsciiCliOptions {
138
140
/// Takes a non-empty STRING as input to replace the ASCII logo
@@ -164,6 +166,7 @@ pub struct AsciiCliOptions {
164
166
hide_possible_values = true
165
167
) ]
166
168
#[ merge( skip) ]
169
+ #[ serde( skip) ]
167
170
pub ascii_language : Option < Language > ,
168
171
/// Specify when to use true color
169
172
///
@@ -173,7 +176,7 @@ pub struct AsciiCliOptions {
173
176
pub true_color : When ,
174
177
}
175
178
176
- #[ derive( Clone , Debug , Args , PartialEq , Eq , Merge ) ]
179
+ #[ derive( Clone , Debug , Args , PartialEq , Eq , Merge , Serialize , Deserialize ) ]
177
180
#[ command( next_help_heading = "IMAGE" ) ]
178
181
pub struct ImageCliOptions {
179
182
/// Path to the IMAGE file
@@ -183,6 +186,7 @@ pub struct ImageCliOptions {
183
186
/// Which image PROTOCOL to use
184
187
#[ arg( long, value_enum, requires = "image" , value_name = "PROTOCOL" ) ]
185
188
#[ merge( skip) ]
189
+ #[ serde( skip) ]
186
190
pub image_protocol : Option < ImageProtocol > ,
187
191
/// VALUE of color resolution to use with SIXEL backend
188
192
#[ arg(
@@ -197,7 +201,7 @@ pub struct ImageCliOptions {
197
201
pub color_resolution : usize ,
198
202
}
199
203
200
- #[ derive( Clone , Debug , Args , PartialEq , Eq , Merge ) ]
204
+ #[ derive( Clone , Debug , Args , PartialEq , Eq , Merge , Serialize , Deserialize ) ]
201
205
#[ command( next_help_heading = "TEXT FORMATTING" ) ]
202
206
pub struct TextForamttingCliOptions {
203
207
/// Changes the text colors (X X X...)
@@ -229,7 +233,7 @@ pub struct TextForamttingCliOptions {
229
233
#[ merge( strategy = merge:: bool :: overwrite_false) ]
230
234
pub no_bold : bool ,
231
235
}
232
- #[ derive( Clone , Debug , Args , PartialEq , Eq , Default , Merge ) ]
236
+ #[ derive( Clone , Debug , Args , PartialEq , Eq , Default , Merge , Serialize , Deserialize ) ]
233
237
#[ command( next_help_heading = "VISUALS" ) ]
234
238
pub struct VisualsCliOptions {
235
239
/// Hides the color palette
@@ -248,29 +252,33 @@ pub struct VisualsCliOptions {
248
252
pub nerd_fonts : bool ,
249
253
}
250
254
251
- #[ derive( Clone , Debug , Args , PartialEq , Eq , Default , Merge ) ]
255
+ #[ derive( Clone , Debug , Args , PartialEq , Eq , Default , Merge , Serialize , Deserialize ) ]
252
256
#[ command( next_help_heading = "DEVELOPER" ) ]
253
257
pub struct DeveloperCliOptions {
254
258
/// Outputs Onefetch in a specific format
255
259
#[ arg( long, short, value_name = "FORMAT" , value_enum) ]
256
260
#[ merge( skip) ]
261
+ #[ serde( skip) ]
257
262
pub output : Option < SerializationFormat > ,
258
263
/// If provided, outputs the completion file for given SHELL
259
264
#[ arg( long = "generate" , value_name = "SHELL" , value_enum) ]
260
265
#[ merge( skip) ]
266
+ #[ serde( skip) ]
261
267
pub completion : Option < Shell > ,
262
268
}
263
269
264
- #[ derive( Clone , Debug , Args , PartialEq , Eq , Default , Merge ) ]
270
+ #[ derive( Clone , Debug , Args , PartialEq , Eq , Default , Merge , Serialize , Deserialize ) ]
265
271
#[ command( next_help_heading = "OTHER" ) ]
266
272
pub struct OtherCliOptions {
267
273
/// Prints out supported languages
268
274
#[ arg( long, short) ]
269
275
#[ merge( skip) ]
276
+ #[ serde( skip) ]
270
277
pub languages : bool ,
271
278
/// Prints out supported package managers
272
279
#[ arg( long, short) ]
273
280
#[ merge( skip) ]
281
+ #[ serde( skip) ]
274
282
pub package_managers : bool ,
275
283
}
276
284
@@ -386,14 +394,14 @@ pub fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
386
394
generate ( gen, cmd, cmd. get_name ( ) . to_string ( ) , & mut io:: stdout ( ) ) ;
387
395
}
388
396
389
- #[ derive( clap:: ValueEnum , Clone , PartialEq , Eq , Debug ) ]
397
+ #[ derive( clap:: ValueEnum , Clone , PartialEq , Eq , Debug , Serialize , Deserialize ) ]
390
398
pub enum When {
391
399
Auto ,
392
400
Never ,
393
401
Always ,
394
402
}
395
403
396
- #[ derive( clap:: ValueEnum , Clone , PartialEq , Eq , Debug , Serialize , Copy ) ]
404
+ #[ derive( clap:: ValueEnum , Clone , PartialEq , Eq , Debug , Serialize , Deserialize , Copy ) ]
397
405
pub enum NumberSeparator {
398
406
Plain ,
399
407
Comma ,
@@ -512,3 +520,37 @@ impl FromStr for MyRegex {
512
520
Ok ( MyRegex ( Regex :: new ( s) ?) )
513
521
}
514
522
}
523
+
524
+ impl Serialize for MyRegex {
525
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
526
+ where
527
+ S : Serializer ,
528
+ { serializer. serialize_str ( self . 0 . as_str ( ) ) }
529
+ }
530
+
531
+ pub struct RegVisitor ;
532
+
533
+ impl < ' de > Visitor < ' de > for RegVisitor {
534
+ type Value = MyRegex ;
535
+
536
+ fn expecting ( & self , formatter : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
537
+ formatter. write_str ( "regex" )
538
+ }
539
+
540
+ fn visit_str < E > ( self , v : & str ) -> Result < Self :: Value , E >
541
+ where
542
+ E : serde:: de:: Error , {
543
+ match MyRegex :: from_str ( v) {
544
+ Ok ( regex) => Ok ( regex) ,
545
+ Err ( error) => Err ( serde:: de:: Error :: custom ( error) )
546
+ }
547
+ }
548
+ }
549
+
550
+ impl < ' de > Deserialize < ' de > for MyRegex {
551
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
552
+ where
553
+ D : serde:: Deserializer < ' de > , {
554
+ deserializer. deserialize_str ( RegVisitor )
555
+ }
556
+ }
0 commit comments