Skip to content

Commit

Permalink
Revert "der: add Error::(set_)source; remove Clone + Copy (#1328)…
Browse files Browse the repository at this point in the history
…" (#1371)

This reverts commit 7784f2d.

This ended up complicating downstream error types, particularly in
`no_std` contexts.

It's also not really necessary now that #1055 has landed.
  • Loading branch information
tarcieri authored Mar 28, 2024
1 parent 6514e64 commit 64ca37f
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 95 deletions.
56 changes: 7 additions & 49 deletions der/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,17 @@ use crate::asn1::ObjectIdentifier;
#[cfg(feature = "pem")]
use crate::pem;

#[cfg(feature = "std")]
use alloc::boxed::Box;

/// Result type.
pub type Result<T> = core::result::Result<T, Error>;

/// Error type.
#[derive(Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Error {
/// Kind of error.
kind: ErrorKind,

/// Position inside of message where error occurred.
position: Option<Length>,

/// Source of the error.
#[cfg(feature = "std")]
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
}

impl Error {
Expand All @@ -37,8 +30,6 @@ impl Error {
Error {
kind,
position: Some(position),
#[cfg(feature = "std")]
source: None,
}
}

Expand All @@ -57,27 +48,15 @@ impl Error {
}

/// Get the [`ErrorKind`] which occurred.
pub fn kind(&self) -> ErrorKind {
pub fn kind(self) -> ErrorKind {
self.kind
}

/// Get the position inside of the message where the error occurred.
pub fn position(&self) -> Option<Length> {
pub fn position(self) -> Option<Length> {
self.position
}

/// Set the source of this error. Useful for e.g. propagating an additional error with
/// [`ErrorKind::Value`].
///
/// Overwrites any previously set source.
#[cfg(feature = "std")]
pub fn set_source<E>(&mut self, source: E)
where
E: std::error::Error + Send + Sync + 'static,
{
self.source = Some(Box::new(source));
}

/// For errors occurring inside of a nested message, extend the position
/// count by the location where the nested message occurs.
pub(crate) fn nested(self, nested_position: Length) -> Self {
Expand All @@ -87,12 +66,13 @@ impl Error {
Self {
kind: self.kind,
position,
#[cfg(feature = "std")]
source: self.source,
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.kind)?;
Expand All @@ -105,20 +85,11 @@ impl fmt::Display for Error {
}
}

impl Eq for Error {}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind && self.position == other.position
}
}

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error {
kind,
position: None,
#[cfg(feature = "std")]
source: None,
}
}
}
Expand All @@ -130,12 +101,10 @@ impl From<Infallible> for Error {
}

impl From<TryFromIntError> for Error {
fn from(_err: TryFromIntError) -> Error {
fn from(_: TryFromIntError) -> Error {
Error {
kind: ErrorKind::Overflow,
position: None,
#[cfg(feature = "std")]
source: Some(Box::new(_err)),
}
}
}
Expand All @@ -145,8 +114,6 @@ impl From<Utf8Error> for Error {
Error {
kind: ErrorKind::Utf8(err),
position: None,
#[cfg(feature = "std")]
source: Some(Box::new(err)),
}
}
}
Expand Down Expand Up @@ -191,15 +158,6 @@ impl From<time::error::ComponentRange> for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_ref()
.map(|source| source.as_ref() as &(dyn std::error::Error + 'static))
}
}

/// Error type.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
Expand Down
13 changes: 2 additions & 11 deletions pkcs1/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use der::pem;
pub type Result<T> = core::result::Result<T, Error>;

/// Error type
#[derive(Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
/// ASN.1 DER-related errors.
Expand Down Expand Up @@ -92,13 +92,4 @@ impl From<pkcs8::spki::Error> for Error {
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Asn1(err) => Some(err),
#[cfg(feature = "pkcs8")]
Error::Pkcs8(err) => Some(err),
_ => None,
}
}
}
impl std::error::Error for Error {}
2 changes: 0 additions & 2 deletions pkcs5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ hex-literal = "0.4"

[features]
alloc = []
std = ["alloc"]

3des = ["dep:des", "pbes2"]
des-insecure = ["dep:des", "pbes2"]
getrandom = ["rand_core/getrandom"]
Expand Down
3 changes: 0 additions & 3 deletions pkcs5/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,3 @@ impl fmt::Display for Error {
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
3 changes: 0 additions & 3 deletions pkcs5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#[cfg(all(feature = "alloc", feature = "pbes2"))]
extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

mod error;

pub mod pbes1;
Expand Down
2 changes: 1 addition & 1 deletion pkcs8/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tempfile = "3"

[features]
alloc = ["der/alloc", "der/zeroize", "spki/alloc"]
std = ["alloc", "der/std", "pkcs5?/std", "spki/std"]
std = ["alloc", "der/std", "spki/std"]

3des = ["encryption", "pkcs5/3des"]
des-insecure = ["encryption", "pkcs5/des-insecure"]
Expand Down
14 changes: 2 additions & 12 deletions pkcs8/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use der::pem;
pub type Result<T> = core::result::Result<T, Error>;

/// Error type
#[derive(Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
/// ASN.1 DER-related errors.
Expand Down Expand Up @@ -48,17 +48,7 @@ impl fmt::Display for Error {
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Asn1(err) => Some(err),
#[cfg(feature = "pkcs5")]
Error::EncryptedPrivateKey(err) => Some(err),
Error::PublicKey(err) => Some(err),
_ => None,
}
}
}
impl std::error::Error for Error {}

impl From<der::Error> for Error {
fn from(err: der::Error) -> Error {
Expand Down
5 changes: 1 addition & 4 deletions pkcs8/tests/encrypted_private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
#![cfg(feature = "pkcs5")]

use hex_literal::hex;
use pkcs8::{pkcs5::pbes2, EncryptedPrivateKeyInfo};
use pkcs8::{pkcs5::pbes2, EncryptedPrivateKeyInfo, PrivateKeyInfo};

#[cfg(feature = "alloc")]
use der::Encode;

#[cfg(feature = "encryption")]
use pkcs8::PrivateKeyInfo;

#[cfg(feature = "pem")]
use der::EncodePem;

Expand Down
2 changes: 1 addition & 1 deletion sec1/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use der::pem;
pub type Result<T> = core::result::Result<T, Error>;

/// Error type
#[derive(Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
/// ASN.1 DER-related errors.
Expand Down
11 changes: 2 additions & 9 deletions spki/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub type Result<T> = core::result::Result<T, Error>;
use der::pem;

/// Error type
#[derive(Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
/// Algorithm parameters are missing.
Expand Down Expand Up @@ -65,11 +65,4 @@ impl From<pem::Error> for Error {
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Asn1(err) => Some(err),
_ => None,
}
}
}
impl std::error::Error for Error {}

0 comments on commit 64ca37f

Please sign in to comment.