Loading codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/error/UnhandledErrorGenerator.kt +22 −18 Original line number Diff line number Diff line Loading @@ -7,8 +7,7 @@ package software.amazon.smithy.rust.codegen.core.smithy.generators.error import software.amazon.smithy.rust.codegen.core.rustlang.RustModule import software.amazon.smithy.rust.codegen.core.rustlang.docs import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType internal fun unhandledError(): RuntimeType = RuntimeType.forInlineFun("Unhandled", RustModule.Error) { Loading @@ -19,23 +18,28 @@ internal fun unhandledError(): RuntimeType = RuntimeType.forInlineFun("Unhandled Call [`Error::source`](std::error::Error::source) for more details about the underlying cause. """, ) rust("##[derive(Debug)]") rustBlock("pub struct Unhandled") { rust("source: Box<dyn #T + Send + Sync + 'static>", RuntimeType.StdError) rustTemplate( """ ##[derive(Debug)] pub struct Unhandled { source: Box<dyn #{StdError} + Send + Sync + 'static>, } rustBlock("impl Unhandled") { rustBlock("pub(crate) fn new(source: Box<dyn #T + Send + Sync + 'static>) -> Self", RuntimeType.StdError) { rust("Self { source }") impl Unhandled { pub(crate) fn new(source: Box<dyn #{StdError} + Send + Sync + 'static>) -> Self { Self { source } } } rustBlock("impl std::fmt::Display for Unhandled") { rustBlock("fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error>") { rust("write!(f, \"unhandled error\")") impl std::fmt::Display for Unhandled { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { write!(f, "unhandled error") } } rustBlock("impl std::error::Error for Unhandled") { rustBlock("fn source(&self) -> Option<&(dyn std::error::Error + 'static)>") { rust("Some(self.source.as_ref() as _)") impl #{StdError} for Unhandled { fn source(&self) -> Option<&(dyn #{StdError} + 'static)> { Some(self.source.as_ref() as _) } } """, "StdError" to RuntimeType.StdError, ) } rust-runtime/aws-smithy-checksums/src/error.rs 0 → 100644 +38 −0 Original line number Diff line number Diff line /* * 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 {} rust-runtime/aws-smithy-checksums/src/lib.rs +11 −28 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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" Loading @@ -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)) } } } Loading Loading @@ -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 Loading Loading @@ -397,10 +376,14 @@ mod tests { } #[test] #[should_panic = "called `Result::unwrap()` on an `Err` value: UnknownChecksumAlgorithm(\"some invalid checksum algorithm\")"] fn test_checksum_algorithm_returns_error_for_unknown() { "some invalid checksum algorithm" let error = "some invalid checksum algorithm" .parse::<ChecksumAlgorithm>() .unwrap(); .err() .expect("it should error"); assert_eq!( "some invalid checksum algorithm", error.checksum_algorithm() ); } } Loading
codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/error/UnhandledErrorGenerator.kt +22 −18 Original line number Diff line number Diff line Loading @@ -7,8 +7,7 @@ package software.amazon.smithy.rust.codegen.core.smithy.generators.error import software.amazon.smithy.rust.codegen.core.rustlang.RustModule import software.amazon.smithy.rust.codegen.core.rustlang.docs import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType internal fun unhandledError(): RuntimeType = RuntimeType.forInlineFun("Unhandled", RustModule.Error) { Loading @@ -19,23 +18,28 @@ internal fun unhandledError(): RuntimeType = RuntimeType.forInlineFun("Unhandled Call [`Error::source`](std::error::Error::source) for more details about the underlying cause. """, ) rust("##[derive(Debug)]") rustBlock("pub struct Unhandled") { rust("source: Box<dyn #T + Send + Sync + 'static>", RuntimeType.StdError) rustTemplate( """ ##[derive(Debug)] pub struct Unhandled { source: Box<dyn #{StdError} + Send + Sync + 'static>, } rustBlock("impl Unhandled") { rustBlock("pub(crate) fn new(source: Box<dyn #T + Send + Sync + 'static>) -> Self", RuntimeType.StdError) { rust("Self { source }") impl Unhandled { pub(crate) fn new(source: Box<dyn #{StdError} + Send + Sync + 'static>) -> Self { Self { source } } } rustBlock("impl std::fmt::Display for Unhandled") { rustBlock("fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error>") { rust("write!(f, \"unhandled error\")") impl std::fmt::Display for Unhandled { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { write!(f, "unhandled error") } } rustBlock("impl std::error::Error for Unhandled") { rustBlock("fn source(&self) -> Option<&(dyn std::error::Error + 'static)>") { rust("Some(self.source.as_ref() as _)") impl #{StdError} for Unhandled { fn source(&self) -> Option<&(dyn #{StdError} + 'static)> { Some(self.source.as_ref() as _) } } """, "StdError" to RuntimeType.StdError, ) }
rust-runtime/aws-smithy-checksums/src/error.rs 0 → 100644 +38 −0 Original line number Diff line number Diff line /* * 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 {}
rust-runtime/aws-smithy-checksums/src/lib.rs +11 −28 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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" Loading @@ -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)) } } } Loading Loading @@ -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 Loading Loading @@ -397,10 +376,14 @@ mod tests { } #[test] #[should_panic = "called `Result::unwrap()` on an `Err` value: UnknownChecksumAlgorithm(\"some invalid checksum algorithm\")"] fn test_checksum_algorithm_returns_error_for_unknown() { "some invalid checksum algorithm" let error = "some invalid checksum algorithm" .parse::<ChecksumAlgorithm>() .unwrap(); .err() .expect("it should error"); assert_eq!( "some invalid checksum algorithm", error.checksum_algorithm() ); } }