dec/
error.rs

1// Copyright Materialize, Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use std::error::Error;
17use std::fmt;
18
19/// An error indicating that a string is not a valid decimal number.
20#[derive(Debug, Eq, PartialEq)]
21pub struct ParseDecimalError;
22
23impl fmt::Display for ParseDecimalError {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        f.write_str("invalid decimal syntax")
26    }
27}
28
29impl Error for ParseDecimalError {}
30
31/// An error indicating that a precision is not valid for a given context.
32#[derive(Debug, Eq, PartialEq)]
33pub struct InvalidPrecisionError;
34
35impl fmt::Display for InvalidPrecisionError {
36    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37        f.write_str("invalid decimal precision")
38    }
39}
40
41impl Error for InvalidPrecisionError {}
42
43/// An error indicating that a minimum exponent or maximum exponent is not valid
44/// for a given context.
45#[derive(Debug, Eq, PartialEq)]
46pub struct InvalidExponentError;
47
48impl fmt::Display for InvalidExponentError {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        f.write_str("invalid decimal exponent")
51    }
52}
53
54impl Error for InvalidExponentError {}
55
56/// An error indicating that a value cannot be cast to a primitive type.
57///
58/// Causes for this failure include calling cast functions on values:
59/// - Representing infinity or NaN
60/// - With non-zero exponent values
61/// - Whose coefficient doesn't fit into the target, e.g. values that require
62///   too many digits of precision.
63#[derive(Debug, Eq, PartialEq)]
64pub struct TryFromDecimalError;
65
66impl fmt::Display for TryFromDecimalError {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        f.write_str("decimal cannot be expressed in target primitive type")
69    }
70}
71
72impl Error for TryFromDecimalError {}
73
74/// An error indicating a value cannot be precisely cast to a Decimal value, e.g.
75/// the cast requires truncating significant digits.
76#[derive(Debug, Eq, PartialEq)]
77pub struct TryIntoDecimalError;
78
79impl fmt::Display for TryIntoDecimalError {
80    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81        f.write_str("value cannot be precisely expressed as decimal")
82    }
83}
84
85impl Error for TryIntoDecimalError {}
86
87/// An error indicating that a value's coefficient cannot be cast to a primitive
88/// type.
89#[derive(Debug, Eq, PartialEq)]
90pub struct InvalidCoefficientError;
91
92impl fmt::Display for InvalidCoefficientError {
93    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94        f.write_str("decimal's coefficient cannot be expressed in target primitive type")
95    }
96}
97
98impl Error for InvalidCoefficientError {}
99
100/// An error indicating that a value's coefficient cannot be cast to a primitive
101/// type.
102#[derive(Debug, Eq, PartialEq)]
103pub struct FromBcdError;
104
105impl fmt::Display for FromBcdError {
106    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107        f.write_str("bcd or scale value invalid, either corrupted or out of acceptable range")
108    }
109}
110
111impl Error for FromBcdError {}