rustworkx_core/
err.rs

1// Licensed under the Apache License, Version 2.0 (the "License"); you may
2// not use this file except in compliance with the License. You may obtain
3// a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10// License for the specific language governing permissions and limitations
11// under the License.
12
13//! This module contains common error types and trait impls.
14
15use std::error::Error;
16use std::fmt::{Debug, Display, Formatter};
17
18#[derive(Debug)]
19pub enum ContractError {
20    DAGWouldCycle,
21}
22
23impl Display for ContractError {
24    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25        match self {
26            ContractError::DAGWouldCycle => fmt_dag_would_cycle(f),
27        }
28    }
29}
30
31impl Error for ContractError {}
32
33#[derive(Debug)]
34pub enum ContractSimpleError<E: Error> {
35    DAGWouldCycle,
36    MergeError(E),
37}
38
39impl<E: Error> Display for ContractSimpleError<E> {
40    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41        match self {
42            ContractSimpleError::DAGWouldCycle => fmt_dag_would_cycle(f),
43            ContractSimpleError::MergeError(ref e) => fmt_merge_error(f, e),
44        }
45    }
46}
47
48impl<E: Error> Error for ContractSimpleError<E> {}
49
50fn fmt_dag_would_cycle(f: &mut Formatter<'_>) -> std::fmt::Result {
51    write!(f, "The operation would introduce a cycle.")
52}
53
54fn fmt_merge_error<E: Error>(f: &mut Formatter<'_>, inner: &E) -> std::fmt::Result {
55    write!(f, "The merge callback failed with: {:?}", inner)
56}
57
58/// Error returned by Layers function when an index is not part of the graph.
59#[derive(Debug, PartialEq, Eq)]
60pub struct LayersError(pub String);
61
62impl Error for LayersError {}
63
64impl Display for LayersError {
65    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
66        write!(f, "{}", self.0)
67    }
68}