From bdb8f7f8aabf49c07466613840c8ae70923f5e8a Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Fri, 26 Oct 2018 09:25:05 -0700 Subject: [PATCH] Make ASN1EncodeErr an instance of std::Error. --- src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 656e8e3..56db616 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,8 @@ extern crate rand; use chrono::{DateTime,TimeZone,Utc}; use num::{BigInt,BigUint,FromPrimitive,One,ToPrimitive,Zero}; +use std::error::Error; +use std::fmt; use std::iter::FromIterator; use std::mem::size_of; @@ -258,6 +260,33 @@ pub enum ASN1EncodeErr { ObjectIdentVal2TooLarge } +impl fmt::Display for ASN1EncodeErr { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(self.description()) + } +} + +impl Error for ASN1EncodeErr { + fn description(&self) -> &str { + match self { + ASN1EncodeErr::ObjectIdentHasTooFewFields => + "ASN1 object identifier has too few fields.", + ASN1EncodeErr::ObjectIdentVal1TooLarge => + "First value in ASN1 OID is too big.", + ASN1EncodeErr::ObjectIdentVal2TooLarge => + "Second value in ASN1 OID is too big." + } + } + + fn cause(&self) -> Option<&Error> { + None + } + + fn source(&self) -> Option<&(Error + 'static)> { + None + } +} + /// Translate a binary blob into a series of `ASN1Block`s, or provide an /// error if it didn't work. pub fn from_der(i: &[u8]) -> Result,ASN1DecodeErr> {