Repository: alex/rust-asn1 (language: rust)

Issue:
ParseError does not impl std::error::Error
Error types should implement [std::error::Error](https://doc.rust-lang.org/std/error/trait.Error.html)


Discussion / hints:

Fix patch:
diff --git a/src/parser.rs b/src/parser.rs
index 5bf9117..ad54b63 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -54,6 +54,9 @@ impl ParseError {
     }
 }
 
+#[cfg(feature = "std")]
+impl std::error::Error for ParseError {}
+
 // Wraps an `Option<T>`, but `fmt::Debug` will only render `Some` values and
 // panics on others.
 struct SomeFmtOption<T>(Option<T>);
@@ -96,6 +99,23 @@ impl fmt::Debug for ParseError {
     }
 }
 
+impl fmt::Display for ParseError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "ASN.1 parsing error: ")?;
+        match self.kind {
+            ParseErrorKind::InvalidValue => write!(f, "invalid value"),
+            ParseErrorKind::UnexpectedTag { actual } => {
+                write!(f, "unexpected tag (got {})", actual)
+            }
+            ParseErrorKind::ShortData => write!(f, "short data"),
+            ParseErrorKind::IntegerOverflow => write!(f, "integer overflow"),
+            ParseErrorKind::ExtraData => write!(f, "extra data"),
+            ParseErrorKind::InvalidSetOrdering => write!(f, "SET value was ordered incorrectly"),
+            ParseErrorKind::EncodedDefault => write!(f, "DEFAULT value was explicitly encoded"),
+        }
+    }
+}
+
 /// The result of a `parse`. Either a successful value or a `ParseError`.
 pub type ParseResult<T> = Result<T, ParseError>;
 
@@ -358,6 +378,31 @@ mod tests {
         }
     }
 
+    #[test]
+    fn test_parse_error_display() {
+        for (e, expected) in [
+            (
+                ParseError::new(ParseErrorKind::InvalidValue),
+                "ASN.1 parsing error: invalid value",
+            ),
+            (
+                ParseError::new(ParseErrorKind::ShortData)
+                    .add_location(ParseLocation::Field("Abc::123")),
+                "ASN.1 parsing error: short data",
+            ),
+            (
+                ParseError::new(ParseErrorKind::UnexpectedTag { actual: 12 })
+                    .add_location(ParseLocation::Index(12))
+                    .add_location(ParseLocation::Field("Abc::123")),
+                "ASN.1 parsing error: unexpected tag (got 12)",
+            ),
+        ]
+        .iter()
+        {
+            assert_eq!(&format!("{}", e), expected)
+        }
+    }
+
     fn assert_parses_cb<
         'a,
         T: fmt::Debug + PartialEq,


Test patch: