@@ -6,6 +6,23 @@ use serde_json::Value;
66use std:: fmt;
77use std:: ops:: Deref ;
88
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+
926#[ derive( Debug , Clone , Serialize , Deserialize ) ]
1027#[ serde( tag = "kind" ) ]
1128pub enum SpecString {
@@ -264,6 +281,13 @@ impl OpSpec {
264281 . unwrap_or ( "#serde_error" . to_string ( ) ) ;
265282 format ! ( "{}({})" , self . kind, spec_str)
266283 }
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+ }
267291}
268292
269293impl fmt:: Display for OpSpec {
@@ -296,27 +320,18 @@ pub struct ImportOpSpec {
296320}
297321
298322impl 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 ( ) ,
304327 } ;
305328 format ! ( "source={}, refresh={}" , source, self . refresh_options)
306329 }
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- }
315330}
316331
317332impl fmt:: Display for ImportOpSpec {
318333 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
319- write ! ( f, "{}" , self . format_concise ( ) )
334+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
320335 }
321336}
322337
@@ -327,39 +342,27 @@ pub struct TransformOpSpec {
327342}
328343
329344impl TransformOpSpec {
330- fn format ( & self , verbose : bool ) -> String {
345+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
331346 let inputs = self
332347 . inputs
333348 . iter ( )
334349 . map ( ToString :: to_string)
335350 . collect :: < Vec < _ > > ( )
336351 . 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 ( ) ,
342355 } ;
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) ,
348359 }
349360 }
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- }
358361}
359362
360363impl fmt:: Display for TransformOpSpec {
361364 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
362- write ! ( f, "{}" , self . format_concise ( ) )
365+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
363366 }
364367}
365368
@@ -374,6 +377,13 @@ impl ForEachOpSpec {
374377 pub fn get_label ( & self ) -> String {
375378 format ! ( "Loop over {}" , self . field_path)
376379 }
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+ }
377387}
378388
379389impl fmt:: Display for ForEachOpSpec {
@@ -396,34 +406,28 @@ pub struct CollectOpSpec {
396406}
397407
398408impl CollectOpSpec {
399- fn format ( & self , verbose : bool ) -> String {
409+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
400410 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+ }
412424 }
413425 }
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- }
422426}
423427
424428impl fmt:: Display for CollectOpSpec {
425429 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
426- write ! ( f, "{}" , self . format_concise ( ) )
430+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
427431 }
428432}
429433
@@ -490,37 +494,25 @@ pub struct ExportOpSpec {
490494}
491495
492496impl 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 ( ) ,
498501 } ;
499-
500502 let base = format ! (
501503 "collector={}, target={}, {}" ,
502504 self . collector_name, target_str, self . index_options
503505 ) ;
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) ,
509509 }
510510 }
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- }
519511}
520512
521513impl fmt:: Display for ExportOpSpec {
522514 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
523- write ! ( f, "{}" , self . format_concise ( ) )
515+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
524516 }
525517}
526518
@@ -533,26 +525,21 @@ pub enum ReactiveOpSpec {
533525}
534526
535527impl ReactiveOpSpec {
536- pub fn format_concise ( & self ) -> String {
528+ pub fn format ( & self , mode : SpecFormatMode ) -> String {
537529 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) ) ,
549536 }
550537 }
551538}
552539
553540impl fmt:: Display for ReactiveOpSpec {
554541 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
555- write ! ( f, "{}" , self . format_concise ( ) )
542+ write ! ( f, "{}" , self . format ( SpecFormatMode :: Concise ) )
556543 }
557544}
558545
0 commit comments