aws_smithy_types/
error.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Errors for Smithy codegen
7
8use std::fmt;
9
10pub mod display;
11pub mod metadata;
12pub mod operation;
13
14pub use metadata::ErrorMetadata;
15
16#[derive(Debug)]
17pub(super) enum TryFromNumberErrorKind {
18    /// Used when the conversion from an integer type into a smaller integer type would be lossy.
19    OutsideIntegerRange(std::num::TryFromIntError),
20    /// Used when the conversion from an `u64` into a floating point type would be lossy.
21    U64ToFloatLossyConversion(u64),
22    /// Used when the conversion from an `i64` into a floating point type would be lossy.
23    I64ToFloatLossyConversion(i64),
24    /// Used when attempting to convert an `f64` into an `f32`.
25    F64ToF32LossyConversion(f64),
26    /// Used when attempting to convert a decimal, infinite, or `NaN` floating point type into an
27    /// integer type.
28    FloatToIntegerLossyConversion(f64),
29    /// Used when attempting to convert a negative [`Number`](crate::Number) into an unsigned integer type.
30    NegativeToUnsignedLossyConversion(i64),
31}
32
33/// The error type returned when conversion into an integer type or floating point type is lossy.
34#[derive(Debug)]
35pub struct TryFromNumberError {
36    pub(super) kind: TryFromNumberErrorKind,
37}
38
39impl fmt::Display for TryFromNumberError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        use TryFromNumberErrorKind::*;
42        match self.kind {
43            OutsideIntegerRange(_) => write!(f, "integer too large"),
44            FloatToIntegerLossyConversion(v) => write!(
45                f,
46                "cannot convert floating point number {v} into an integer"
47            ),
48            NegativeToUnsignedLossyConversion(v) => write!(
49                f,
50                "cannot convert negative integer {v} into an unsigned integer type"
51            ),
52            U64ToFloatLossyConversion(v) => {
53                write!(
54                    f,
55                    "cannot convert {v}u64 into a floating point type without precision loss"
56                )
57            }
58            I64ToFloatLossyConversion(v) => {
59                write!(
60                    f,
61                    "cannot convert {v}i64 into a floating point type without precision loss"
62                )
63            }
64            F64ToF32LossyConversion(v) => {
65                write!(f, "will not attempt to convert {v}f64 into a f32")
66            }
67        }
68    }
69}
70
71impl std::error::Error for TryFromNumberError {
72    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
73        use TryFromNumberErrorKind::*;
74        match &self.kind {
75            OutsideIntegerRange(err) => Some(err as _),
76            FloatToIntegerLossyConversion(_)
77            | NegativeToUnsignedLossyConversion(_)
78            | U64ToFloatLossyConversion(_)
79            | I64ToFloatLossyConversion(_)
80            | F64ToF32LossyConversion(_) => None,
81        }
82    }
83}
84
85impl From<std::num::TryFromIntError> for TryFromNumberError {
86    fn from(value: std::num::TryFromIntError) -> Self {
87        Self {
88            kind: TryFromNumberErrorKind::OutsideIntegerRange(value),
89        }
90    }
91}
92
93impl From<TryFromNumberErrorKind> for TryFromNumberError {
94    fn from(kind: TryFromNumberErrorKind) -> Self {
95        Self { kind }
96    }
97}