hypergraph_rs/core/error.rs
1//! Error types for hypergraph operations.
2
3use thiserror::Error;
4
5/// Error raised when a node operation fails.
6#[derive(Debug, Clone, Error, PartialEq)]
7pub enum NodeError {
8 /// The node ID was not found in the hypergraph.
9 #[error("node {node_id} does not exist")]
10 NotFound { node_id: String },
11 /// The named edge (not node) was not found — returned by methods that
12 /// take both an edge id and a node id (e.g. `remove_node_from_edge`).
13 #[error("edge {edge_id} not found")]
14 EdgeNotFound { edge_id: String },
15}
16
17/// Error raised when a membership operation fails
18/// (`Hypergraph::remove_node_from_edge` / `DiHypergraph::remove_node_from_edge`).
19///
20/// XGI raises `XGIError` for all three branches, each with a distinct
21/// message (probed v0.10.2); the core returns a dedicated variant per
22/// branch so callers discriminate without string-matching. The PyO3
23/// binding translates `Err` -> raise, reproducing XGI's exact messages
24/// (D2 error-channel class).
25#[derive(Debug, Clone, Error, PartialEq)]
26pub enum MembershipError {
27 /// The edge ID was not found in the hypergraph.
28 #[error("edge {edge_id} does not exist")]
29 EdgeNotFound { edge_id: String },
30 /// The node ID was not found in the hypergraph.
31 #[error("node {node_id} does not exist")]
32 NodeNotFound { node_id: String },
33 /// Both exist, but the node is not a member of the edge.
34 #[error("node {node_id} is not a member of edge {edge_id}")]
35 NotAMember { node_id: String, edge_id: String },
36}
37
38/// Error raised when an edge operation fails.
39#[derive(Debug, Clone, Error, PartialEq)]
40pub enum EdgeError {
41 /// The edge ID was not found in the hypergraph.
42 #[error("edge {edge_id} does not exist")]
43 NotFound { edge_id: String },
44 /// The edge ID already exists (duplicate on add_edge).
45 #[error("edge {edge_id} already exists")]
46 AlreadyExists { edge_id: String },
47 /// `random_edge_shuffle` needs at least two edges to swap between.
48 #[error("random_edge_shuffle requires at least 2 edges")]
49 TooFewEdges,
50}
51
52/// Error raised by `double_edge_swap`.
53#[derive(Debug, Clone, Error, PartialEq)]
54pub enum SwapError {
55 /// A specified node or edge was not found, or the node is not a member of
56 /// the named edge.
57 #[error("id {id} not found (or node not a member of the named edge)")]
58 IdNotFound { id: String },
59 /// The swap would not preserve edge sizes (the incoming node is already a
60 /// member of the target edge).
61 #[error("swap does not preserve edge sizes")]
62 SizeNotPreserved,
63}
64
65/// Error raised by `SimplicialComplex` operations.
66#[derive(Debug, Clone, Error, PartialEq)]
67pub enum SimplexError {
68 /// A `Hypergraph` method that is not available on a `SimplicialComplex`.
69 #[error("{method} is not available for SimplicialComplex; use {alternative}")]
70 NotAvailable { method: String, alternative: String },
71 /// The simplex id was not found.
72 #[error("simplex {idx} is not in the simplicial complex")]
73 NotFound { idx: String },
74 /// A complex rebuilt from a list of simplices lacks a face of this one.
75 #[error("simplex {idx} has a face that is not in the list")]
76 NotClosed { idx: String },
77}