aws_smithy_types/
error.rs1use 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 OutsideIntegerRange(std::num::TryFromIntError),
20 U64ToFloatLossyConversion(u64),
22 I64ToFloatLossyConversion(i64),
24 F64ToF32LossyConversion(f64),
26 FloatToIntegerLossyConversion(f64),
29 NegativeToUnsignedLossyConversion(i64),
31}
32
33#[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}