Submenu Status: make regex more precise

This commit is contained in:
hinto-janaiyo 2022-12-28 22:46:52 -05:00
parent 19b5a2790b
commit 30d8968dd8
No known key found for this signature in database
GPG key ID: B1C5A64B80691E45
5 changed files with 74 additions and 39 deletions

View file

@ -1329,7 +1329,7 @@ impl PubP2poolApi {
let mut sum: f64 = 0.0; let mut sum: f64 = 0.0;
let mut count: u128 = 0; let mut count: u128 = 0;
for i in iter { for i in iter {
if let Some(word) = regex.float.find(i.as_str()) { if let Some(word) = regex.payout_float.find(i.as_str()) {
match word.as_str().parse::<f64>() { match word.as_str().parse::<f64>() {
Ok(num) => { sum += num; count += 1; }, Ok(num) => { sum += num; count += 1; },
Err(e) => error!("P2Pool | Total XMR sum calculation error: [{}]", e), Err(e) => error!("P2Pool | Total XMR sum calculation error: [{}]", e),

View file

@ -17,22 +17,22 @@
// These are general QoL macros, nothing too scary, I promise. // These are general QoL macros, nothing too scary, I promise.
// //
// | MACRO | PURPOSE | EQUIVALENT CODE | // | MACRO | PURPOSE | EQUIVALENT CODE |
// |---------|----------------------------------------------|------------------------------------------------------------| // |---------|-----------------------------------------------|------------------------------------------------------------|
// | lock | Lock an [Arc<Mutex>] | a.lock().unwrap() | // | lock | Lock an [Arc<Mutex>] | a.lock().unwrap() |
// | lock2 | Lock a field inside a struct, both Arc<Mutex | a.lock().unwrap().b.lock().unwrap() | // | lock2 | Lock a field inside a struct, both Arc<Mutex> | a.lock().unwrap().b.lock().unwrap() |
// | arc_mut | Create a new [Arc<Mutex>] | std::sync::Arc::new(std::sync::Mutex::new(my_value)) | // | arc_mut | Create a new [Arc<Mutex>] | std::sync::Arc::new(std::sync::Mutex::new(my_value)) |
// | sleep | Sleep the current thread for x milliseconds | std::thread::sleep(std::time::Duration::from_millis(1000)) | // | sleep | Sleep the current thread for x milliseconds | std::thread::sleep(std::time::Duration::from_millis(1000)) |
// | flip | Flip a bool in place | my_bool = !my_bool | // | flip | Flip a bool in place | my_bool = !my_bool |
// //
// Hopefully the long ass code on the right justifies usage of macros :D // Hopefully the long ass code on the right justifies usage of macros :D
// //
// [lock2!()] works like this: "lock2!(my_first, my_second)" // [lock2!()] works like this: "lock2!(my_first, my_second)"
// and expects it be a [Struct]-[field] relationship, e.g: // and expects it be a [Struct]-[field] relationship, e.g:
// //
// let my_first = Struct { // let my_first = Arc::new(Mutex::new(Struct {
// my_second: Arc::new(Mutex::new(true)), // my_second: Arc::new(Mutex::new(true)),
// }; // }));
// lock2!(my_first, my_second); // lock2!(my_first, my_second);
// //
// The equivalent code is: "my_first.lock().unwrap().my_second.lock().unwrap()" (see? this is long as hell) // The equivalent code is: "my_first.lock().unwrap().my_second.lock().unwrap()" (see? this is long as hell)
@ -45,14 +45,6 @@ macro_rules! lock {
} }
pub(crate) use lock; pub(crate) use lock;
// Creates a new [Arc<Mutex<T>]
macro_rules! arc_mut {
($arc_mutex:expr) => {
std::sync::Arc::new(std::sync::Mutex::new($arc_mutex))
};
}
pub(crate) use arc_mut;
// Locks and unwraps a field of a struct, both of them being [Arc<Mutex>] // Locks and unwraps a field of a struct, both of them being [Arc<Mutex>]
// Yes, I know this is bad code. // Yes, I know this is bad code.
macro_rules! lock2 { macro_rules! lock2 {
@ -62,6 +54,14 @@ macro_rules! lock2 {
} }
pub(crate) use lock2; pub(crate) use lock2;
// Creates a new [Arc<Mutex<T>]
macro_rules! arc_mut {
($arc_mutex:expr) => {
std::sync::Arc::new(std::sync::Mutex::new($arc_mutex))
};
}
pub(crate) use arc_mut;
// Sleeps a [std::thread] using milliseconds // Sleeps a [std::thread] using milliseconds
macro_rules! sleep { macro_rules! sleep {
($millis:expr) => { ($millis:expr) => {
@ -72,11 +72,11 @@ pub(crate) use sleep;
// Flips a [bool] in place // Flips a [bool] in place
macro_rules! flip { macro_rules! flip {
($b:expr) => { ($b:expr) => {
match $b { match $b {
true|false => $b = !$b, true|false => $b = !$b,
}; }
}; };
} }
pub(crate) use flip; pub(crate) use flip;

View file

@ -1326,7 +1326,7 @@ impl eframe::App for App {
} }
} }
let color = if hide { BLACK } else { BRIGHT_YELLOW }; let color = if hide { BLACK } else { BRIGHT_YELLOW };
if ui.add_sized([box_width, height], Button::new(RichText::new("👁").color(color))).on_hover_text(PASSWORD_HIDE).clicked() { flip!(sudo.hide) } if ui.add_sized([box_width, height], Button::new(RichText::new("👁").color(color))).on_hover_text(PASSWORD_HIDE).clicked() { flip!(sudo.hide); }
}); });
if (key.is_esc() && !sudo.testing) || ui.add_sized([width, height*4.0], Button::new("Leave")).on_hover_text(PASSWORD_LEAVE).clicked() { self.error_state.reset(); }; if (key.is_esc() && !sudo.testing) || ui.add_sized([width, height*4.0], Button::new("Leave")).on_hover_text(PASSWORD_LEAVE).clicked() { self.error_state.reset(); };
// If [test_sudo()] finished, reset error state. // If [test_sudo()] finished, reset error state.

View file

@ -69,21 +69,56 @@ impl Regexes {
// Both are nominally fast enough where it doesn't matter too much but meh, why not use regex. // Both are nominally fast enough where it doesn't matter too much but meh, why not use regex.
#[derive(Clone,Debug)] #[derive(Clone,Debug)]
pub struct P2poolRegex { pub struct P2poolRegex {
pub payout: regex::Regex,
pub float: regex::Regex,
pub date: regex::Regex, pub date: regex::Regex,
pub payout: regex::Regex,
pub payout_float: regex::Regex,
pub block: regex::Regex, pub block: regex::Regex,
pub int: regex::Regex, pub block_int: regex::Regex,
} }
impl P2poolRegex { impl P2poolRegex {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
payout: regex::Regex::new("payout of [0-9].[0-9]+ XMR").unwrap(),
float: regex::Regex::new("[0-9].[0-9]+").unwrap(),
date: regex::Regex::new("[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+.[0-9]+").unwrap(), date: regex::Regex::new("[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+.[0-9]+").unwrap(),
block: regex::Regex::new("block [0-9]+").unwrap(), payout: regex::Regex::new("payout of [0-9].[0-9]+ XMR").unwrap(), // Assumes 12 digits after the dot.
int: regex::Regex::new("[0-9]+").unwrap(), payout_float: regex::Regex::new("[0-9].[0-9]{12}").unwrap(), // Assumes 12 digits after the dot.
block: regex::Regex::new("block [0-9]{7}").unwrap(), // Monero blocks will be 7 digits for... the next 10,379 years
block_int: regex::Regex::new("[0-9]{7}").unwrap(),
} }
} }
} }
//---------------------------------------------------------------------------------------------------- TESTS
#[cfg(test)]
mod test {
#[test]
fn build_regexes() {
use regex::Regex;
let r = crate::Regexes::new();
assert!(Regex::is_match(&r.name, "_this_ is... a n-a-m-e."));
assert!(Regex::is_match(&r.address, "44hintoFpuo3ugKfcqJvh5BmrsTRpnTasJmetKC4VXCt6QDtbHVuixdTtsm6Ptp7Y8haXnJ6j8Gj2dra8CKy5ewz7Vi9CYW"));
assert!(Regex::is_match(&r.ipv4, "192.168.1.2"));
assert!(Regex::is_match(&r.ipv4, "127.0.0.1"));
assert!(Regex::is_match(&r.domain, "my.node.com"));
assert!(Regex::is_match(&r.domain, "my.monero-node123.net"));
assert!(Regex::is_match(&r.domain, "www.my-node.org"));
assert!(Regex::is_match(&r.domain, "www.my-monero-node123.io"));
for i in 1..=65535 {
assert!(Regex::is_match(&r.port, &i.to_string()));
}
assert!(!Regex::is_match(&r.port, "0"));
assert!(!Regex::is_match(&r.port, "65536"));
}
#[test]
fn build_p2pool_regex() {
use regex::Regex;
let r = crate::P2poolRegex::new();
let text = "NOTICE 2022-11-11 11:11:11.1111 P2Pool You received a payout of 0.111111111111 XMR in block 1111111";
assert_eq!(r.payout.find(text).unwrap().as_str(), "payout of 0.111111111111 XMR");
assert_eq!(r.payout_float.find(text).unwrap().as_str(), "0.111111111111");
assert_eq!(r.date.find(text).unwrap().as_str(), "2022-11-11 11:11:11.1111");
assert_eq!(r.block.find(text).unwrap().as_str(), "block 1111111");
assert_eq!(r.block_int.find(text).unwrap().as_str(), "1111111");
}
}

View file

@ -127,7 +127,7 @@ impl PayoutOrd {
}; };
// AtomicUnit // AtomicUnit
let atomic_unit = if let Some(word) = regex.payout.find(line) { let atomic_unit = if let Some(word) = regex.payout.find(line) {
if let Some(word) = regex.float.find(word.as_str()) { if let Some(word) = regex.payout_float.find(word.as_str()) {
match word.as_str().parse::<f64>() { match word.as_str().parse::<f64>() {
Ok(au) => AtomicUnit::from_f64(au), Ok(au) => AtomicUnit::from_f64(au),
Err(e) => { error!("P2Pool | AtomicUnit parse error: [{}] on [{}]", e, line); AtomicUnit::new() }, Err(e) => { error!("P2Pool | AtomicUnit parse error: [{}] on [{}]", e, line); AtomicUnit::new() },
@ -140,7 +140,7 @@ impl PayoutOrd {
}; };
// Block // Block
let block = if let Some(word) = regex.block.find(line) { let block = if let Some(word) = regex.block.find(line) {
if let Some(word) = regex.int.find(word.as_str()) { if let Some(word) = regex.block_int.find(word.as_str()) {
match word.as_str().parse::<u64>() { match word.as_str().parse::<u64>() {
Ok(b) => HumanNumber::from_u64(b), Ok(b) => HumanNumber::from_u64(b),
Err(e) => { error!("P2Pool | Block parse error: [{}] on [{}]", e, line); HumanNumber::unknown() }, Err(e) => { error!("P2Pool | Block parse error: [{}] on [{}]", e, line); HumanNumber::unknown() },
@ -225,18 +225,18 @@ mod test {
fn update_p2pool_payout_log() { fn update_p2pool_payout_log() {
use crate::xmr::PayoutOrd; use crate::xmr::PayoutOrd;
let log = let log =
r#"NOTICE 2021-12-21 01:01:01.1111 P2Pool You received a payout of 0.001000000000 XMR in block 1 r#"NOTICE 2021-12-21 01:01:01.1111 P2Pool You received a payout of 0.001000000000 XMR in block 1234567
NOTICE 2021-12-21 02:01:01.1111 P2Pool You received a payout of 0.002000000000 XMR in block 2 NOTICE 2021-12-21 02:01:01.1111 P2Pool You received a payout of 0.002000000000 XMR in block 2345678
NOTICE 2021-12-21 03:01:01.1111 P2Pool You received a payout of 0.003000000000 XMR in block 3 NOTICE 2021-12-21 03:01:01.1111 P2Pool You received a payout of 0.003000000000 XMR in block 3456789
"#; "#;
let mut payout_ord = PayoutOrd::new(); let mut payout_ord = PayoutOrd::new();
println!("BEFORE: {}", payout_ord); println!("BEFORE: {}", payout_ord);
PayoutOrd::update_from_payout_log(&mut payout_ord, log); PayoutOrd::update_from_payout_log(&mut payout_ord, log);
println!("AFTER: {}", payout_ord); println!("AFTER: {}", payout_ord);
let should_be = let should_be =
r#"2021-12-21 01:01:01.1111 | 0.001000000000 XMR | Block 1 r#"2021-12-21 01:01:01.1111 | 0.001000000000 XMR | Block 1,234,567
2021-12-21 02:01:01.1111 | 0.002000000000 XMR | Block 2 2021-12-21 02:01:01.1111 | 0.002000000000 XMR | Block 2,345,678
2021-12-21 03:01:01.1111 | 0.003000000000 XMR | Block 3 2021-12-21 03:01:01.1111 | 0.003000000000 XMR | Block 3,456,789
"#; "#;
assert_eq!(payout_ord.to_string(), should_be) assert_eq!(payout_ord.to_string(), should_be)
} }