Skip to main content

rust_igraph/core/
error.rs

1//! Error type for rust-igraph.
2//!
3//! Mirrors the most-used variants of `igraph_error_t` from the C core, with room to grow.
4//! Numeric error codes from `include/igraph_error.h` are kept as a separate enum so
5//! existing igraph-C tests can be reproduced precisely.
6
7use thiserror::Error;
8
9/// All errors returned from rust-igraph.
10///
11/// Variants are added as algorithms land; the initial set covers the cases the
12/// walking-skeleton (Phase 0) needs.
13#[derive(Debug, Error)]
14pub enum IgraphError {
15    /// Argument was outside its accepted range or otherwise invalid.
16    #[error("invalid argument: {0}")]
17    InvalidArgument(String),
18
19    /// A vertex id referred to a vertex that does not exist.
20    #[error("vertex {id} out of range (graph has {n} vertices)")]
21    VertexOutOfRange {
22        /// The invalid vertex id.
23        id: u32,
24        /// Total number of vertices in the graph.
25        n: u32,
26    },
27
28    /// An edge id referred to an edge that does not exist.
29    #[error("edge {id} out of range (graph has {m} edges)")]
30    EdgeOutOfRange {
31        /// The invalid edge id.
32        id: u32,
33        /// Total number of edges in the graph.
34        m: u32,
35    },
36
37    /// An I/O failure while reading or writing graph data.
38    #[error("I/O error: {0}")]
39    Io(#[from] std::io::Error),
40
41    /// Parsing a graph file failed.
42    #[error("parse error at line {line}: {message}")]
43    Parse {
44        /// Line number where the parse error occurred (1-based).
45        line: usize,
46        /// Description of the parse error.
47        message: String,
48    },
49
50    /// Operation is not supported (e.g. unweighted-only path requested on weighted graph).
51    #[error("unsupported: {0}")]
52    Unsupported(&'static str),
53
54    /// Numeric algorithm failed to converge within iteration budget.
55    #[error("did not converge after {iters} iterations (last residual {residual})")]
56    DidNotConverge {
57        /// Number of iterations completed.
58        iters: usize,
59        /// Last measured residual norm.
60        residual: f64,
61    },
62
63    /// Catch-all for unexpected internal failures (bugs).
64    #[error("internal error: {0}")]
65    Internal(&'static str),
66}
67
68/// Convenience alias for `Result<T, IgraphError>`.
69pub type IgraphResult<T> = std::result::Result<T, IgraphError>;