Skip to content

Conversation

leynos
Copy link
Owner

@leynos leynos commented Jun 22, 2025

Summary

  • make envelope type generic via Packet trait
  • provide new_with_envelope constructor
  • update middleware and tests for generic envelopes
  • document custom envelopes in README

Testing

  • make lint
  • make test

https://chatgpt.com/codex/tasks/task_e_6857449705c08322bd5082fc1489fb35

Summary by Sourcery

Enable custom envelope formats by defining a Packet trait and making the application, handlers, middleware, and utilities generic over that trait; provide a new_with_envelope constructor and document the feature with examples.

New Features:

  • Introduce Packet trait for generic envelope types.
  • Parameterize WireframeApp, Handler, Middleware, and related components over a Packet type.
  • Add new_with_envelope constructor to support custom envelope types.

Enhancements:

  • Refactor frame deserialization and response serialization to use the Packet trait generically.
  • Generalize middleware, handler service, and utility functions to be generic over envelope types.
  • Update tests and utilities to accept WireframeApp with custom envelope parameter.

Documentation:

  • Document custom envelope usage and examples in README and architecture design docs.

Tests:

  • Extend test routes and middleware tests to verify custom Packet implementations.

Summary by CodeRabbit

  • New Features
    • Introduced support for custom envelope types, allowing applications to use their own packet formats by implementing a new trait.
  • Documentation
    • Added a comprehensive section to the README explaining how to use custom envelopes, including example code and integration guidance.
  • Refactor
    • Generalised core types and methods to support arbitrary envelope types, enhancing flexibility across the application and middleware layers.
  • Tests
    • Updated and extended tests to validate functionality with custom envelope types and ensure compatibility with the new trait-based approach.

Copy link
Contributor

sourcery-ai bot commented Jun 22, 2025

Reviewer's Guide

This PR refactors core types and flows to support custom envelope types by introducing a Packet trait, parameterizing application and middleware layers over an arbitrary packet type, and updating constructors, routing, serialization, and tests accordingly.

Sequence diagram for message processing with generic Packet

sequenceDiagram
    participant Client
    participant WireframeApp
    participant Middleware
    participant HandlerService
    participant Handler

    Client->>WireframeApp: Send frame (bytes)
    WireframeApp->>WireframeApp: Deserialize to E: Packet
    WireframeApp->>Middleware: Pass envelope (E)
    Middleware->>HandlerService: Pass envelope (E)
    HandlerService->>Handler: Call handler with &E
    Handler-->>HandlerService: Return response (bytes)
    HandlerService->>WireframeApp: Construct response envelope (E::from_parts)
    WireframeApp->>Client: Send response frame (bytes)
Loading

Class diagram for custom envelope implementation

classDiagram
    class MyEnv {
        +id: u32
        +data: Vec<u8>
        +id() u32
        +into_parts() (u32, Vec<u8>)
        +from_parts(id: u32, data: Vec<u8>) MyEnv
    }
    Packet <|.. MyEnv
Loading

File-Level Changes

Change Details Files
Introduce Packet trait abstraction
  • Define Packet trait with id(), into_parts(), from_parts()
  • Implement Packet for default Envelope
  • Provide Packet impl for TestEnvelope in tests
src/app.rs
tests/routes.rs
Parameterize core components over Packet
  • Update WireframeApp, Handler, HandlerService, and Transform traits to use generic E: Packet
  • Refactor process_stream, build_chains, and response generation to use Packet methods
  • Add Send+Sync bounds to Serializer and Packet generics
src/app.rs
src/middleware.rs
Add new_with_envelope constructor
  • Implement WireframeApp::new_with_envelope for custom envelope types
  • Ensure default() supports generic E
src/app.rs
Update documentation for custom envelopes
  • Document Packet trait and usage in README with example
  • Add Custom Envelopes section and sequence diagram in design doc
README.md
docs/rust-binary-router-library-design.md
Adapt tests and utilities for generic envelopes
  • Make run_app_with_frame and related utilities generic over E: Packet
  • Change tests/routes.rs to use WireframeApp::<,,TestEnvelope>::new_with_envelope
  • Update middleware_order test to reference Handler
tests/util.rs
tests/routes.rs
tests/middleware_order.rs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

coderabbitai bot commented Jun 22, 2025

Warning

Rate limit exceeded

@leynos has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 2 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 457c658 and 65be00b.

📒 Files selected for processing (3)
  • docs/rust-binary-router-library-design.md (1 hunks)
  • src/app.rs (15 hunks)
  • src/middleware.rs (3 hunks)

Walkthrough

The codebase was refactored to allow the WireframeApp to work with custom envelope types via a new Packet trait, replacing the previously fixed Envelope type. All relevant types, services, middleware, and test utilities were made generic over the envelope type, and documentation was updated to explain the new extensibility.

Changes

