rshyper_core/
error.rs

1/*
2    Appellation: error <module>
3    Contrib: @FL03
4*/
5//! this module implements the [`Error`] type for the [`rshyper`](https://docs.rs/rshyper)
6//! crate.
7
8#[cfg(feature = "alloc")]
9use alloc::{boxed::Box, string::String};
10
11use crate::{EdgeId, VertexId};
12
13/// A type alias for a [Result] with the crate-specific error type [Error]
14pub type Result<T = ()> = core::result::Result<T, Error>;
15
16/// The error type for this crate
17#[derive(Debug, thiserror::Error)]
18pub enum Error {
19    #[error("Index is out of bounds")]
20    IndexOutOfBounds,
21    #[error("Invalid index")]
22    InvalidIndex,
23    #[error("Cannot create empty hyperedge")]
24    EmptyHyperedge,
25    #[error("Hyperedge {0} does not exist")]
26    HyperedgeDoesNotExist(EdgeId<usize>),
27    #[error("Vertex {0} does not exist")]
28    VertexDoesNotExist(VertexId<usize>),
29    #[cfg(feature = "anyhow")]
30    #[error(transparent)]
31    AnyError(#[from] anyhow::Error),
32    #[cfg(feature = "serde_json")]
33    #[error(transparent)]
34    JsonError(#[from] serde_json::Error),
35    #[cfg(feature = "alloc")]
36    #[error(transparent)]
37    Other(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
38    #[cfg(feature = "alloc")]
39    #[error("Unknown error: {0}")]
40    Unknown(String),
41}
42
43#[cfg(feature = "alloc")]
44impl From<&str> for Error {
45    fn from(s: &str) -> Self {
46        Error::Unknown(String::from(s))
47    }
48}
49
50#[cfg(feature = "alloc")]
51impl From<String> for Error {
52    fn from(s: String) -> Self {
53        Error::Unknown(s)
54    }
55}