From 8d15c3d0f9d2291db59a2c2e5fa50f23e1e76154 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 11 Feb 2022 23:55:04 +0100 Subject: [PATCH] signatures: Merge SplitError into Error --- crates/ruma-signatures/CHANGELOG.md | 4 ++++ crates/ruma-signatures/src/error.rs | 31 ++++++++++------------------- crates/ruma-signatures/src/lib.rs | 10 +++++----- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/crates/ruma-signatures/CHANGELOG.md b/crates/ruma-signatures/CHANGELOG.md index f8f370e6..df06a116 100644 --- a/crates/ruma-signatures/CHANGELOG.md +++ b/crates/ruma-signatures/CHANGELOG.md @@ -1,5 +1,9 @@ # [unreleased] +Breaking changes: + +* Merge `SplitError` into `Error` + Improvements: * Move Room Version 9 keys out of `unstable-pre-spec` in `allowed_content_keys_for` diff --git a/crates/ruma-signatures/src/error.rs b/crates/ruma-signatures/src/error.rs index 33e5b04b..0efdf698 100644 --- a/crates/ruma-signatures/src/error.rs +++ b/crates/ruma-signatures/src/error.rs @@ -22,9 +22,17 @@ pub enum Error { #[error("DER Parse error: {0}")] DerParse(pkcs8::der::Error), - /// [`SplitError`] wrapper. - #[error("Split error: {0}")] - SplitError(#[from] SplitError), + /// The signature's ID does not have exactly two components separated by a colon. + #[error("malformed signature ID: expected exactly 2 segment separated by a colon, found {0}")] + InvalidLength(usize), + + /// The signature's ID contains invalid characters in its version. + #[error("malformed signature ID: expected version to contain only characters in the character set `[a-zA-Z0-9_]`, found `{0}`")] + InvalidVersion(String), + + /// The signature uses an unsupported algorithm. + #[error("signature uses an unsupported algorithm: {0}")] + UnsupportedAlgorithm(String), /// PDU was too large #[error("PDU is larger than maximum of 65535 bytes")] @@ -251,20 +259,3 @@ impl ParseError { Self::Base64 { of_type: of_type.into(), string: string.into(), source }.into() } } - -/// An error when trying to extract the algorithm and version from a key identifier. -#[derive(Error, Debug)] -#[non_exhaustive] -pub enum SplitError { - /// The signature's ID does not have exactly two components separated by a colon. - #[error("malformed signature ID: expected exactly 2 segment separated by a colon, found {0}")] - InvalidLength(usize), - - /// The signature's ID contains invalid characters in its version. - #[error("malformed signature ID: expected version to contain only characters in the character set `[a-zA-Z0-9_]`, found `{0}`")] - InvalidVersion(String), - - /// The signature uses an unsupported algorithm. - #[error("unsupported algorithm: {0}")] - UnsupportedAlgorithm(String), -} diff --git a/crates/ruma-signatures/src/lib.rs b/crates/ruma-signatures/src/lib.rs index f134f097..df04765c 100644 --- a/crates/ruma-signatures/src/lib.rs +++ b/crates/ruma-signatures/src/lib.rs @@ -46,7 +46,7 @@ use ruma_serde::{AsRefStr, DisplayAsRefStr}; -pub use error::{Error, JsonError, JsonType, ParseError, SplitError, VerificationError}; +pub use error::{Error, JsonError, JsonType, ParseError, VerificationError}; pub use functions::{ canonical_json, content_hash, hash_and_sign_event, redact, redact_content_in_place, redact_in_place, reference_hash, sign_json, verify_event, verify_json, @@ -72,7 +72,7 @@ pub enum Algorithm { } /// Extract the algorithm and version from a key identifier. -fn split_id(id: &str) -> Result<(Algorithm, String), SplitError> { +fn split_id(id: &str) -> Result<(Algorithm, String), Error> { /// The length of a valid signature ID. const SIGNATURE_ID_LENGTH: usize = 2; @@ -81,20 +81,20 @@ fn split_id(id: &str) -> Result<(Algorithm, String), SplitError> { let signature_id_length = signature_id.len(); if signature_id_length != SIGNATURE_ID_LENGTH { - return Err(SplitError::InvalidLength(signature_id_length)); + return Err(Error::InvalidLength(signature_id_length)); } let version = signature_id[1]; if !version.bytes().all(|ch| ch.is_ascii_alphanumeric() || ch == b'_') { - return Err(SplitError::InvalidVersion(version.into())); + return Err(Error::InvalidVersion(version.into())); } let algorithm_input = signature_id[0]; let algorithm = match algorithm_input { "ed25519" => Algorithm::Ed25519, - algorithm => return Err(SplitError::UnsupportedAlgorithm(algorithm.into())), + algorithm => return Err(Error::UnsupportedAlgorithm(algorithm.into())), }; Ok((algorithm, signature_id[1].to_owned()))