Skip to content

Commit

Permalink
Revamp errors in aws-smithy-checksums
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti committed Nov 2, 2022
1 parent bd16b5d commit 55c1535
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
38 changes: 38 additions & 0 deletions rust-runtime/aws-smithy-checksums/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

use std::error::Error;
use std::fmt;

/// A checksum algorithm was unknown
#[derive(Debug)]
pub struct UnknownChecksumAlgorithmError {
checksum_algorithm: String,
}

impl UnknownChecksumAlgorithmError {
pub(crate) fn new(checksum_algorithm: impl Into<String>) -> Self {
Self {
checksum_algorithm: checksum_algorithm.into(),
}
}

/// The checksum algorithm that is unknown
pub fn checksum_algorithm(&self) -> &str {
&self.checksum_algorithm
}
}

impl fmt::Display for UnknownChecksumAlgorithmError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
r#"unknown checksum algorithm "{}", please pass a known algorithm name ("crc32", "crc32c", "sha1", "sha256", "md5")"#,
self.checksum_algorithm
)
}
}

impl Error for UnknownChecksumAlgorithmError {}
31 changes: 5 additions & 26 deletions rust-runtime/aws-smithy-checksums/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

//! Checksum calculation and verification callbacks.
use crate::error::UnknownChecksumAlgorithmError;
use bytes::Bytes;
use std::str::FromStr;

pub mod body;
pub mod error;
pub mod http;

// Valid checksum algorithm names
Expand All @@ -29,7 +31,7 @@ pub enum ChecksumAlgorithm {
}

impl FromStr for ChecksumAlgorithm {
type Err = Error;
type Err = UnknownChecksumAlgorithmError;

/// Create a new `ChecksumAlgorithm` from an algorithm name. Valid algorithm names are:
/// - "crc32"
Expand All @@ -51,9 +53,7 @@ impl FromStr for ChecksumAlgorithm {
} else if checksum_algorithm.eq_ignore_ascii_case(MD5_NAME) {
Ok(Self::Md5)
} else {
Err(Error::UnknownChecksumAlgorithm(
checksum_algorithm.to_owned(),
))
Err(UnknownChecksumAlgorithmError::new(checksum_algorithm))
}
}
}
Expand Down Expand Up @@ -82,27 +82,6 @@ impl ChecksumAlgorithm {
}
}

#[derive(Debug, PartialEq)]
pub enum Error {
UnknownChecksumAlgorithm(String),
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnknownChecksumAlgorithm(algorithm) => {
write!(
f,
r#"unknown checksum algorithm "{}", please pass a known algorithm name ("crc32", "crc32c", "sha1", "sha256", "md5")"#,
algorithm
)
}
}
}
}

impl std::error::Error for Error {}

/// Types implementing this trait can calculate checksums.
///
/// Checksum algorithms are used to validate the integrity of data. Structs that implement this trait
Expand Down Expand Up @@ -397,7 +376,7 @@ mod tests {
}

#[test]
#[should_panic = "called `Result::unwrap()` on an `Err` value: UnknownChecksumAlgorithm(\"some invalid checksum algorithm\")"]
#[should_panic = "called `Result::unwrap()` on an `Err` value: UnknownChecksumAlgorithmError { checksum_algorithm: \"some invalid checksum algorithm\" }"]
fn test_checksum_algorithm_returns_error_for_unknown() {
"some invalid checksum algorithm"
.parse::<ChecksumAlgorithm>()
Expand Down

0 comments on commit 55c1535

Please sign in to comment.