Skip to content

Commit 6b1a612

Browse files
AmmarAbouZormarcmo
authored andcommitted
Chipmunk CLI: Text formatter generic + improvements
* Make text formatter independent from parser by introducing fields for the original separator that must be provided by initializing an instance of it. * Add more documentation for this part and leave a TODO to revisit this temp solution in the future. * Avoid index checking to determine if separator should be printed. * Use direct methods on string buffer instead of write macros.
1 parent f035300 commit 6b1a612

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

cli/chipmunk-cli/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ pub async fn run_app(cancel_token: CancellationToken) -> anyhow::Result<()> {
4949
.await?;
5050
}
5151
OutputFormat::Text => {
52-
let text_formatter =
53-
MsgTextFormatter::new(cli.text_columns_separator, cli.text_args_separator);
52+
use parsers::dlt::fmt;
53+
let text_formatter = MsgTextFormatter::new(
54+
fmt::DLT_COLUMN_SENTINAL,
55+
fmt::DLT_ARGUMENT_SENTINAL,
56+
cli.text_columns_separator,
57+
cli.text_args_separator,
58+
);
5459

5560
start_session(parser, input, text_formatter, cli.output_path, cancel_token)
5661
.await?;

cli/chipmunk-cli/src/session/format/text.rs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ use std::fmt::Write as _;
44

55
use 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

159
use 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)]
3330
pub 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

4044
impl 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

Comments
 (0)