Skip to content

Commit c48e11d

Browse files
bsamsethctz
authored andcommitted
Implement Error for graviola::Error
This makes it possible to use the `?` operator with `graviola::Error` in application code that uses e.g. `anyhow`. The required implementations of `core::fmt::Display` are simply based on the docs for each variant, converted to the standard lowercase, non-punctuated style. This currently used `std::error::Error` instead of `core::error::Error` because the latter would mean a `rust-version` bump up to 1.81. Depending on what happens with #57, this could perhaps use some feature flag(s) to conditionally use `std` or `core`. Or hide the entire trait impl behind a feature flag.
1 parent 0110c6b commit c48e11d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

graviola/src/error.rs

+37
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,40 @@ impl From<KeyFormatError> for Error {
4848
Self::KeyFormatError(kfe)
4949
}
5050
}
51+
52+
impl core::fmt::Display for KeyFormatError {
53+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
54+
match self {
55+
Self::UnsupportedPkcs8Version => write!(f, "unsupported PKCS#8 version"),
56+
Self::MismatchedPkcs8Algorithm => write!(f, "mismatched PKCS#8 algorithm"),
57+
Self::MismatchedPkcs8Parameters => write!(f, "mismatched PKCS#8 parameters"),
58+
Self::MismatchedSec1Curve => write!(f, "mismatched SEC1 curve"),
59+
Self::MismatchedSec1PublicKey => write!(f, "mismatched SEC1 public key"),
60+
}
61+
}
62+
}
63+
64+
impl core::fmt::Display for Error {
65+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
66+
match self {
67+
Self::WrongLength => write!(f, "some slice was the wrong length"),
68+
Self::NotUncompressed => write!(
69+
f,
70+
"a compressed elliptic curve point encoding was encountered"
71+
),
72+
Self::NotOnCurve => write!(f, "a public key was invalid"),
73+
Self::OutOfRange => write!(f, "a value was too small or large"),
74+
Self::RngFailed => write!(
75+
f,
76+
"a random number generator returned an error or fixed values"
77+
),
78+
Self::BadSignature => write!(f, "presented signature is invalid"),
79+
Self::DecryptFailed => write!(f, "presented AEAD tag/aad/ciphertext/nonce was wrong"),
80+
Self::Asn1Error(e) => write!(f, "an ASN.1 encoding/decoding error: {e}"),
81+
Self::KeyFormatError(e) => write!(f, "a key formatting/validation error: {e}"),
82+
}
83+
}
84+
}
85+
86+
impl std::error::Error for KeyFormatError {}
87+
impl std::error::Error for Error {}

graviola/src/high/asn1.rs

+19
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,25 @@ pub enum Error {
755755
UnhandledBitString,
756756
}
757757

758+
impl core::fmt::Display for Error {
759+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
760+
match self {
761+
Error::UnexpectedTag => write!(f, "unexpected tag"),
762+
Error::UnexpectedEof => write!(f, "unexpected end of input"),
763+
Error::UnexpectedTrailingData => write!(f, "unexpected trailing data"),
764+
Error::NonCanonicalEncoding => write!(f, "non-canonical encoding"),
765+
Error::UnhandledEnumValue => write!(f, "unhandled enum value"),
766+
Error::IntegerOutOfRange => write!(f, "integer out of range"),
767+
Error::IllegalNull => write!(f, "illegal null"),
768+
Error::UnsupportedLargeObjectId => write!(f, "unsupported large object identifier"),
769+
Error::UnsupportedLargeObjectLength => write!(f, "unsupported large object length"),
770+
Error::UnhandledBitString => write!(f, "unhandled bit string"),
771+
}
772+
}
773+
}
774+
775+
impl std::error::Error for Error {}
776+
758777
#[derive(Clone, Copy, Debug)]
759778
pub(crate) struct Tag(u8);
760779

0 commit comments

Comments
 (0)