Skip to content

Commit

Permalink
Mark error enums as non-exhaustive (#1227)
Browse files Browse the repository at this point in the history
* Make all error enums non-exhaustive

* Make more enums non-exhaustive

* Feature-gate method used in tests and memstore

* Address new clippy lints
  • Loading branch information
PhilippGackstatter authored Aug 29, 2023
1 parent 6fb2df4 commit 4b5ff05
Show file tree
Hide file tree
Showing 15 changed files with 18 additions and 18 deletions.
8 changes: 0 additions & 8 deletions bindings/wasm/src/iota/iota_metadata_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ pub enum WasmStateMetadataEncoding {
Json = 0,
}

impl From<StateMetadataEncoding> for WasmStateMetadataEncoding {
fn from(encoding: StateMetadataEncoding) -> Self {
match encoding {
StateMetadataEncoding::Json => Self::Json,
}
}
}

impl From<WasmStateMetadataEncoding> for StateMetadataEncoding {
fn from(encoding: WasmStateMetadataEncoding) -> Self {
match encoding {
Expand Down
4 changes: 2 additions & 2 deletions identity_core/src/common/one_or_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ where
OneOrSetInner::One(inner) if inner.key() == item.key() => false,
OneOrSetInner::One(_) => match replace(&mut self.0, OneOrSetInner::Set(OrderedSet::new())) {
OneOrSetInner::One(inner) => {
self.0 = OneOrSetInner::Set(OrderedSet::from_iter([inner, item].into_iter()));
self.0 = OneOrSetInner::Set(OrderedSet::from_iter([inner, item]));
true
}
OneOrSetInner::Set(_) => unreachable!(),
Expand Down Expand Up @@ -348,7 +348,7 @@ mod tests {
#[test]
fn test_new_set() {
// VALID: non-empty set.
let ordered_set: OrderedSet<MockKeyU8> = OrderedSet::from_iter([1, 2, 3].map(MockKeyU8).into_iter());
let ordered_set: OrderedSet<MockKeyU8> = OrderedSet::from_iter([1, 2, 3].map(MockKeyU8));
let new_set: OneOrSet<MockKeyU8> = OneOrSet::new_set(ordered_set.clone()).unwrap();
let try_from_set: OneOrSet<MockKeyU8> = OneOrSet::try_from(ordered_set.clone()).unwrap();
assert_eq!(new_set, try_from_set);
Expand Down
2 changes: 1 addition & 1 deletion identity_core/src/convert/base_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ mod tests {
#[test]
fn test_multibase() {
// Encode.
let data: &str = r#"Multibase is awesome! \o/"#;
let data: &str = r"Multibase is awesome! \o/";
for (base, expected) in [
(Base::Base16Upper, "F4D756C74696261736520697320617765736F6D6521205C6F2F"),
(Base::Base32Upper, "BJV2WY5DJMJQXGZJANFZSAYLXMVZW63LFEEQFY3ZP"),
Expand Down
4 changes: 2 additions & 2 deletions identity_credential/src/domain_linkage/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::error::Error;

pub(crate) type DomainLinkageValidationResult = Result<(), DomainLinkageValidationError>;

#[derive(Debug, thiserror::Error)]
/// An error caused by a failure to verify a Domain Linkage configuration or credential.
#[derive(Debug, thiserror::Error)]
pub struct DomainLinkageValidationError {
/// Cause of the error.
pub cause: DomainLinkageValidationErrorCause,
Expand All @@ -27,8 +27,8 @@ impl From<DomainLinkageValidationError> for &str {
}

/// The causes for why domain linkage validation can fail.
#[non_exhaustive]
#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
#[non_exhaustive]
pub enum DomainLinkageValidationErrorCause {
/// Caused when a Domain Linkage Credential cannot be successfully validated.
#[error("invalid credential")]
Expand Down
1 change: 1 addition & 0 deletions identity_credential/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub type Result<T, E = Error> = ::core::result::Result<T, E>;

/// This type represents errors that can occur when constructing credentials and presentations or their serializations.
#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
#[non_exhaustive]
pub enum Error {
/// Caused when constructing a credential or presentation without a valid base context.
#[error("missing base context")]
Expand Down
1 change: 1 addition & 0 deletions identity_credential/src/revocation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub type RevocationResult<T> = std::result::Result<T, RevocationError>;

/// Errors occurring when creating or extracting a Service of type `RevocationBitmap2022`
#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
#[non_exhaustive]
pub enum RevocationError {
#[error("revocation bitmap could not be deserialized or decompressed")]
/// Indicates that the bitmap could not be reconstructed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::fmt::Display;

use itertools;

/// An error associated with validating credentials and presentations.
#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
#[non_exhaustive]
/// An error associated with validating credentials and presentations.
pub enum JwtValidationError {
/// Indicates that the JWS representation of an issued credential or presentation could not be decoded.
#[error("could not decode jws")]
Expand Down
1 change: 1 addition & 0 deletions identity_document/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub type Result<T, E = Error> = ::core::result::Result<T, E>;

/// This type represents all possible errors that can occur in the library.
#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
#[non_exhaustive]
pub enum Error {
/// Caused by querying for a method that does not exist.
#[error("verification method not found")]
Expand Down
1 change: 1 addition & 0 deletions identity_iota_core/src/state_metadata/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::Error;

/// Indicates the encoding of a DID document in state metadata.
#[derive(Copy, Clone, Debug, Default, Ord, PartialOrd, Eq, PartialEq, num_derive::FromPrimitive)]
#[non_exhaustive]
pub enum StateMetadataEncoding {
/// State Metadata encoded as JSON.
#[default]
Expand Down
1 change: 1 addition & 0 deletions identity_iota_core/src/state_metadata/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::Error;

/// Indicates the version of a DID document in state metadata.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, num_derive::FromPrimitive)]
#[non_exhaustive]
pub(crate) enum StateMetadataVersion {
V1 = 1,
}
Expand Down
1 change: 1 addition & 0 deletions identity_jose/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub type Result<T, E = Error> = core::result::Result<T, E>;

/// All possible errors that can occur in the library.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// Caused by invalid json serialization or deserialization.
#[error("invalid json")]
Expand Down
4 changes: 2 additions & 2 deletions identity_jose/src/tests/es256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub(crate) fn expand_p256_jwk(jwk: &Jwk) -> (SecretKey, PublicKey) {
// Transformation according to section 2.3.3 from http://www.secg.org/sec1-v2.pdf.
let pk_bytes: Vec<u8> = [0x04]
.into_iter()
.chain(jwu::decode_b64(&params.x).unwrap().into_iter())
.chain(jwu::decode_b64(&params.y).unwrap().into_iter())
.chain(jwu::decode_b64(&params.x).unwrap())
.chain(jwu::decode_b64(&params.y).unwrap())
.collect();

let pk = PublicKey::from_sec1_bytes(&pk_bytes).unwrap();
Expand Down
1 change: 1 addition & 0 deletions identity_storage/src/key_id_storage/method_digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub type MethodDigestConstructionError = identity_core::common::SingleStructErro

/// Characterization of the underlying cause of a [`MethodDigestConstructionError`].
#[derive(Debug)]
#[non_exhaustive]
pub enum MethodDigestConstructionErrorKind {
/// Caused by a missing id on a verification method.
///
Expand Down
4 changes: 2 additions & 2 deletions identity_storage/src/key_storage/ed25519.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crypto::signatures::ed25519::PublicKey;
use crypto::signatures::ed25519::SecretKey;
use identity_verification::jose::jwk::EdCurve;
use identity_verification::jose::jwk::Jwk;
Expand Down Expand Up @@ -47,7 +46,8 @@ pub(crate) fn expand_secret_jwk(jwk: &Jwk) -> KeyStorageResult<SecretKey> {
Ok(SecretKey::from_bytes(&sk))
}

pub(crate) fn encode_jwk(private_key: &SecretKey, public_key: &PublicKey) -> Jwk {
#[cfg(any(test, feature = "memstore"))]
pub(crate) fn encode_jwk(private_key: &SecretKey, public_key: &crypto::signatures::ed25519::PublicKey) -> Jwk {
let x = jwu::encode_b64(public_key.as_ref());
let d = jwu::encode_b64(private_key.to_bytes().as_ref());
let mut params = JwkParamsOkp::new();
Expand Down
1 change: 1 addition & 0 deletions identity_verification/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub type Result<T, E = Error> = ::core::result::Result<T, E>;

/// This type represents all possible errors that can occur in the crate.
#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
#[non_exhaustive]
pub enum Error {
/// Caused by invalid or missing properties when constructing a
/// [`VerificationMethod`](crate::VerificationMethod).
Expand Down

0 comments on commit 4b5ff05

Please sign in to comment.