From eda862ff1a097c3a3875212df9e42cf446e568fe Mon Sep 17 00:00:00 2001 From: Einliterflasche Date: Thu, 8 Aug 2024 12:56:28 +0200 Subject: [PATCH] format and comment --- swap/src/common.rs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/swap/src/common.rs b/swap/src/common.rs index 54332f01..0b5491e9 100644 --- a/swap/src/common.rs +++ b/swap/src/common.rs @@ -36,10 +36,17 @@ fn is_latest_version(current: &str, latest: &str) -> bool { } /// helper macro for [`redact`]... eldrich sorcery +/// the macro does in essence the following: +/// 1. create a static regex automaton for the pattern +/// 2. find all matching patterns using regex +/// 3. create a placeholder for each distinct matching pattern +/// 4. add the placeholder to the hashmap macro_rules! regex_find_placeholders { ($pattern:expr, $create_placeholder:expr, $replacements:expr, $input:expr) => {{ // compile the regex pattern - let regex = once_cell::sync::Lazy::new(|| regex::Regex::new($pattern).expect("invalid regex pattern")); + let regex = once_cell::sync::Lazy::new(|| { + regex::Regex::new($pattern).expect("invalid regex pattern") + }); // keep count of count patterns to generate distinct placeholders let mut counter: usize = 0; @@ -81,10 +88,30 @@ pub fn redact(input: &str) -> String { // use the macro to find all addresses and generate placeholders // has to be a macro in order to create the regex automata only once. - regex_find_placeholders!(MONERO_ADDR_REGEX, |count| format!(""), replacements, input); - regex_find_placeholders!(BITCOIN_ADDR_REGEX, |count| format!(""), replacements, input); - regex_find_placeholders!(TX_ID_REGEX, |count| format!(""), replacements, input); - regex_find_placeholders!(SWAP_ID_REGEX, |count| format!(""), replacements, input); + regex_find_placeholders!( + MONERO_ADDR_REGEX, + |count| format!(""), + replacements, + input + ); + regex_find_placeholders!( + BITCOIN_ADDR_REGEX, + |count| format!(""), + replacements, + input + ); + regex_find_placeholders!( + TX_ID_REGEX, + |count| format!(""), + replacements, + input + ); + regex_find_placeholders!( + SWAP_ID_REGEX, + |count| format!(""), + replacements, + input + ); // allocate string variable to operate on let mut redacted = input.to_owned();