File(s) Change Summary
README.md Added documentation on custom envelopes, the Packet trait, and usage examples.
src/app.rs Generalised WireframeApp, handlers, and middleware to be generic over an envelope type E: Packet. Introduced the Packet trait and updated all relevant method signatures and constructors. Added new_with_envelope() for custom envelope instantiation.
src/middleware.rs Made HandlerService and RouteService generic over E: Packet. Updated service construction and call logic to use the generic envelope type.
tests/middleware_order.rs Updated the TagMiddleware and handler types to be generic over Envelope.
tests/routes.rs Implemented Packet for TestEnvelope and updated tests to use WireframeApp with custom envelope types.
tests/util.rs Updated test utility functions to be generic over serializer, context, and envelope types with appropriate trait bounds.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant WireframeApp
    participant Middleware
    participant HandlerService
    participant Handler

    Client->>WireframeApp: Send frame (bytes)
    WireframeApp->>WireframeApp: Deserialize to E: Packet
    WireframeApp->>Middleware: Pass envelope (E)
    Middleware->>HandlerService: Pass envelope (E)
    HandlerService->>Handler: Call handler with &E
    Handler-->>HandlerService: Return response (bytes)
    HandlerService->>WireframeApp: Construct response envelope (E::from_parts)
    WireframeApp->>Client: Send response frame (bytes)
Loading

Possibly related PRs

Poem

A rabbit hopped with packets new,
No longer bound to just one view.
Envelopes custom, traits in tow,
Through middleware and routes they flow.
Now any shape your packets take,
WireframeApp will gladly bake!
📨🐇

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch codex/extend-envelope-type-to-support-generics

🪧 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 auto-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
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @leynos - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/middleware.rs:220` </location>
<code_context>

 /// Service that invokes a stored route handler and middleware chain.
-pub struct HandlerService {
+pub struct HandlerService<E: Packet> {
     id: u32,
     svc: Box<dyn Service<Error = Infallible> + Send + Sync>,
+    _marker: std::marker::PhantomData<E>,
 }

</code_context>

<issue_to_address>
The addition of PhantomData<E> is necessary for type safety but could be documented for clarity.

Consider adding a comment explaining that PhantomData<E> is used to parameterize HandlerService by E for type safety.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 220 to 223
pub struct HandlerService<E: Packet> {
id: u32,
svc: Box<dyn Service<Error = Infallible> + Send + Sync>,
_marker: std::marker::PhantomData<E>,
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: The addition of PhantomData is necessary for type safety but could be documented for clarity.

Consider adding a comment explaining that PhantomData is used to parameterize HandlerService by E for type safety.

Copy link
Contributor

@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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a790d75 and 457c658.

📒 Files selected for processing (6)
  • README.md (1 hunks)
  • src/app.rs (15 hunks)
  • src/middleware.rs (3 hunks)
  • tests/middleware_order.rs (2 hunks)
  • tests/routes.rs (3 hunks)
  • tests/util.rs (5 hunks)
🧰 Additional context used
🪛 LanguageTool
README.md

[style] ~91-~91: Since ownership is already implied, this phrasing may be redundant.
Context: ... payload bytes. Applications can supply their own envelope type by calling `WireframeApp:...

(PRP_OWN)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-test
🔇 Additional comments (12)
README.md (1)

88-115: Well-documented feature addition!

The new "Custom Envelopes" section provides clear documentation for the generic envelope support, including a complete example implementation of the Packet trait and its usage with WireframeApp.

tests/routes.rs (1)

25-31: Test coverage for generic envelope support looks good!

The Packet trait implementation for TestEnvelope and the corresponding test updates correctly demonstrate the usage of custom envelope types.

Also applies to: 41-46, 83-89

tests/middleware_order.rs (1)

34-37: Middleware test correctly adapted for generic envelopes!

The Transform implementation and handler type updates properly reflect the generic HandlerService<Envelope> design.

Also applies to: 51-51

src/middleware.rs (1)

217-274: Excellent implementation of generic envelope support in middleware!

The generalization of HandlerService and RouteService over E: Packet is well-implemented:

  • Proper use of PhantomData for the unused type parameter
  • Consistent usage of Packet trait methods (from_parts, into_parts)
  • All service trait implementations correctly updated
tests/util.rs (1)

3-7: Test utilities properly generalized!

The addition of generic parameters <S, C, E> with appropriate trait bounds ensures the test utilities work correctly with the new generic WireframeApp design.

Also applies to: 34-42, 55-64, 74-82, 95-104

src/app.rs (7)

70-77: Well-structured generic implementation with sensible defaults.

The addition of the generic parameter E: Packet = Envelope maintains backward compatibility whilst enabling custom envelope types. The internal fields are correctly updated to use the generic type.


89-89: Handler type correctly generalised.

The Handler type alias properly accepts the generic envelope type E as a parameter reference.


92-101: Middleware trait properly generalised with appropriate bounds.

The trait definition and blanket implementation correctly constrain the middleware to work with HandlerService<E> for the envelope type.


140-154: Excellent trait design for envelope abstraction.

The Packet trait provides a minimal and clean interface with appropriate trait bounds. The three methods cover all necessary operations for message routing and handling.


175-182: Correct Packet implementation for Envelope.

The implementation properly delegates to existing Envelope methods, ensuring backward compatibility.


225-237: Constructor for custom envelope types is well-designed.

The new_with_envelope() method enables users to create a WireframeApp with custom envelope types whilst maintaining API consistency by returning Result.


547-566: Core message handling correctly uses Packet trait methods.

The implementation properly deserialises to the generic type E, uses into_parts() for decomposition, and E::from_parts() for response construction. The routing logic remains intact.

@leynos
Copy link
Owner Author

leynos commented Jun 22, 2025

@sourcery-ai review

@leynos
Copy link
Owner Author

leynos commented Jun 22, 2025

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jun 22, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @leynos - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@leynos leynos merged commit 9c31510 into main Jun 22, 2025
5 checks passed
@leynos leynos deleted the codex/extend-envelope-type-to-support-generics branch June 22, 2025 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant