Skip to content

Conversation

@SOF3
Copy link

@SOF3 SOF3 commented Jul 27, 2025

Summary by CodeRabbit

  • New Features

    • Added fallible variants of Dijkstra and Yen algorithms that support error handling during pathfinding operations.
    • Both infallible and fallible versions are now available in the public API.
  • Bug Fixes

    • Improved error propagation ensures immediate feedback if any closure fails during algorithm execution.
  • Documentation

    • Updated public API documentation to reflect the addition of fallible variants and their error handling behavior.

@coderabbitai
Copy link

coderabbitai bot commented Jul 27, 2025

Walkthrough

The Dijkstra and Yen algorithm implementations were refactored to introduce fallible variants that propagate errors from user-supplied closures. New try_* functions were added, returning Result types. The original infallible functions now delegate to these fallible versions, wrapping closures to always succeed. Control flow and internal helpers were updated to support error propagation.

Changes

File(s) Change Summary
src/directed/dijkstra.rs Refactored Dijkstra functions to add fallible (try_*) variants; updated internal logic for error propagation; changed signatures of helpers to return Result; added type aliases for clarity; public API updated to expose both infallible and fallible variants.
src/directed/yen.rs Refactored Yen's k-shortest paths algorithm to add a fallible (try_yen) variant; updated internal logic and helpers to propagate errors; original function now delegates to fallible version.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Dijkstra
    participant UserClosure

    User->>Dijkstra: try_dijkstra(start, successors, success)
    Dijkstra->>UserClosure: successors(node) returns Result
    alt Ok
        Dijkstra->>UserClosure: success(node) returns Result
        alt Ok
            Dijkstra-->>User: Result<Option<(Path, Cost)>, E>
        else Error
            Dijkstra-->>User: Err(E)
        end
    else Error
        Dijkstra-->>User: Err(E)
    end
Loading
sequenceDiagram
    participant User
    participant Yen
    participant Dijkstra
    participant UserClosure

    User->>Yen: try_yen(start, successors, success, k)
    loop up to k times
        Yen->>Dijkstra: dijkstra_internal(...)
        Dijkstra->>UserClosure: successors(node) returns Result
        Dijkstra->>UserClosure: success(node) returns Result
        Dijkstra-->>Yen: Result<Option<(Path, Cost)>, E>
        Yen-->>User: Result<Vec<(Path, Cost)>, E>
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Poem

In the warren of graphs, where paths twist and bend,
Dijkstra and Yen now both handle "the end"—
If closures should stumble, with errors in tow,
Our algorithms pause, letting problems bestow.
A hop and a skip, with Result in the air,
The rabbit reviews with meticulous care!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/directed/yen.rs (1)

113-121: Consider using a more idiomatic unwrap pattern.

Since Infallible can never be constructed, the match statement could be simplified.

-    match try_yen::<_, _, _, _, _, Infallible>(
-        start,
-        |node| Ok(successors(node)),
-        |node| Ok(success(node)),
-        k,
-    ) {
-        Ok(v) => v,
-    }
+    try_yen::<_, _, _, _, _, Infallible>(
+        start,
+        |node| Ok(successors(node)),
+        |node| Ok(success(node)),
+        k,
+    )
+    .unwrap_or_else(|inf: Infallible| match inf {})

This makes it explicit that the error case is impossible and will satisfy the compiler's exhaustiveness checking.

src/directed/dijkstra.rs (3)

83-91: Consider using the same idiomatic unwrap pattern as suggested for yen.rs.

For consistency across the codebase and better idiomaticity.

-{
-    match dijkstra_internal::<_, _, _, _, _, Infallible>(
-        start,
-        &mut |node| Ok(successors(node)),
-        &mut |node| Ok(success(node)),
-    ) {
-        Ok(result) => result,
-    }
-}
+{
+    dijkstra_internal::<_, _, _, _, _, Infallible>(
+        start,
+        &mut |node| Ok(successors(node)),
+        &mut |node| Ok(success(node)),
+    )
+    .unwrap_or_else(|inf: Infallible| match inf {})
+}

229-236: Apply the same unwrap pattern for consistency.

-    match try_dijkstra_partial::<_, _, _, _, _, Infallible>(
-        start,
-        |node| Ok(successors(node)),
-        |node| Ok(stop(node)),
-    ) {
-        Ok(result) => result,
-    }
+    try_dijkstra_partial::<_, _, _, _, _, Infallible>(
+        start,
+        |node| Ok(successors(node)),
+        |node| Ok(stop(node)),
+    )
+    .unwrap_or_else(|inf: Infallible| match inf {})

487-515: Verify: Should dijkstra_reach have a fallible variant?

The dijkstra_reach function and its iterator implementation still use infallible closures. Was this intentionally left out of the fallible variants update?

If a fallible variant is needed, I can help implement try_dijkstra_reach, though it would require careful consideration of how to handle errors in the iterator pattern.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d7bf6d9 and 91fa896.

📒 Files selected for processing (2)
  • src/directed/dijkstra.rs (7 hunks)
  • src/directed/yen.rs (5 hunks)
🔇 Additional comments (9)
src/directed/yen.rs (3)

8-8: Import addition looks good.

The Infallible type is correctly imported for use in the infallible wrapper pattern.


123-229: Excellent implementation of the fallible variant.

The error propagation is correctly implemented throughout the function, maintaining the original algorithm logic while properly handling potential failures in the successors and success closures.


231-247: Helper function correctly updated for error propagation.

The make_cost function properly handles the fallible successors closure and propagates errors as expected.

src/directed/dijkstra.rs (6)

11-11: Import correctly added.

The Infallible type import is necessary for the wrapper pattern implementation.


93-112: Clean implementation of the fallible dijkstra variant.

The function correctly delegates to dijkstra_internal with proper error type constraints.


114-133: Internal function correctly updated for error handling.

The dijkstra_internal function properly propagates errors from run_dijkstra while maintaining the original logic.


183-199: Fallible dijkstra_all variant correctly implemented.

The function properly handles errors and delegates to try_dijkstra_partial with appropriate error propagation.


238-239: Good use of type aliases for clarity.

The PartialResult and RunResult type aliases improve code readability.

Also applies to: 271-272


273-333: Core dijkstra implementation correctly updated for error handling.

The run_dijkstra function properly propagates errors from both stop and successors closures while maintaining the algorithm's correctness.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 4, 2025

CodSpeed Performance Report

Merging #689 will degrade performances by 18.38%

Comparing SOF3:fallible-dijkstra (91fa896) with main (d7bf6d9)

Summary

⚡ 1 improvements
❌ 2 regressions
✅ 35 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
corner_to_corner_dijkstra 1.4 ms 1.7 ms -17.45%
no_path_dijkstra 1.3 ms 1.6 ms -18.38%
no_path_fringe 1.9 ms 1.6 ms +18.84%

@SOF3
Copy link
Author

SOF3 commented Aug 9, 2025

doesn't look like the regression is related to this change, considering no_path_fringe was not involved at all.

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.

1 participant