scl_lib/api_objects/
error.rs

1// SPDX-License-Identifier: EUPL-1.2
2use std::{error, fmt};
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// The error type for interaction with the api_objects module of the scl-lib.
7#[derive(Debug, Eq, PartialEq)]
8pub enum Error {
9    /// An internal logic bug occurred that should be fixed.
10    Application,
11
12    /// Parsing failed (e.g., invalid chars, value is out of bounds etc.).
13    ParsingFailed(String),
14
15    /// Parsing succeeded but some field (typically resource version or status) has a
16    /// semantically illegal initial state.
17    IllegalInitialValue(String),
18
19    /// Parsing succeeded but an update is semantically illegal.
20    IllegalTransition(TransitionError),
21
22    ValueSpaceExhausted(ValueSpaceExhaustedError),
23}
24
25impl fmt::Display for Error {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        use Error::*;
28        match self {
29            Application => write!(f, "ApplicationError: Internal application error."),
30            ParsingFailed(e) => write!(f, "ParsingError: {}", e),
31            IllegalInitialValue(e) => write!(f, "IllegalInitialValue: {}", e),
32            IllegalTransition(e) => e.fmt(f),
33            ValueSpaceExhausted(e) => e.fmt(f),
34        }
35    }
36}
37
38impl error::Error for Error {}
39
40#[derive(Debug, Eq, PartialEq)]
41pub enum TransitionError {
42    Concurrency,
43    Other(String),
44}
45
46impl fmt::Display for TransitionError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        use TransitionError::*;
49        write!(
50            f,
51            "TransitionError: {}",
52            match self {
53                Concurrency => "Resource version is out of date.",
54                Other(e) => e,
55            }
56        )
57    }
58}
59
60impl error::Error for TransitionError {}
61
62impl From<TransitionError> for Error {
63    fn from(e: TransitionError) -> Self {
64        Self::IllegalTransition(e)
65    }
66}
67
68#[derive(Debug, Eq, PartialEq)]
69pub enum ValueSpaceExhaustedError {
70    NoFreeVlanTagLeft,
71    CannotIncreaseResourceVersion,
72}
73
74impl fmt::Display for ValueSpaceExhaustedError {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        use ValueSpaceExhaustedError::*;
77        write!(
78            f,
79            "ValueSpaceExhaustedError: {}",
80            match self {
81                NoFreeVlanTagLeft => "No free VLAN tag is left",
82                CannotIncreaseResourceVersion => "Cannot increase resource version any higher.",
83            }
84        )
85    }
86}
87
88impl error::Error for ValueSpaceExhaustedError {}
89
90impl From<ValueSpaceExhaustedError> for Error {
91    fn from(e: ValueSpaceExhaustedError) -> Self {
92        Self::ValueSpaceExhausted(e)
93    }
94}