fix reading invalid utf-8

This commit is contained in:
lelgenio 2022-02-13 21:02:14 -03:00
parent 5cdd18fb72
commit 2bd4b95bf4

View file

@ -16,9 +16,7 @@ fn main() {
.expect("hotwatch failed to initialize!"); .expect("hotwatch failed to initialize!");
std::env::args().for_each(|arg| { std::env::args().for_each(|arg| {
hotwatch hotwatch.watch(arg, handle_event).ok();
.watch(arg, handle_event)
.expect("hotwatch failed to initialize!");
}); });
hotwatch.run(); hotwatch.run();
@ -33,7 +31,7 @@ fn handle_event(event: hotwatch::Event) -> hotwatch::blocking::Flow {
} }
fn _handle_event(newfile: PathBuf) { fn _handle_event(newfile: PathBuf) {
let raw_content = match std::fs::read_to_string(&newfile) { let raw_content = match std::fs::read(&newfile) {
Ok(c) => c, Ok(c) => c,
Err(e) => { Err(e) => {
eprintln!( eprintln!(
@ -45,7 +43,7 @@ fn _handle_event(newfile: PathBuf) {
} }
}; };
let mail_content = match mailparse::parse_mail(raw_content.as_bytes()) { let mail_content = match mailparse::parse_mail(&raw_content) {
Ok(c) => c, Ok(c) => c,
Err(e) => { Err(e) => {
eprintln!( eprintln!(
@ -67,17 +65,16 @@ fn _handle_event(newfile: PathBuf) {
} }
}; };
let subject = match headers.get_first_value("Subject") { let subject = headers.get_first_value("Subject").unwrap_or_else(|| {
Some(sub) => sub, mail_content
None => mail_content
.get_body() .get_body()
.unwrap_or("".to_string()) .unwrap_or("".to_string())
.lines() .lines()
.filter(|line| line.len() != 0) .filter(|line| line.len() != 0)
.next() .next()
.unwrap_or("") .unwrap_or("")
.to_string(), .to_string()
}; });
Notification::new() Notification::new()
.summary(&format!("From: {}", from)) .summary(&format!("From: {}", from))