diff --git a/rust-runtime/aws-smithy-checksums/src/error.rs b/rust-runtime/aws-smithy-checksums/src/error.rs new file mode 100644 index 00000000000..6a93cddbb7b --- /dev/null +++ b/rust-runtime/aws-smithy-checksums/src/error.rs @@ -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) -> 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 {} diff --git a/rust-runtime/aws-smithy-checksums/src/lib.rs b/rust-runtime/aws-smithy-checksums/src/lib.rs index 59a85c397cc..eef3a376a89 100644 --- a/rust-runtime/aws-smithy-checksums/src/lib.rs +++ b/rust-runtime/aws-smithy-checksums/src/lib.rs @@ -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 @@ -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" @@ -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)) } } } @@ -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 @@ -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::()