Continue to use anyhow
This commit is contained in:
parent
97fae54252
commit
cc2968849d
4 changed files with 24 additions and 18 deletions
|
@ -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<Self, String> {
|
||||
pub fn init() -> Result<Self> {
|
||||
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<PathBuf>) -> Option<PathBuf> {
|
|||
}
|
||||
}
|
||||
|
||||
fn process_domains(lst: &[String], domain_file: &Option<PathBuf>) -> Result<Vec<String>, String> {
|
||||
fn process_domains(lst: &[String], domain_file: &Option<PathBuf>) -> Result<Vec<String>> {
|
||||
let mut domain_set: HashSet<String> = 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());
|
||||
|
|
|
@ -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<Entry, String> {
|
||||
let (_, entry) = parse_entry(input).map_err(|e| format!("parsing error: {e}"))?;
|
||||
fn from_bytes(input: &[u8]) -> Result<Entry> {
|
||||
let (_, entry) = parse_entry(input).map_err(|e| anyhow!("parsing error: {e}"))?;
|
||||
Ok(entry)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn read_entry(reader_lock: Arc<RwLock<StdinReader>>) -> Option<Result<Entry, String>> {
|
||||
pub async fn read_entry(reader_lock: Arc<RwLock<StdinReader>>) -> Option<Result<Entry>> {
|
||||
let mut reader = reader_lock.write().await;
|
||||
log::trace!("reader lock on stdin locked");
|
||||
let line_res = reader.read_line().await;
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
|
||||
pub struct ParsedMessage<'a> {
|
||||
pub headers: Vec<ParsedHeader<'a>>,
|
||||
pub body: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> ParsedMessage<'a> {
|
||||
pub fn from_bytes(data: &'a [u8]) -> Result<Self, ()> {
|
||||
pub fn from_bytes(data: &'a [u8]) -> Result<Self> {
|
||||
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<usize, ()> {
|
||||
fn header_end_pos(data: &[u8]) -> Result<usize> {
|
||||
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<Self, ()> {
|
||||
let colon_pos = data.iter().position(|&w| w == b':').ok_or(())?;
|
||||
fn from_bytes(data: &'a [u8]) -> Result<Self> {
|
||||
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,
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue