Implement Error (and Display) for nom::Err if std is enabled
@@ -80,6 +80,43 @@ pub enum Err<I, E = u32> {
Failure(Context<I, E>),
}
+#[cfg(feature = "std")]
+use std::fmt;
+
+#[cfg(feature = "std")]
+impl<I, E> fmt::Display for Err<I, E>
+where
+ I: fmt::Debug,
+ E: fmt::Debug,
+{
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{:?}", self)
+ }
+}
+
+#[cfg(feature = "std")]
+use std::error::Error;
+
+#[cfg(feature = "std")]
+impl<I, E> Error for Err<I, E>
+where
+ I: fmt::Debug,
+ E: fmt::Debug,
+{
+ fn description(&self) -> &str {
+ match self {
+ &Err::Incomplete(..) => "there was not enough data",
+ &Err::Error(Context::Code(_, ref error_kind)) | &Err::Failure(Context::Code(_, ref error_kind)) => error_kind.description(),
+ #[cfg(feature = "verbose-errors")]
+ &Err::Error(Context::List(..)) | &Err::Failure(Context::List(..)) => "list of errors",
+ }
+ }
+
+ fn cause(&self) -> Option<&Error> {
+ None
+ }
+}
+
use util::Convert;
impl<I, F, E: From<F>> Convert<Err<I, F>> for Err<I, E> {
