Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions src/output/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::{self, BufWriter, Write};
use std::path::Path;
use std::sync::Mutex;

use super::Output;
use super::{escape_csv_field, Output};
use crate::derive::DerivedKey;
use crate::matcher::MatchInfo;

Expand Down Expand Up @@ -76,8 +76,13 @@ impl Output for ConsoleOutput {
// Compact format: source,transform,privkey,address_compressed
writeln!(
w,
"{},{},{},{}",
source, transform, derived.private_key_hex, derived.p2pkh_compressed
"{}",
format_compact_csv_row(
source,
transform,
&derived.private_key_hex,
&derived.p2pkh_compressed,
)
)?;
}

Expand Down Expand Up @@ -122,6 +127,19 @@ impl Output for ConsoleOutput {
}
}

fn format_compact_csv_row(
source: &str,
transform: &str,
private_key: &str,
address: &str,
) -> String {
[source, transform, private_key, address]
.into_iter()
.map(escape_csv_field)
.collect::<Vec<_>>()
.join(",")
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -164,6 +182,48 @@ mod tests {
assert!(content.contains("test_source,sha256,abc123,1Address"));
}

#[test]
fn test_to_file_writes_compact_format_with_commas_in_source() {
let temp = NamedTempFile::new().unwrap();
let output = ConsoleOutput::to_file(temp.path()).unwrap();

output
.key("test,source", "sha256", &make_test_key())
.unwrap();
output.flush().unwrap();

let content = std::fs::read_to_string(temp.path()).unwrap();
assert!(content.contains("\"test,source\",sha256,abc123,1Address"));
}

#[test]
fn test_to_file_writes_compact_format_with_quotes_in_source() {
let temp = NamedTempFile::new().unwrap();
let output = ConsoleOutput::to_file(temp.path()).unwrap();

output
.key("say \"hello\"", "sha256", &make_test_key())
.unwrap();
output.flush().unwrap();

let content = std::fs::read_to_string(temp.path()).unwrap();
assert!(content.contains("\"say \"\"hello\"\"\",sha256,abc123,1Address"));
}

#[test]
fn test_to_file_writes_compact_format_with_newlines_in_source() {
let temp = NamedTempFile::new().unwrap();
let output = ConsoleOutput::to_file(temp.path()).unwrap();

output
.key("line1\nline2", "sha256", &make_test_key())
.unwrap();
output.flush().unwrap();

let content = std::fs::read_to_string(temp.path()).unwrap();
assert!(content.contains("\"line1\nline2\",sha256,abc123,1Address"));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

#[test]
fn test_to_file_verbose_writes_yaml_format() {
let temp = NamedTempFile::new().unwrap();
Expand Down
8 changes: 8 additions & 0 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ use crate::derive::DerivedKey;
use crate::matcher::MatchInfo;
use anyhow::Result;

pub(crate) fn escape_csv_field(field: &str) -> String {
if field.contains(',') || field.contains('"') || field.contains('\n') || field.contains('\r') {
format!("\"{}\"", field.replace('"', "\"\""))
} else {
field.to_string()
}
}

/// Output trait for handling generated keys.
pub trait Output: Send + Sync {
/// Output a key (no matcher, output all keys).
Expand Down
19 changes: 9 additions & 10 deletions src/output/query_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;
use arrow::datatypes::Schema;
use comfy_table::{presets::UTF8_FULL, Cell, Color, ContentArrangement, Table};

use super::escape_csv_field;
use crate::storage::{Row, Value};

#[derive(Clone, Copy, Debug, Default, PartialEq)]
Expand Down Expand Up @@ -216,14 +217,6 @@ fn format_value_csv(value: Option<&Value>) -> String {
}
}

fn escape_csv_field(field: &str) -> String {
if field.contains(',') || field.contains('"') || field.contains('\n') || field.contains('\r') {
format!("\"{}\"", field.replace('"', "\"\""))
} else {
field.to_string()
}
}

pub fn format_schema(schema: &Schema) -> String {
let mut table = Table::new();
table
Expand Down Expand Up @@ -270,8 +263,14 @@ mod tests {

#[test]
fn output_format_from_str() {
assert_eq!("table".parse::<OutputFormat>().unwrap(), OutputFormat::Table);
assert_eq!("TABLE".parse::<OutputFormat>().unwrap(), OutputFormat::Table);
assert_eq!(
"table".parse::<OutputFormat>().unwrap(),
OutputFormat::Table
);
assert_eq!(
"TABLE".parse::<OutputFormat>().unwrap(),
OutputFormat::Table
);
assert_eq!("json".parse::<OutputFormat>().unwrap(), OutputFormat::Json);
assert_eq!("JSON".parse::<OutputFormat>().unwrap(), OutputFormat::Json);
assert_eq!("csv".parse::<OutputFormat>().unwrap(), OutputFormat::Csv);
Expand Down
Loading