@@ -6,6 +6,23 @@ use serde_json::Value;
6
6
use std:: fmt;
7
7
use std:: ops:: Deref ;
8
8
9
+ // Define SpecFormatMode enum for type-safe formatting
10
+ #[ derive( Debug , Clone , Copy , PartialEq ) ]
11
+ pub enum SpecFormatMode {
12
+ Concise ,
13
+ Verbose ,
14
+ }
15
+
16
+ impl SpecFormatMode {
17
+ pub fn from_str ( s : & str ) -> Result < Self , String > {
18
+ match s. to_lowercase ( ) . as_str ( ) {
19
+ "concise" => Ok ( SpecFormatMode :: Concise ) ,
20
+ "verbose" => Ok ( SpecFormatMode :: Verbose ) ,
21
+ _ => Err ( format ! ( "Invalid format mode: {}" , s) ) ,
22
+ }
23
+ }
24
+ }
25
+
9
26
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
10
27
#[ serde( tag = "kind" ) ]
11
28
pub enum SpecString {
@@ -264,6 +281,13 @@ impl OpSpec {
264
281
. unwrap_or ( "#serde_error" . to_string ( ) ) ;
265
282
format ! ( "{}({})" , self . kind, spec_str)
266
283
}
284
+
285
+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
286
+ match mode {
287
+ SpecFormatMode :: Concise => self . format_concise ( ) ,
288
+ SpecFormatMode :: Verbose => self . format_verbose ( ) ,
289
+ }
290
+ }
267
291
}
268
292
269
293
impl fmt:: Display for OpSpec {
@@ -296,27 +320,18 @@ pub struct ImportOpSpec {
296
320
}
297
321
298
322
impl ImportOpSpec {
299
- fn format ( & self , verbose : bool ) -> String {
300
- let source = if verbose {
301
- self . source . format_verbose ( )
302
- } else {
303
- self . source . format_concise ( )
323
+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
324
+ let source = match mode {
325
+ SpecFormatMode :: Concise => self . source . format_concise ( ) ,
326
+ SpecFormatMode :: Verbose => self . source . format_verbose ( ) ,
304
327
} ;
305
328
format ! ( "source={}, refresh={}" , source, self . refresh_options)
306
329
}
307
-
308
- pub fn format_concise ( & self ) -> String {
309
- self . format ( false )
310
- }
311
-
312
- pub fn format_verbose ( & self ) -> String {
313
- self . format ( true )
314
- }
315
330
}
316
331
317
332
impl fmt:: Display for ImportOpSpec {
318
333
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
319
- write ! ( f, "{}" , self . format_concise ( ) )
334
+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
320
335
}
321
336
}
322
337
@@ -327,39 +342,27 @@ pub struct TransformOpSpec {
327
342
}
328
343
329
344
impl TransformOpSpec {
330
- fn format ( & self , verbose : bool ) -> String {
345
+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
331
346
let inputs = self
332
347
. inputs
333
348
. iter ( )
334
349
. map ( ToString :: to_string)
335
350
. collect :: < Vec < _ > > ( )
336
351
. join ( "," ) ;
337
-
338
- let op_str = if verbose {
339
- self . op . format_verbose ( )
340
- } else {
341
- self . op . format_concise ( )
352
+ let op_str = match mode {
353
+ SpecFormatMode :: Concise => self . op . format_concise ( ) ,
354
+ SpecFormatMode :: Verbose => self . op . format_verbose ( ) ,
342
355
} ;
343
-
344
- if verbose {
345
- format ! ( "op={}, inputs=[{}]" , op_str, inputs)
346
- } else {
347
- format ! ( "op={}, inputs={}" , op_str, inputs)
356
+ match mode {
357
+ SpecFormatMode :: Concise => format ! ( "op={}, inputs={}" , op_str, inputs) ,
358
+ SpecFormatMode :: Verbose => format ! ( "op={}, inputs=[{}]" , op_str, inputs) ,
348
359
}
349
360
}
350
-
351
- pub fn format_concise ( & self ) -> String {
352
- self . format ( false )
353
- }
354
-
355
- pub fn format_verbose ( & self ) -> String {
356
- self . format ( true )
357
- }
358
361
}
359
362
360
363
impl fmt:: Display for TransformOpSpec {
361
364
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
362
- write ! ( f, "{}" , self . format_concise ( ) )
365
+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
363
366
}
364
367
}
365
368
@@ -374,6 +377,13 @@ impl ForEachOpSpec {
374
377
pub fn get_label ( & self ) -> String {
375
378
format ! ( "Loop over {}" , self . field_path)
376
379
}
380
+
381
+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
382
+ match mode {
383
+ SpecFormatMode :: Concise => self . get_label ( ) ,
384
+ SpecFormatMode :: Verbose => format ! ( "field={}" , self . field_path) ,
385
+ }
386
+ }
377
387
}
378
388
379
389
impl fmt:: Display for ForEachOpSpec {
@@ -396,34 +406,28 @@ pub struct CollectOpSpec {
396
406
}
397
407
398
408
impl CollectOpSpec {
399
- fn format ( & self , verbose : bool ) -> String {
409
+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
400
410
let uuid = self . auto_uuid_field . as_deref ( ) . unwrap_or ( "none" ) ;
401
-
402
- if verbose {
403
- format ! (
404
- "scope={}, collector={}, input=[{}], uuid={}" ,
405
- self . scope_name, self . collector_name, self . input, uuid
406
- )
407
- } else {
408
- format ! (
409
- "collector={}, input={}, uuid={}" ,
410
- self . collector_name, self . input, uuid
411
- )
411
+ match mode {
412
+ SpecFormatMode :: Concise => {
413
+ format ! (
414
+ "collector={}, input={}, uuid={}" ,
415
+ self . collector_name, self . input, uuid
416
+ )
417
+ }
418
+ SpecFormatMode :: Verbose => {
419
+ format ! (
420
+ "scope={}, collector={}, input=[{}], uuid={}" ,
421
+ self . scope_name, self . collector_name, self . input, uuid
422
+ )
423
+ }
412
424
}
413
425
}
414
-
415
- pub fn format_concise ( & self ) -> String {
416
- self . format ( false )
417
- }
418
-
419
- pub fn format_verbose ( & self ) -> String {
420
- self . format ( true )
421
- }
422
426
}
423
427
424
428
impl fmt:: Display for CollectOpSpec {
425
429
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
426
- write ! ( f, "{}" , self . format_concise ( ) )
430
+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
427
431
}
428
432
}
429
433
@@ -490,37 +494,25 @@ pub struct ExportOpSpec {
490
494
}
491
495
492
496
impl ExportOpSpec {
493
- fn format ( & self , verbose : bool ) -> String {
494
- let target_str = if verbose {
495
- self . target . format_verbose ( )
496
- } else {
497
- self . target . format_concise ( )
497
+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
498
+ let target_str = match mode {
499
+ SpecFormatMode :: Concise => self . target . format_concise ( ) ,
500
+ SpecFormatMode :: Verbose => self . target . format_verbose ( ) ,
498
501
} ;
499
-
500
502
let base = format ! (
501
503
"collector={}, target={}, {}" ,
502
504
self . collector_name, target_str, self . index_options
503
505
) ;
504
-
505
- if verbose {
506
- format ! ( "{}, setup_by_user={}" , base, self . setup_by_user)
507
- } else {
508
- base
506
+ match mode {
507
+ SpecFormatMode :: Concise => base,
508
+ SpecFormatMode :: Verbose => format ! ( "{}, setup_by_user={}" , base, self . setup_by_user) ,
509
509
}
510
510
}
511
-
512
- pub fn format_concise ( & self ) -> String {
513
- self . format ( false )
514
- }
515
-
516
- pub fn format_verbose ( & self ) -> String {
517
- self . format ( true )
518
- }
519
511
}
520
512
521
513
impl fmt:: Display for ExportOpSpec {
522
514
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
523
- write ! ( f, "{}" , self . format_concise ( ) )
515
+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
524
516
}
525
517
}
526
518
@@ -533,26 +525,21 @@ pub enum ReactiveOpSpec {
533
525
}
534
526
535
527
impl ReactiveOpSpec {
536
- pub fn format_concise ( & self ) -> String {
528
+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
537
529
match self {
538
- ReactiveOpSpec :: Transform ( t) => format ! ( "Transform: {}" , t. format_concise( ) ) ,
539
- ReactiveOpSpec :: ForEach ( fe) => format ! ( "{}" , fe. get_label( ) ) ,
540
- ReactiveOpSpec :: Collect ( c) => c. format_concise ( ) ,
541
- }
542
- }
543
-
544
- pub fn format_verbose ( & self ) -> String {
545
- match self {
546
- ReactiveOpSpec :: Transform ( t) => format ! ( "Transform: {}" , t. format_verbose( ) ) ,
547
- ReactiveOpSpec :: ForEach ( fe) => format ! ( "ForEach: {}" , fe) ,
548
- ReactiveOpSpec :: Collect ( c) => format ! ( "Collect: {}" , c. format_verbose( ) ) ,
530
+ ReactiveOpSpec :: Transform ( t) => format ! ( "Transform: {}" , t. format( mode) ) ,
531
+ ReactiveOpSpec :: ForEach ( fe) => match mode {
532
+ SpecFormatMode :: Concise => format ! ( "{}" , fe. get_label( ) ) ,
533
+ SpecFormatMode :: Verbose => format ! ( "ForEach: {}" , fe. format( mode) ) ,
534
+ } ,
535
+ ReactiveOpSpec :: Collect ( c) => format ! ( "Collect: {}" , c. format( mode) ) ,
549
536
}
550
537
}
551
538
}
552
539
553
540
impl fmt:: Display for ReactiveOpSpec {
554
541
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
555
- write ! ( f, "{}" , self . format_concise ( ) )
542
+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
556
543
}
557
544
}
558
545
0 commit comments