-
Notifications
You must be signed in to change notification settings - Fork 792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support error-chain #581
Comments
after rust-bakery/nom#581 is closed
Hello, nom 4.0 should be easier to use with error-chain, since its main type will be a Result. Please test with the branch named "result" to see if it would work |
I tested it and it seems I can't do the following because the If you want a clonable exemple https://github.com/bbigras/nom-error-chain error_chain! {
foreign_links {
Nom(::nom::Err<&str>);
}
} |
Try derive-error-chain. I don't know how well it handles lifetimes, but it tends to be less temperamental in general. Note, however, that there is talk of it merging with error-chain. |
This issue isn't resolved with derive-error-chain. I ended up creating a new error type and surround every call to a nom parser with a macro that uses .map_err to convert the nom error into the new error type. |
I use failure now. nom doesn't seem to support it and since I'm lazy I use: fn nom_err(e: nom::IError) -> Error {
match e {
nom::IError::Error(e) => err_msg(e.description().to_string()),
nom::IError::Incomplete(_) => err_msg("incomplete"),
}
}
parse_with_nom(&data).to_full_result().map_err(nom_err)?; |
Hello, I have faced with this issue too. I end up with the following errors {
Nom(kind: ::nom::ErrorKind) {
description("parsing error")
display("parsing error: {:?}", kind)
}
}
impl<'a> From<::nom::Err<::nom::types::CompleteStr<'a>>> for Error {
fn from(err: ::nom::Err<::nom::types::CompleteStr<'a>>) -> Error {
let kind = err.into_error_kind();
Error::from_kind(ErrorKind::Nom(kind))
}
} Not sure if it is a right way. |
Mine is more similar to @bbigras' solution: #[derive(Debug, ErrorChain)]
pub enum ErrorKind {
#[error_chain(foreign)]
Nom(NomError)
}
#[derive(Debug)]
pub struct NomError {
desc: String,
}
impl<E: fmt::Debug + Clone> From<nom::Err<E>> for NomError {
fn from(error: nom::Err<E>) -> Self {
let desc = match error {
nom::Err::Incomplete(needed) => format!("ran out of bytes: {:?}", needed),
nom::Err::Error(context) => format!("{:?}", nom::error_to_list(&context)),
nom::Err::Failure(context) => format!("{:?}", nom::error_to_list(&context)),
};
NomError { desc }
}
}
macro_rules! nom {
($expr:expr) => {
$expr.map_err(NomError::from)
}
}
// For example:
let short = nom!(be_u16(bytes))?; |
I think only impl std::fmt::Display and std::error::Error are needed.
To test, this code should build:
currently the output is:
Something like 5bde059 but better.
The text was updated successfully, but these errors were encountered: