This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
rust-opensmtpd/opensmtpd/src/error.rs
2020-12-20 17:24:08 +01:00

27 lines
636 B
Rust

use nom::error::Error;
use nom::Err;
use pretty_hex::pretty_hex;
fn error_to_string(e: Error<&[u8]>) -> String {
format!(
"parsing error: {:?}: input:{}",
e.code,
get_pretty_hex(&e.input)
)
}
pub(crate) fn get_pretty_hex(input: &[u8]) -> String {
let mut s = String::new();
for l in pretty_hex(&input).split('\n') {
s += &format!("\n{}", l);
}
s
}
pub(crate) fn nom_err_to_string(e: Err<Error<&[u8]>>) -> String {
match e {
Err::Incomplete(_) => e.to_string(),
Err::Error(er) => error_to_string(er),
Err::Failure(er) => error_to_string(er),
}
}