@@ -4,13 +4,7 @@ use std::fmt::Write as _;
44
55use anyhow:: Context ;
66
7- use parsers:: {
8- dlt:: fmt:: {
9- DLT_ARGUMENT_SENTINAL as INEXER_DLT_ARGUMENT_SENTINAL ,
10- DLT_COLUMN_SENTINAL as INEXER_DLT_COLUMN_SENTINAL ,
11- } ,
12- LogMessage ,
13- } ;
7+ use parsers:: LogMessage ;
148
159use super :: MessageFormatter ;
1610
@@ -28,22 +22,48 @@ const WRITE_ERROR_MSG: &str = "Error while writing parsed message to buffer";
2822/// the performance assuming it will be called inside a hot loop.
2923///
3024/// # Note:
31- /// Struct currently have support for DLT-messages only.
25+ /// Formatting needs the columns and arguments separators used originally in each parser
26+ /// to avoid any changes in indexer libraries before the implementation of this tool is
27+ /// stabilized.
28+ // TODO: Revisit this part once the UI part of this CLI tool is implemented.
3229#[ derive( Debug , Clone ) ]
3330pub struct MsgTextFormatter {
3431 origin_msg_buffer : String ,
3532 replaced_msg_buffer : String ,
33+ /// The separator used for message columns in the parser used in indexer crates originally.
34+ indexer_cols_sep : char ,
35+ /// The separator used for message payload arguments in the parser used in indexer
36+ /// crates originally.
37+ indexer_args_sep : char ,
38+ /// The separator to be used for message columns in the output of this session.
3639 columns_separator : String ,
40+ /// The separator to be used for message payload arguments in the output of this session.
3741 argument_separator : String ,
3842}
3943
4044impl MsgTextFormatter {
41- pub fn new ( columns_separator : String , argument_separator : String ) -> Self {
45+ /// Creates a new instance with the given arguments.
46+ ///
47+ /// * `indexer_cols_sep`: Separator used for message columns in the parser used in indexer
48+ /// crates originally.
49+ /// * `indexer_args_sep`: Separator used for message payload arguments in the parser used
50+ /// in indexer crates originally
51+ /// * `columns_separator`: Separator to be used for message columns in the output of this session.
52+ /// * `argument_separator`: Separator to be used for message payload arguments in the output of
53+ /// this session.
54+ pub fn new (
55+ indexer_cols_sep : char ,
56+ indexer_args_sep : char ,
57+ columns_separator : String ,
58+ argument_separator : String ,
59+ ) -> Self {
4260 Self {
4361 origin_msg_buffer : String :: new ( ) ,
4462 replaced_msg_buffer : String :: new ( ) ,
4563 columns_separator,
4664 argument_separator,
65+ indexer_cols_sep,
66+ indexer_args_sep,
4767 }
4868 }
4969}
@@ -63,23 +83,28 @@ impl MessageFormatter for MsgTextFormatter {
6383
6484 let rep_buff = & mut self . replaced_msg_buffer ;
6585
66- for ( idx, main ) in self
86+ for ( idx, cols ) in self
6787 . origin_msg_buffer
68- . split ( INEXER_DLT_COLUMN_SENTINAL )
88+ . split ( self . indexer_cols_sep )
6989 . enumerate ( )
7090 {
7191 if idx != 0 {
72- write ! ( rep_buff, "{}" , self . columns_separator) . context ( WRITE_ERROR_MSG ) ? ;
92+ rep_buff. push_str ( & self . columns_separator ) ;
7393 }
74- for ( jdx, argument) in main
75- . split ( INEXER_DLT_ARGUMENT_SENTINAL )
76- . filter ( |e| !e. trim ( ) . is_empty ( ) )
77- . enumerate ( )
78- {
79- if jdx != 0 {
80- write ! ( rep_buff, "{}" , self . argument_separator) . context ( WRITE_ERROR_MSG ) ?;
81- }
82- write ! ( rep_buff, "{argument}" ) . context ( WRITE_ERROR_MSG ) ?;
94+
95+ let mut main_iter = cols
96+ . split ( self . indexer_args_sep )
97+ . filter ( |e| !e. trim ( ) . is_empty ( ) ) ;
98+
99+ let Some ( first) = main_iter. next ( ) else {
100+ continue ;
101+ } ;
102+
103+ rep_buff. push_str ( first) ;
104+
105+ for argument in main_iter {
106+ rep_buff. push_str ( & self . argument_separator ) ;
107+ rep_buff. push_str ( argument) ;
83108 }
84109 }
85110
0 commit comments