Skip to content

Commit

Permalink
Extraneous horizontal tab at end of single/last output column #119
Browse files Browse the repository at this point in the history
  • Loading branch information
jhspetersson committed Jan 10, 2022
1 parent 9a7cabd commit 6f17bad
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/output/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl ResultsFormatter for CsvFormatter {
None
}

fn format_element(&mut self, _: &str, record: &str) -> Option<String> {
fn format_element(&mut self, _: &str, record: &str, _is_last: bool) -> Option<String> {
self.records.push(record.to_owned());
None
}
Expand Down
13 changes: 8 additions & 5 deletions src/output/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::output::ResultsFormatter;

pub const LINES_FORMATTER: FlatWriter = FlatWriter {
record_separator: '\n',
line_separator: None,
line_separator: Some('\n'),
};


pub const LIST_FORMATTER: FlatWriter = FlatWriter {
record_separator: '\0',
line_separator: None,
line_separator: Some('\0'),
};


Expand All @@ -32,8 +32,11 @@ impl ResultsFormatter for FlatWriter {
None
}

fn format_element(&mut self, _: &str, record: &str) -> Option<String> {
Some(format!("{}{}", record, self.record_separator))
fn format_element(&mut self, _: &str, record: &str, is_last: bool) -> Option<String> {
match is_last {
true => Some(format!("{}", record)),
false => Some(format!("{}{}", record, self.record_separator))
}
}

fn row_ended(&mut self) -> Option<String> {
Expand Down Expand Up @@ -66,6 +69,6 @@ mod test {
#[test]
fn test_tab() {
let result = write_test_items(&mut TABS_FORMATTER);
assert_eq!("foo_value\tBAR value\t\n123\t\t\n", result);
assert_eq!("foo_value\tBAR value\n123\t\n", result);
}
}
2 changes: 1 addition & 1 deletion src/output/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl ResultsFormatter for HtmlFormatter {
Some("<tr>".to_owned())
}

fn format_element(&mut self, _: &str, record: &str) -> Option<String> {
fn format_element(&mut self, _: &str, record: &str, _is_last: bool) -> Option<String> {
Some(format!("<td>{}</td>", record))
}

Expand Down
2 changes: 1 addition & 1 deletion src/output/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl ResultsFormatter for JsonFormatter {
None
}

fn format_element(&mut self, name: &str, record: &str) -> Option<String> {
fn format_element(&mut self, name: &str, record: &str, _is_last: bool) -> Option<String> {
self.file_map.insert(name.to_owned(), record.to_owned());
None
}
Expand Down
19 changes: 10 additions & 9 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod csv;
pub trait ResultsFormatter {
fn header(&mut self) -> Option<String>;
fn row_started(&mut self) -> Option<String>;
fn format_element(&mut self, name: &str, record: &str) -> Option<String>;
fn format_element(&mut self, name: &str, record: &str, is_last: bool) -> Option<String>;
fn row_ended(&mut self) -> Option<String>;
fn footer(&mut self) -> Option<String>;

Expand Down Expand Up @@ -43,8 +43,9 @@ impl ResultsWriter {

pub fn write_row(&mut self, writer: &mut dyn Write, values: Vec<(String, String)>) -> std::io::Result<()> {
self.write_row_start(writer)?;
for (name, value) in values {
self.write_row_item(writer, &name, &value)?;
let len = values.len();
for (pos, (name, value)) in values.iter().enumerate() {
self.write_row_item(writer, &name, &value, pos == len - 1)?;
}
self.write_row_end(writer)
}
Expand All @@ -58,8 +59,8 @@ impl ResultsWriter {
self.formatter.row_started()
.map_or(Ok(()), |value| write!(writer, "{}", value))
}
fn write_row_item(&mut self, writer: &mut dyn Write, name: &str, value: &str) -> std::io::Result<()> {
self.formatter.format_element(name, value)
fn write_row_item(&mut self, writer: &mut dyn Write, name: &str, value: &str, is_last: bool) -> std::io::Result<()> {
self.formatter.format_element(name, value, is_last)
.map_or(Ok(()), |value| write!(writer, "{}", value))
}

Expand Down Expand Up @@ -89,13 +90,13 @@ mod test {
let mut result = String::from("");
under_test.header().and_then(|s| Some(result.push_str(&s)));
under_test.row_started().and_then(|s| Some(result.push_str(&s)));
under_test.format_element("foo", "foo_value").and_then(|s| Some(result.push_str(&s)));
under_test.format_element("bar", "BAR value").and_then(|s| Some(result.push_str(&s)));
under_test.format_element("foo", "foo_value", false).and_then(|s| Some(result.push_str(&s)));
under_test.format_element("bar", "BAR value", true).and_then(|s| Some(result.push_str(&s)));
under_test.row_ended().and_then(|s| Some(result.push_str(&s)));
under_test.row_separator().and_then(|s| Some(result.push_str(&s)));
under_test.row_started().and_then(|s| Some(result.push_str(&s)));
under_test.format_element("foo", "123").and_then(|s| Some(result.push_str(&s)));
under_test.format_element("bar", "").and_then(|s| Some(result.push_str(&s)));
under_test.format_element("foo", "123", false).and_then(|s| Some(result.push_str(&s)));
under_test.format_element("bar", "", true).and_then(|s| Some(result.push_str(&s)));
under_test.row_ended().and_then(|s| Some(result.push_str(&s)));
under_test.footer().and_then(|s| Some(result.push_str(&s)));
result
Expand Down

0 comments on commit 6f17bad

Please sign in to comment.