Skip to content

Commit 3deb43f

Browse files
committed
chore(node): get rid of regex in motd
1 parent 687bdea commit 3deb43f

File tree

3 files changed

+19
-72
lines changed

3 files changed

+19
-72
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tox_node/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ futures = { version = "0.3", default-features = false, features = ["std", "async
4242
hex = "0.4"
4343
itertools = "0.10"
4444
log = "0.4"
45-
regex = "1.6"
4645
serde = { version = "1.0", features = ["derive"] }
4746
serde_yaml = "0.9"
4847
crypto_box = "0.8"

tox_node/src/motd.rs

Lines changed: 19 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
1-
use std::borrow::Cow;
2-
use regex::Regex;
31
use time::OffsetDateTime;
42

53
use tox::core::stats::Stats;
64

7-
struct RegexMatches {
8-
regex: Regex,
9-
matches: bool,
10-
}
11-
12-
impl RegexMatches {
13-
pub fn new(template: &str, regex: Regex) -> RegexMatches {
14-
RegexMatches {
15-
matches: regex.is_match(template),
16-
regex,
17-
}
18-
}
19-
20-
pub fn replace<'a, F: FnOnce() -> String>(&self, template: &'a str, f: F) -> Cow<'a, str> {
21-
if self.matches {
22-
self.regex.replace_all(template, f().as_str())
23-
} else {
24-
Cow::from(template)
25-
}
26-
}
27-
}
28-
295
/// Packet counters for both tcp and udp.
306
pub struct Counters {
317
tcp: Stats,
@@ -42,45 +18,21 @@ impl Counters {
4218
}
4319

4420
pub struct Motd {
45-
start_date_regex: RegexMatches,
46-
uptime_regex: RegexMatches,
47-
tcp_packets_in_regex: RegexMatches,
48-
tcp_packets_out_regex: RegexMatches,
49-
udp_packets_in_regex: RegexMatches,
50-
udp_packets_out_regex: RegexMatches,
5121
start_date: OffsetDateTime,
5222
counters: Counters,
5323
template: String,
5424
}
5525

5626
impl Motd {
5727
pub fn new(template: String, counters: Counters) -> Motd {
58-
let start_date_regex = Regex::new(r"(?i)\{\{\s*start_date\s*\}\}")
59-
.expect("Failed to compile start_date regex");
60-
let uptime_regex = Regex::new(r"(?i)\{\{\s*uptime\s*\}\}")
61-
.expect("Failed to compile uptime regex");
62-
let tcp_packets_in_regex = Regex::new(r"(?i)\{\{\s*tcp_packets_in\s*\}\}")
63-
.expect("Failed to compile tcp_in regex");
64-
let tcp_packets_out_regex = Regex::new(r"(?i)\{\{\s*tcp_packets_out\s*\}\}")
65-
.expect("Failed to compile tcp_out regex");
66-
let udp_packets_in_regex = Regex::new(r"(?i)\{\{\s*udp_packets_in\s*\}\}")
67-
.expect("Failed to compile udp_in regex");
68-
let udp_packets_out_regex = Regex::new(r"(?i)\{\{\s*udp_packets_out\s*\}\}")
69-
.expect("Failed to compile udp_out regex");
7028
Motd {
71-
start_date_regex: RegexMatches::new(&template, start_date_regex),
72-
uptime_regex: RegexMatches::new(&template, uptime_regex),
73-
tcp_packets_in_regex: RegexMatches::new(&template, tcp_packets_in_regex),
74-
tcp_packets_out_regex: RegexMatches::new(&template, tcp_packets_out_regex),
75-
udp_packets_in_regex: RegexMatches::new(&template, udp_packets_in_regex),
76-
udp_packets_out_regex: RegexMatches::new(&template, udp_packets_out_regex),
7729
start_date: OffsetDateTime::now_utc(),
7830
counters,
7931
template,
8032
}
8133
}
8234

83-
fn summary(source: u64) -> String {
35+
fn format_n(source: u64) -> String {
8436
match source {
8537
0..=999 => format!("{}",source),
8638
1_000..=999_999 => format!("{0:.1}K", source as f32 / 1_000.0),
@@ -91,13 +43,14 @@ impl Motd {
9143
}
9244

9345
pub fn format(&self) -> String {
94-
let result = self.start_date_regex.replace(&self.template, || {
46+
let start_date = {
9547
let format = time::format_description::parse(
9648
"[year]-[month]-[day] [hour]:[minute]:[second]",
9749
).unwrap();
9850
self.start_date.format(&format).unwrap()
99-
});
100-
let result = self.uptime_regex.replace(&result, || {
51+
};
52+
53+
let uptime = {
10154
let uptime = OffsetDateTime::now_utc() - self.start_date;
10255
let days = uptime.whole_days();
10356
let hours = uptime.whole_hours() - uptime.whole_days() * 24;
@@ -108,23 +61,19 @@ impl Motd {
10861
hours,
10962
minutes
11063
)
111-
});
112-
let result = self.tcp_packets_in_regex.replace(&result, || {
113-
let packets = self.counters.tcp.counters.incoming();
114-
Self::summary(packets)
115-
});
116-
let result = self.tcp_packets_out_regex.replace(&result, || {
117-
let packets = self.counters.tcp.counters.outgoing();
118-
Self::summary(packets)
119-
});
120-
let result = self.udp_packets_in_regex.replace(&result, || {
121-
let packets = self.counters.udp.counters.incoming();
122-
Self::summary(packets)
123-
});
124-
let result = self.udp_packets_out_regex.replace(&result, || {
125-
let packets = self.counters.udp.counters.outgoing();
126-
Self::summary(packets)
127-
});
128-
result.into_owned()
64+
};
65+
66+
let tcp_packets_in = Self::format_n(self.counters.tcp.counters.incoming());
67+
let tcp_packets_out = Self::format_n(self.counters.tcp.counters.outgoing());
68+
let udp_packets_in = Self::format_n(self.counters.udp.counters.incoming());
69+
let udp_packets_out = Self::format_n(self.counters.udp.counters.outgoing());
70+
71+
let result = self.template.clone();
72+
let result = result.replace("{{start_date}}", &start_date);
73+
let result = result.replace("{{uptime}}", &uptime);
74+
let result = result.replace("{{tcp_packets_in}}", &tcp_packets_in);
75+
let result = result.replace("{{tcp_packets_out}}", &tcp_packets_out);
76+
let result = result.replace("{{udp_packets_in}}", &udp_packets_in);
77+
result.replace("{{udp_packets_out}}", &udp_packets_out)
12978
}
13079
}

0 commit comments

Comments
 (0)