forked from nonsensical-dev/zenyx-engine
build: remove regex dependency in favor of rust iterators
This commit is contained in:
parent
4f423f4300
commit
31a06ff45b
5 changed files with 90 additions and 75 deletions
|
@ -6,7 +6,6 @@ use std::{error::Error, path::PathBuf};
|
|||
use backtrace::Backtrace;
|
||||
use native_dialog::{MessageDialog, MessageType};
|
||||
use parking_lot::Once;
|
||||
use regex::Regex;
|
||||
use tracing::error;
|
||||
|
||||
static INIT: parking_lot::Once = Once::new();
|
||||
|
@ -71,27 +70,26 @@ For future reference, the error summary is as follows:
|
|||
|
||||
println!("{}", final_msg.red().bold());
|
||||
|
||||
if let Err(e) = MessageDialog::new()
|
||||
if let Err(e) = MessageDialog::new()
|
||||
.set_type(MessageType::Error)
|
||||
.set_title("A fatal error in Zenyx has occurred")
|
||||
.set_text(&final_msg)
|
||||
.show_confirm() {
|
||||
error!("Failed to show message dialog: {e}")
|
||||
}
|
||||
|
||||
Ok(())
|
||||
.show_confirm()
|
||||
{
|
||||
error!("Failed to show message dialog: {e}")
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
fn capture_backtrace() -> String {
|
||||
let mut backtrace = String::new();
|
||||
let sysinfo = crate::metadata::SystemMetadata::current();
|
||||
let sysinfo = crate::metadata::SystemMetadata::current();
|
||||
backtrace.push_str(&sysinfo.verbose_summary());
|
||||
|
||||
let trace = backtrace::Backtrace::new();
|
||||
let message = format!("\nBacktrace:\n\n");
|
||||
backtrace.push_str(&message);
|
||||
backtrace.push_str(&format!("{trace:?}"));
|
||||
|
||||
|
||||
|
||||
backtrace
|
||||
}
|
||||
|
@ -102,8 +100,78 @@ trait Sanitize {
|
|||
|
||||
impl Sanitize for str {
|
||||
fn sanitize_path(&self) -> String {
|
||||
let username_pattern = r"(?i)(/home/|/Users/|\\Users\\)([^/\\]+)";
|
||||
let re = Regex::new(username_pattern).expect("Failed to compile regex for sanitization");
|
||||
re.replace_all(self, "${1}<USER>").to_string()
|
||||
let prefixes = ["/home/", "/Users/", "\\Users\\", "/opt/home/"];
|
||||
let mut result = String::from(self);
|
||||
|
||||
for prefix in prefixes {
|
||||
if let Some(start_index) = result.find(prefix) {
|
||||
let start_of_user = start_index + prefix.len();
|
||||
let mut end_of_user = result[start_of_user..]
|
||||
.find(|c| c == '/' || c == '\\')
|
||||
.map(|i| start_of_user + i)
|
||||
.unwrap_or(result.len());
|
||||
if end_of_user == start_of_user && start_of_user < result.len() {
|
||||
end_of_user = result.len();
|
||||
}
|
||||
result.replace_range(start_of_user..end_of_user, "<USER>");
|
||||
break;
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_sanitize_home() {
|
||||
assert_eq!(
|
||||
"/home/<USER>/documents",
|
||||
"/home/john.doe/documents".sanitize_path()
|
||||
);
|
||||
assert_eq!("/home/<USER>", "/home/jane".sanitize_path());
|
||||
assert_eq!("/opt/home/<USER>", "/opt/home/user".sanitize_path());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sanitize_users_unix() {
|
||||
assert_eq!(
|
||||
"/Users/<USER>/desktop",
|
||||
"/Users/alice/desktop".sanitize_path()
|
||||
);
|
||||
assert_eq!("/Users/<USER>/", "/Users/bob/".sanitize_path());
|
||||
assert_eq!("/user/Users/<USER>", "/user/Users/name".sanitize_path());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sanitize_users_windows() {
|
||||
assert_eq!(
|
||||
"\\Users\\<USER>\\documents",
|
||||
"\\Users\\charlie\\documents".sanitize_path()
|
||||
);
|
||||
assert_eq!("\\Users\\<USER>", "\\Users\\david".sanitize_path());
|
||||
assert_eq!(
|
||||
"C:\\Other\\Users\\<USER>",
|
||||
"C:\\Other\\Users\\folder".sanitize_path()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_match() {
|
||||
assert_eq!("/opt/data/file.txt", "/opt/data/file.txt".sanitize_path());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mixed_separators() {
|
||||
assert_eq!(
|
||||
"/home/<USER>\\documents",
|
||||
"/home/eve\\documents".sanitize_path()
|
||||
);
|
||||
assert_eq!(
|
||||
"\\Users\\<USER>/desktop",
|
||||
"\\Users\\frank/desktop".sanitize_path()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue