aws_smithy_types/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Errors for Smithy codegen

use std::fmt;

pub mod display;
pub mod metadata;
pub mod operation;

pub use metadata::ErrorMetadata;

#[derive(Debug)]
pub(super) enum TryFromNumberErrorKind {
    /// Used when the conversion from an integer type into a smaller integer type would be lossy.
    OutsideIntegerRange(std::num::TryFromIntError),
    /// Used when the conversion from an `u64` into a floating point type would be lossy.
    U64ToFloatLossyConversion(u64),
    /// Used when the conversion from an `i64` into a floating point type would be lossy.
    I64ToFloatLossyConversion(i64),
    /// Used when attempting to convert an `f64` into an `f32`.
    F64ToF32LossyConversion(f64),
    /// Used when attempting to convert a decimal, infinite, or `NaN` floating point type into an
    /// integer type.
    FloatToIntegerLossyConversion(f64),
    /// Used when attempting to convert a negative [`Number`](crate::Number) into an unsigned integer type.
    NegativeToUnsignedLossyConversion(i64),
}

/// The error type returned when conversion into an integer type or floating point type is lossy.
#[derive(Debug)]
pub struct TryFromNumberError {
    pub(super) kind: TryFromNumberErrorKind,
}

impl fmt::Display for TryFromNumberError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use TryFromNumberErrorKind::*;
        match self.kind {
            OutsideIntegerRange(_) => write!(f, "integer too large"),
            FloatToIntegerLossyConversion(v) => write!(
                f,
                "cannot convert floating point number {v} into an integer"
            ),
            NegativeToUnsignedLossyConversion(v) => write!(
                f,
                "cannot convert negative integer {v} into an unsigned integer type"
            ),
            U64ToFloatLossyConversion(v) => {
                write!(
                    f,
                    "cannot convert {v}u64 into a floating point type without precision loss"
                )
            }
            I64ToFloatLossyConversion(v) => {
                write!(
                    f,
                    "cannot convert {v}i64 into a floating point type without precision loss"
                )
            }
            F64ToF32LossyConversion(v) => {
                write!(f, "will not attempt to convert {v}f64 into a f32")
            }
        }
    }
}

impl std::error::Error for TryFromNumberError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        use TryFromNumberErrorKind::*;
        match &self.kind {
            OutsideIntegerRange(err) => Some(err as _),
            FloatToIntegerLossyConversion(_)
            | NegativeToUnsignedLossyConversion(_)
            | U64ToFloatLossyConversion(_)
            | I64ToFloatLossyConversion(_)
            | F64ToF32LossyConversion(_) => None,
        }
    }
}

impl From<std::num::TryFromIntError> for TryFromNumberError {
    fn from(value: std::num::TryFromIntError) -> Self {
        Self {
            kind: TryFromNumberErrorKind::OutsideIntegerRange(value),
        }
    }
}

impl From<TryFromNumberErrorKind> for TryFromNumberError {
    fn from(kind: TryFromNumberErrorKind) -> Self {
        Self { kind }
    }
}