Use thiserror

This commit is contained in:
asonix 2019-12-30 21:13:18 -06:00
parent f491286068
commit 9b8b284bb1
2 changed files with 9 additions and 15 deletions

View file

@ -10,12 +10,12 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
failure = "0.1"
num-bigint = "0.2" num-bigint = "0.2"
num-bigint-dig = "0.4" num-bigint-dig = "0.4"
num-traits = "0.2" num-traits = "0.2"
pem = "0.4" pem = "0.4"
rsa = "0.1.3" rsa = "0.1.3"
thiserror = "1.0.9"
yasna = { version = "0.3", features = ["num-bigint"] } yasna = { version = "0.3", features = ["num-bigint"] }
[dev-dependencies] [dev-dependencies]

View file

@ -1,4 +1,4 @@
use failure::Fail; use thiserror::Error;
use num_bigint_dig::{BigInt, BigUint, Sign}; use num_bigint_dig::{BigInt, BigUint, Sign};
mod private; mod private;
@ -17,21 +17,21 @@ pub trait KeyExt {
Self: Sized; Self: Sized;
} }
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum KeyError { pub enum KeyError {
#[fail(display = "Invalid key kind supplied")] #[error("Invalid key kind supplied")]
Kind, Kind,
#[fail(display = "Key not PEM-formatted")] #[error("Key not PEM-formatted")]
Pem, Pem,
#[fail(display = "Error parsing key, {}", _0)] #[error("Error parsing key, {}", .0)]
Parse(#[cause] yasna::ASN1Error), Parse(#[from] yasna::ASN1Error),
#[fail(display = "Constructed key is invalid")] #[error("Constructed key is invalid")]
Validate, Validate,
#[fail(display = "Could not serialize key")] #[error("Could not serialize key")]
Serialize, Serialize,
} }
@ -56,12 +56,6 @@ fn sign_from_dig(sign: Sign) -> num_bigint::Sign {
} }
} }
impl From<yasna::ASN1Error> for KeyError {
fn from(e: yasna::ASN1Error) -> Self {
KeyError::Parse(e)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::KeyExt; use crate::KeyExt;