diff --git a/src/config.rs b/src/config.rs index c1df822..62c7dad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use crate::algorithm::Algorithm; use crate::canonicalization::Canonicalization; +use anyhow::{anyhow, Result}; use clap::Parser; use std::collections::HashSet; use std::fs::File; @@ -39,7 +40,7 @@ pub struct Config { } impl Config { - pub fn init() -> Result { + pub fn init() -> Result { let mut cnf = Self::parse(); cnf.key_data_base = process_key_data_base(cnf.key_data_base); cnf.domain = process_domains(&cnf.domain, &cnf.domain_file)?; @@ -116,12 +117,12 @@ fn process_key_data_base(opt: Option) -> Option { } } -fn process_domains(lst: &[String], domain_file: &Option) -> Result, String> { +fn process_domains(lst: &[String], domain_file: &Option) -> Result> { let mut domain_set: HashSet = lst.iter().map(|e| e.to_string()).collect(); if let Some(path) = domain_file { - let f = File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let f = File::open(path).map_err(|e| anyhow!("{}: {e}", path.display()))?; for line in BufReader::new(f).lines() { - let line = line.map_err(|e| format!("{}: {e}", path.display()))?; + let line = line.map_err(|e| anyhow!("{}: {e}", path.display()))?; let domain = line.trim(); if !domain.is_empty() && !domain.starts_with('#') { domain_set.insert(domain.to_string().to_lowercase()); diff --git a/src/entry.rs b/src/entry.rs index d117e06..0f46091 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,4 +1,5 @@ use crate::stdin_reader::StdinReader; +use anyhow::{anyhow, Result}; use nom::bytes::streaming::{tag, take_till, take_while1}; use nom::IResult; use std::sync::Arc; @@ -32,13 +33,13 @@ impl Entry { self.data == vec![b'.'] } - fn from_bytes(input: &[u8]) -> Result { - let (_, entry) = parse_entry(input).map_err(|e| format!("parsing error: {e}"))?; + fn from_bytes(input: &[u8]) -> Result { + let (_, entry) = parse_entry(input).map_err(|e| anyhow!("parsing error: {e}"))?; Ok(entry) } } -pub async fn read_entry(reader_lock: Arc>) -> Option> { +pub async fn read_entry(reader_lock: Arc>) -> Option> { let mut reader = reader_lock.write().await; log::trace!("reader lock on stdin locked"); let line_res = reader.read_line().await; diff --git a/src/message.rs b/src/message.rs index b936283..5b3d0bf 100644 --- a/src/message.rs +++ b/src/message.rs @@ -77,8 +77,8 @@ impl Message { ); // TODO: sign the message using DKIM } - Err(_) => { - log::error!("{}: unable to parse message", self.session_id); + Err(err) => { + log::error!("{msg_id}: unable to parse message: {err}"); } } if let Err(err) = self.print_msg().await { diff --git a/src/parsed_message.rs b/src/parsed_message.rs index 20705dc..1aafe69 100644 --- a/src/parsed_message.rs +++ b/src/parsed_message.rs @@ -1,13 +1,15 @@ +use anyhow::{anyhow, Result}; + pub struct ParsedMessage<'a> { pub headers: Vec>, pub body: &'a [u8], } impl<'a> ParsedMessage<'a> { - pub fn from_bytes(data: &'a [u8]) -> Result { + pub fn from_bytes(data: &'a [u8]) -> Result { let (mut raw_headers, body) = match data.windows(4).position(|w| w == b"\r\n\r\n") { Some(body_index) => (&data[..body_index + 2], &data[body_index + 4..]), - None => return Err(()), + None => return Err(anyhow!("message body not found")), }; let mut headers = Vec::with_capacity(128); while !raw_headers.is_empty() { @@ -25,14 +27,15 @@ fn is_wsp(c: u8) -> bool { c == b' ' || c == b'\t' } -fn header_end_pos(data: &[u8]) -> Result { +fn header_end_pos(data: &[u8]) -> Result { let mut ret = 0; let max_len = data.len(); loop { ret += data[ret..] .windows(2) .position(|w| w == b"\r\n") - .ok_or(())? + 2; + .ok_or(anyhow!("end of header not found"))? + + 2; if ret >= max_len { return Ok(max_len); } @@ -50,15 +53,16 @@ pub struct ParsedHeader<'a> { } impl<'a> ParsedHeader<'a> { - fn from_bytes(data: &'a [u8]) -> Result { - let colon_pos = data.iter().position(|&w| w == b':').ok_or(())?; + fn from_bytes(data: &'a [u8]) -> Result { + let colon_pos = data + .iter() + .position(|&w| w == b':') + .ok_or(anyhow!("colon not found in header"))?; let name = &data[..colon_pos]; let value = &data[colon_pos + 1..]; Ok(Self { name, - name_lower: String::from_utf8(name.to_vec()) - .map_err(|_| ())? - .to_lowercase(), + name_lower: String::from_utf8(name.to_vec())?.to_lowercase(), value, raw: data, })