summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2018-03-01 13:06:25 +0100
committerLinus Färnstrand <linus@mullvad.net>2018-03-05 21:37:20 +0100
commit8e8ac4845387f76d0bc610372424ce8099136891 (patch)
tree81bd70595e69cadf0d24bf808f03a002a4185a36
parentf0a8aa532b7ea1ced7b650ee835cead61cb6942d (diff)
downloadmullvadvpn-8e8ac4845387f76d0bc610372424ce8099136891.tar.xz
mullvadvpn-8e8ac4845387f76d0bc610372424ce8099136891.zip
Use Cow<str> instead of String where possible
-rw-r--r--mullvad-daemon/src/bin/problem-report.rs30
1 files changed, 16 insertions, 14 deletions
diff --git a/mullvad-daemon/src/bin/problem-report.rs b/mullvad-daemon/src/bin/problem-report.rs
index 5b8a2f3569..e13116ca5d 100644
--- a/mullvad-daemon/src/bin/problem-report.rs
+++ b/mullvad-daemon/src/bin/problem-report.rs
@@ -22,6 +22,7 @@ use regex::Regex;
use std::cmp::min;
use std::collections::HashMap;
use std::env;
+use std::borrow::Cow;
use std::fmt;
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom, Write};
@@ -207,27 +208,27 @@ impl ProblemReport {
}
fn redact(&self, input: &str) -> String {
- let mut out = Self::redact_account_number(input);
- out = Self::redact_home_dir(&out);
- out = Self::redact_network_info(&out);
- self.redact_custom_strings(out)
+ let out1 = Self::redact_account_number(input);
+ let out2 = Self::redact_home_dir(&out1);
+ let out3 = Self::redact_network_info(&out2);
+ self.redact_custom_strings(&out3).to_string()
}
- fn redact_account_number(input: &str) -> String {
+ fn redact_account_number(input: &str) -> Cow<str> {
lazy_static! {
static ref RE: Regex = Regex::new("\\d{16}").unwrap();
}
- RE.replace_all(input, "[REDACTED ACCOUNT NUMBER]").to_string()
+ RE.replace_all(input, "[REDACTED ACCOUNT NUMBER]")
}
- fn redact_home_dir(input: &str) -> String {
+ fn redact_home_dir(input: &str) -> Cow<str> {
match env::home_dir() {
- Some(home) => input.replace(home.to_string_lossy().as_ref(), "~"),
- None => input.to_owned(),
+ Some(home) => Cow::from(input.replace(home.to_string_lossy().as_ref(), "~")),
+ None => Cow::from(input),
}
}
- fn redact_network_info(input: &str) -> String {
+ fn redact_network_info(input: &str) -> Cow<str> {
lazy_static! {
static ref RE: Regex = {
let combined_pattern = format!(
@@ -239,13 +240,14 @@ impl ProblemReport {
Regex::new(&combined_pattern).unwrap()
};
}
- RE.replace_all(input, "[REDACTED]").to_string()
+ RE.replace_all(input, "[REDACTED]")
}
- fn redact_custom_strings(&self, input: String) -> String {
- let mut out = input;
+ fn redact_custom_strings<'a>(&self, input: &'a str) -> Cow<'a, str> {
+ // Can probably me made a lot faster with aho-corasick if optimization is ever needed.
+ let mut out = Cow::from(input);
for redact in &self.redact_custom_strings {
- out = out.replace(redact, "[REDACTED]")
+ out = out.replace(redact, "[REDACTED]").into()
}
out
}