diff --git a/.talismanrc b/.talismanrc index eba24af..423c319 100644 --- a/.talismanrc +++ b/.talismanrc @@ -3,5 +3,7 @@ fileignoreconfig: checksum: f2e4da4dfc22981641fe21b13c8cef755785845c1431427b3312ca6558251a21 - filename: test/helpers/bats-support checksum: 2076897e3d8d1a27b5d649ebdf9637ea9de70464a8c45346e9054689157c1db3 +- filename: redactor/src/main.rs + checksum: ee591091d4fd625a98dd33fe1ec8a31a15e2a57a3123c3595722834e63d9df0c allowed_patterns: - pwd diff --git a/redactor/src/main.rs b/redactor/src/main.rs index 8b58e44..9f102d5 100644 --- a/redactor/src/main.rs +++ b/redactor/src/main.rs @@ -1,28 +1,31 @@ use std::io::{self, BufRead, Write, Result}; -pub fn redactor(input: R, mut output: W, num_digits: usize) +pub fn redactor(input: R, mut output: W) where R: BufRead, W: Write, { - let mut mask = String::new(); - for _ in 0..num_digits { - mask.push('#'); - } + const NUM_DIGITS: usize = 10; + let mask = "[REDACTED]"; for maybe_line in input.lines() { let mut line = fetch_next_line(maybe_line); - let sequences = find_digit_sequences(num_digits, &line); + let sequence_positions = find_digit_sequences(NUM_DIGITS, &line); - for sequence in &sequences { - let redaction_end = sequence + num_digits; - line.replace_range(sequence..&redaction_end, &mask); + for sequence_position in &sequence_positions { + if is_not_in_uuid(sequence_position, &line) { + line.replace_range(sequence_position..&(sequence_position + NUM_DIGITS), &mask); + } } write!(&mut output, "{}", line).expect("failed to write"); } () } +pub fn is_not_in_uuid(ten_digit_start_position: &usize, line: &String) -> bool { + true +} + fn fetch_next_line(maybe_line: Result) -> String { let mut line = maybe_line.expect("Failed to read line"); line.push('\n'); @@ -52,7 +55,7 @@ fn main() { let output = io::stdout(); - redactor(input, output, 10); + redactor(input, output); } #[cfg(test)] @@ -62,21 +65,21 @@ mod tests { #[test] fn test_replaces_digits_with_redaction_message() { - let input = b"123\n"; + let input = b"1234567890\n"; let mut output = Vec::new(); - redactor(&input[..], &mut output, 3); + redactor(&input[..], &mut output); let output_str = String::from_utf8(output).expect("Not UTF-8"); - assert_eq!("###\n", output_str); + assert_eq!("[REDACTED]\n", output_str); } #[test] fn test_does_not_replace_non_digits() { let mut output = Vec::new(); - redactor(&b"abc\n"[..], &mut output, 3); + redactor(&b"abc\n"[..], &mut output); assert_eq!("abc\n", String::from_utf8(output).unwrap()); } @@ -85,80 +88,89 @@ mod tests { fn test_replaces_only_digits() { let mut output = Vec::new(); - redactor(&b"a123b\n"[..], &mut output, 3); + redactor(&b"a1234567890b\n"[..], &mut output); - assert_eq!("a###b\n", String::from_utf8(output).unwrap()); + assert_eq!("a[REDACTED]b\n", String::from_utf8(output).unwrap()); } #[test] fn test_does_not_replace_fewer_digits_than_intended() { let mut output = Vec::new(); - redactor(&b"a12bc\n"[..], &mut output, 3); + redactor(&b"a123456789bc\n"[..], &mut output); - assert_eq!("a12bc\n", String::from_utf8(output).unwrap()); + assert_eq!("a123456789bc\n", String::from_utf8(output).unwrap()); } #[test] fn test_does_not_replace_more_digits_than_intended() { let mut output = Vec::new(); - redactor(&b"a1234bc\n"[..], &mut output, 3); + redactor(&b"a12345678904bc\n"[..], &mut output); - assert_eq!("a1234bc\n", String::from_utf8(output).unwrap()); + assert_eq!("a12345678904bc\n", String::from_utf8(output).unwrap()); } #[test] fn test_replaces_multiple_occurrences_on_line() { let mut output = Vec::new(); - redactor(&b"abc123de456fgh789xyz\n"[..], &mut output, 3); + redactor(&b"abc1234567890de1112223334fgh7778889990xyz\n"[..], &mut output); - assert_eq!("abc###de###fgh###xyz\n", String::from_utf8(output).unwrap()); + assert_eq!("abc[REDACTED]de[REDACTED]fgh[REDACTED]xyz\n", String::from_utf8(output).unwrap()); } #[test] fn test_replaces_digits_at_start_of_line() { let mut output = Vec::new(); - redactor(&b"123abc\n"[..], &mut output, 3); + redactor(&b"1234567890abc\n"[..], &mut output); - assert_eq!("###abc\n", String::from_utf8(output).unwrap()); + assert_eq!("[REDACTED]abc\n", String::from_utf8(output).unwrap()); } #[test] fn test_replaces_digits_at_end_of_line() { let mut output = Vec::new(); - redactor(&b"abc123\n"[..], &mut output, 3); + redactor(&b"abc1234567890\n"[..], &mut output); - assert_eq!("abc###\n", String::from_utf8(output).unwrap()); + assert_eq!("abc[REDACTED]\n", String::from_utf8(output).unwrap()); } #[test] fn test_includes_fewer_that_intended_digits_at_end_of_line() { let mut output = Vec::new(); - redactor(&b"abc12\n"[..], &mut output, 3); + redactor(&b"abc123456789\n"[..], &mut output); - assert_eq!("abc12\n", String::from_utf8(output).unwrap()); + assert_eq!("abc123456789\n", String::from_utf8(output).unwrap()); } #[test] fn test_replaces_digits_on_multiple_lines() { let mut output = Vec::new(); - redactor(&b"abc123\n456def789\n"[..], &mut output, 3); + redactor(&b"abc1234567890\n4567890123def7890123456\n"[..], &mut output); - assert_eq!("abc###\n###def###\n", String::from_utf8(output).unwrap()); + assert_eq!("abc[REDACTED]\n[REDACTED]def[REDACTED]\n", String::from_utf8(output).unwrap()); } #[test] fn test_copes_with_multiple_runs_of_shorter_digits() { let mut output = Vec::new(); - redactor(&b"abc12__34__56\n"[..], &mut output, 3); + redactor(&b"abc12__34__56\n"[..], &mut output); assert_eq!("abc12__34__56\n", String::from_utf8(output).unwrap()); } + + // #[test] + // fn test_recognises_that_ten_digit_sequence_is_in_uuid_when_position_is_at_start_of_last_sequence_of_a_uuid() { + // let single_uuid_line = String::from("2523fa52-1719-4300-9bb0-321612e7393a"); + // let in_uuid = !is_not_in_uuid(&24, &single_uuid_line); + // + // assert_eq!(single_uuid_line.chars().nth(24).unwrap(), '3'); + // assert_eq!(in_uuid, true); + // } }