Skip to main content

amplify_num/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2//
3// Written in 2020-2026 by Dr. Maxim Orlovsky <orlovsky@ubideco.org>
4//
5// Copyright 2024-2026 Laboratories for Ubiquitous and Deterministic Computing,
6// Institute for Distributed and Cognitive Computing (InDCS), Switzerland.
7// All rights reserved.
8//
9// Copyright (C) 2020-2025 Dr Maxim Orlovsky.
10// All rights reserved.
11//
12// Licensed under the Apache License, Version 2.0 (the "License"); you may not
13// use this file except in compliance with the License. You may obtain a copy of
14// the License at <http://www.apache.org/licenses/LICENSE-2.0>
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19// License for the specific language governing permissions and limitations under
20// the License.
21
22#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
23/// Error indicating that a value does not fit integer dimension
24pub struct OverflowError<T = usize> {
25    /// Integer bit size
26    pub max: T,
27    /// Value that overflows
28    pub value: T,
29}
30
31impl core::fmt::Display for OverflowError {
32    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33        write!(
34            f,
35            "Unable to construct bit-sized integer from a value `{}` overflowing max value `{}`",
36            self.value, self.max
37        )
38    }
39}
40
41#[cfg(feature = "std")]
42impl std::error::Error for OverflowError {}
43
44#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
45pub enum DivError {
46    ZeroDiv,
47    Overflow,
48}
49
50impl core::fmt::Display for DivError {
51    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
52        match *self {
53            DivError::ZeroDiv => write!(f, "division by zero"),
54            DivError::Overflow => write!(f, "division with overflow"),
55        }
56    }
57}
58
59#[cfg(feature = "std")]
60impl std::error::Error for DivError {}
61
62#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
63pub enum PositDecodeError {
64    Zero,
65    NaR,
66}
67
68impl core::fmt::Display for PositDecodeError {
69    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
70        match *self {
71            PositDecodeError::Zero => write!(f, "exception value zero"),
72            PositDecodeError::NaR => write!(f, "exception value NaR"),
73        }
74    }
75}
76
77#[cfg(feature = "std")]
78impl std::error::Error for PositDecodeError {}
79
80#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
81/// Invalid slice length
82pub struct ParseLengthError {
83    /// The length of the slice de-facto
84    pub actual: usize,
85    /// The required length of the slice
86    pub expected: usize,
87}
88
89impl core::fmt::Display for ParseLengthError {
90    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
91        write!(f, "Invalid length: got {}, expected {}", self.actual, self.expected)
92    }
93}
94#[cfg(feature = "std")]
95impl std::error::Error for ParseLengthError {}