@@ -8,42 +8,39 @@ use std::path::PathBuf;
88
99/// Configuration for handling BE API responses
1010pub struct BeResponseHandler < ' a > {
11- pub success_message : & ' a str ,
1211 pub empty_warning : & ' a str ,
1312 pub error_context : & ' a str ,
1413 pub tips : & ' a str ,
1514}
1615
1716impl < ' a > BeResponseHandler < ' a > {
17+ fn handle_error ( & self , e : crate :: error:: CliError ) -> crate :: error:: CliError {
18+ ui:: print_error ( & format ! ( "{}: {e}." , self . error_context) ) ;
19+ ui:: print_info ( & format ! ( "Tips: {}" , self . tips) ) ;
20+ e
21+ }
22+
1823 /// Handle response for console-only output (like be_vars)
1924 pub fn handle_console_result (
2025 & self ,
2126 result : Result < String > ,
2227 context : & str ,
2328 ) -> Result < ExecutionResult > {
24- match result {
25- Ok ( output) => {
26- ui:: print_success ( self . success_message ) ;
27- println ! ( ) ;
28- ui:: print_info ( "Results:" ) ;
29+ let output = result. map_err ( |e| self . handle_error ( e) ) ?;
2930
30- if output. is_empty ( ) {
31- ui:: print_warning ( & self . empty_warning . replace ( "{}" , context) ) ;
32- } else {
33- println ! ( "{output}" ) ;
34- }
31+ println ! ( ) ;
32+ ui:: print_info ( "Results:" ) ;
3533
36- Ok ( ExecutionResult {
37- output_path : PathBuf :: from ( "console_output" ) ,
38- message : format ! ( "Query completed for: {context}" ) ,
39- } )
40- }
41- Err ( e) => {
42- ui:: print_error ( & format ! ( "{}: {e}." , self . error_context) ) ;
43- ui:: print_info ( & format ! ( "Tips: {}" , self . tips) ) ;
44- Err ( e)
45- }
34+ if output. is_empty ( ) {
35+ ui:: print_warning ( & self . empty_warning . replace ( "{}" , context) ) ;
36+ } else {
37+ println ! ( "{output}" ) ;
4638 }
39+
40+ Ok ( ExecutionResult {
41+ output_path : PathBuf :: from ( "console_output" ) ,
42+ message : format ! ( "Query completed for: {context}" ) ,
43+ } )
4744 }
4845
4946 /// Handle response with file output (like pipeline_tasks)
@@ -57,47 +54,36 @@ impl<'a> BeResponseHandler<'a> {
5754 where
5855 F : Fn ( & str ) -> String ,
5956 {
60- match result {
61- Ok ( output) => {
62- ui:: print_success ( self . success_message ) ;
63- println ! ( ) ;
64- ui:: print_info ( "Results:" ) ;
57+ let output = result. map_err ( |e| self . handle_error ( e) ) ?;
6558
66- if output . trim ( ) . is_empty ( ) {
67- ui:: print_warning ( self . empty_warning ) ;
59+ println ! ( ) ;
60+ ui:: print_info ( "Results:" ) ;
6861
69- Ok ( ExecutionResult {
70- output_path : PathBuf :: from ( "console_output" ) ,
71- message : "No data found" . to_string ( ) ,
72- } )
73- } else {
74- let timestamp = Utc :: now ( ) . format ( "%Y%m%d_%H%M%S" ) ;
75-
76- let filename = format ! ( "{file_prefix}_{timestamp}.txt" ) ;
77- let output_path = config. output_dir . join ( filename) ;
62+ if output. trim ( ) . is_empty ( ) {
63+ ui:: print_warning ( self . empty_warning ) ;
64+ return Ok ( ExecutionResult {
65+ output_path : PathBuf :: from ( "console_output" ) ,
66+ message : "No data found" . to_string ( ) ,
67+ } ) ;
68+ }
7869
79- fs:: write ( & output_path, & output) ?;
70+ let timestamp = Utc :: now ( ) . format ( "%Y%m%d_%H%M%S" ) ;
71+ let filename = format ! ( "{file_prefix}_{timestamp}.txt" ) ;
72+ let output_path = config. output_dir . join ( filename) ;
8073
81- println ! ( "{}" , summary_fn( & output) ) ;
74+ fs:: write ( & output_path, & output) ?;
75+ println ! ( "{}" , summary_fn( & output) ) ;
8276
83- let message = format ! (
84- "{} saved to {}" ,
85- file_prefix. replace( '_' , " " ) . to_title_case( ) ,
86- output_path. display( )
87- ) ;
77+ let message = format ! (
78+ "{} saved to {}" ,
79+ file_prefix. replace( '_' , " " ) . to_title_case( ) ,
80+ output_path. display( )
81+ ) ;
8882
89- Ok ( ExecutionResult {
90- output_path,
91- message,
92- } )
93- }
94- }
95- Err ( e) => {
96- ui:: print_error ( & format ! ( "{}: {e}." , self . error_context) ) ;
97- ui:: print_info ( & format ! ( "Tips: {}" , self . tips) ) ;
98- Err ( e)
99- }
100- }
83+ Ok ( ExecutionResult {
84+ output_path,
85+ message,
86+ } )
10187 }
10288}
10389
0 commit comments