Skip to content

Conversation

@starovoid
Copy link
Collaborator

Methods with recoverable errors

This PR provides try_add_node, try_add_edge and try_update_edge for Graph and StableGraph.

The GraphError type has been added to represent possible errors:

/// The error type for fallible `Graph` & `StableGraph` operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphError {
    /// The Graph is at the maximum number of nodes for its index.
    NodeIxLimit,

    /// The Graph is at the maximum number of edges for its index.
    EdgeIxLimit,

    /// The node with the specified index is missing from the graph.
    NodeMissed(usize),

    /// Node indices out of bounds.
    NodeOutBounds,
}

To represent potential errors in MatrixGraph and other storages some other Errors will be needed due to the significant differences in provided APIs.

Rewrite fallible methods with its try counterparts

To avoid code duplication, I rewrote add_node, add_edge and update_edge for both Graph and StableGraph using try counterparts.
This changed the set of possible panics, but I tried to keep the custom panic messages:

panic!(
    "StableGraph::add_edge: node index {} is not a node in the graph",
    i
);

panic!("Graph::add_edge: node indices out of bounds")

@ABorgna ABorgna self-requested a review February 1, 2025 11:33
@starovoid starovoid self-assigned this Mar 20, 2025
@starovoid starovoid added this to the 0.8.1 milestone Mar 21, 2025
@starovoid starovoid modified the milestones: 0.8.1, 0.8 Mar 28, 2025
@starovoid starovoid mentioned this pull request Mar 28, 2025
8 tasks
@starovoid starovoid changed the title Add methods with recoverable errors for Graph and StableGraph feat: Add methods with recoverable errors for Graph and StableGraph Mar 28, 2025
Copy link

@notxvilka notxvilka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some future release we should make this behavior the default in add_edge(), not try_add_edge() in my opinion.

@starovoid
Copy link
Collaborator Author

In some future release we should make this behavior the default in add_edge(), not try_add_edge() in my opinion.

I think this will mostly resolve itself when switching to the Storage API #563.

@starovoid starovoid added this pull request to the merge queue Mar 31, 2025
Merged via the queue into petgraph:master with commit 40ea94c Mar 31, 2025
10 checks passed
@starovoid starovoid deleted the graph-rec-err branch March 31, 2025 16:58
github-merge-queue bot pushed a commit that referenced this pull request Apr 5, 2025
A sufficient number of proposed changes have accumulated to combine them
and publish a new major release numbered `0.8.0`.


BREAKING CHANGE:

This will require the user to provide extra type parameter in some APIs
(Read more in #747).

## List of changes
- [x] #747
The main innovation of the current release, the long-awaited feature
that has become very relevant due to the transition of dependent
projects to support `no_std`.
- [x] #662 
- [x] #611
- [x] #728
- [x] #686
- [x] #737 
- [x] #720 
- [x] #718 

## Note
There are still a large number of PRs that we want to adopt in the near
future, so we should expect at least a release of `0.8.1` soon after the
completion of `0.8.0`.

Thank you all for participating!

---------

Co-authored-by: Agustin Borgna <agustinborgna@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants