summaryrefslogtreecommitdiff
path: root/graph/src/error.rs
blob: ce45acc9057045a8dacd49ac24ad8baa4ff25f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#![warn(missing_docs)]
//! This file implements the error data type of the graph library.

use core::fmt::{self, Display};

/// The error type for methods of the trait [`Graph`][`super::Graph`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
#[non_exhaustive]
pub enum Error {
    /// The index is out of bounds.
    ///
    /// The first component is the index that is out of bounds, and
    /// the second component is the current length of nodes.
    IndexOutOfBounds(usize, usize),
    /// The graph does not permit duplicate nodes but encounters a
    /// repeated node.
    DuplicatedNode(usize),
    /// The graph does not permit duplicate edges but encounters a
    /// repeated edge.
    DuplicatedEdge(usize, usize),
    /// The source node has no room to add a new edge.
    FullNode(usize),
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::IndexOutOfBounds(index, len) => {
                write!(f, "index {index} out of bounds {len} ")
            }
            Error::DuplicatedNode(node) => {
                write!(f, "No duplicate nodes permitted, but found one: {node}")
            }
            Error::DuplicatedEdge(source, target) => {
                write!(
                    f,
                    "No duplicate edges permitted, but found one from {source} to {target}"
                )
            }
            Error::FullNode(index) => write!(f, "the node {index} has no room for new edges"),
        }
    }
}

impl std::error::Error for Error {}