Skip to content

Conversation

leynos
Copy link
Owner

@leynos leynos commented Jun 19, 2025

Summary

  • add ServiceRequest and ServiceResponse wrappers with helpers
  • expose mutable frame access and builder functions
  • support middleware call chaining via Next
  • provide unit test demonstrating request and response modification

Testing

  • cargo clippy --all-targets --all-features -- -D warnings
  • RUSTFLAGS="-D warnings" cargo test --quiet

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

Summary by Sourcery

Implement basic middleware wrappers and chaining for asynchronous services, including typed request/response wrappers with helper methods and a Next helper for composing middleware, and demonstrate usage with a unit test.

New Features:

  • Add ServiceRequest and ServiceResponse types with constructors and frame accessors.
  • Introduce Next helper to chain middleware calls for asynchronous services.

Enhancements:

  • Expose mutable accessors and into_inner builders on ServiceRequest and ServiceResponse.

Documentation:

  • Update roadmap.md to mark middleware wrappers and tests as completed.

Tests:

  • Add unit test demonstrating middleware modifying requests and responses during chaining.

Summary by CodeRabbit

  • New Features
    • Enhanced request and response handling to support manipulation of raw frame data.
  • Tests
    • Introduced tests to verify middleware behaviour, including modification of request and response frames.

Copy link
Contributor

sourcery-ai bot commented Jun 19, 2025

Reviewer's Guide

This PR extends the middleware module by turning the empty request/response placeholders into full-featured frame wrappers with constructor, accessor, and mutation helpers, enhances the Next helper for chaining, updates the roadmap to mark these items complete, and adds a unit test that verifies request and response frame modification through a middleware chain.

Class diagram for ServiceRequest and ServiceResponse wrappers

classDiagram
    class ServiceRequest {
        - frame: Vec<u8>
        + new(frame: Vec<u8>)
        + frame() &[u8]
        + frame_mut() &mut Vec<u8>
        + into_inner() Vec<u8>
    }
    class ServiceResponse {
        - frame: Vec<u8>
        + new(frame: Vec<u8>)
        + frame() &[u8]
        + frame_mut() &mut Vec<u8>
        + into_inner() Vec<u8>
    }
Loading

Class diagram for Next helper for middleware chaining

classDiagram
    class Next~'a, S~ {
        - service: &'a S
        + new(service: &'a S)
        + call(request: ServiceRequest) -> ServiceResponse
    }
Loading

File-Level Changes

Change Details Files
Introduce ServiceRequest wrapper with constructors, accessors, and mutation helpers
  • Added frame: Vec<u8> field to ServiceRequest
  • Implemented new, frame, frame_mut, and into_inner methods
src/middleware.rs
Introduce ServiceResponse wrapper with constructors, accessors, and mutation helpers
  • Replaced unit struct with frame: Vec<u8>
  • Implemented new, frame, frame_mut, and into_inner methods
src/middleware.rs
Enhance Next helper for middleware chaining
  • Refined doc comment for Next::new
  • Added #[inline] and #[must_use] attributes to its constructor
src/middleware.rs
Add middleware chaining unit test demonstrating request/response modification
  • Defined EchoService and ModifyMiddleware
  • Implemented Transform/Service on ModifyService to modify frames
  • Added middleware_modifies_request_and_response test
tests/middleware.rs
Update roadmap to reflect implemented wrappers and tests
  • Marked ServiceRequest/ServiceResponse and Next helper tasks as complete
  • Marked tests verifying request/response modification as complete
docs/roadmap.md

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 19, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The ServiceRequest and ServiceResponse structs have been updated to encapsulate a Vec<u8> frame, along with methods for construction and frame access. Corresponding tests were added to validate middleware that modifies request and response frames, demonstrating the new struct capabilities in an asynchronous context.

Changes

File(s) Change Summary
src/middleware.rs Refactored ServiceRequest and ServiceResponse to hold a Vec<u8> frame and added access methods.
tests/middleware.rs Added tests and supporting types for middleware behaviour, including frame modification and validation.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ModifyMiddleware
    participant EchoService

    Client->>ModifyMiddleware: Send ServiceRequest(frame)
    ModifyMiddleware->>ModifyMiddleware: Append b'!' to frame
    ModifyMiddleware->>EchoService: Forward modified ServiceRequest
    EchoService->>ModifyMiddleware: Return ServiceResponse(modified frame)
    ModifyMiddleware->>ModifyMiddleware: Append b'?' to frame
    ModifyMiddleware->>Client: Return ServiceResponse(final frame)
Loading

Possibly related PRs

  • Implement trait foundations #8: Introduced the initial middleware abstractions and traits in src/middleware.rs that are now extended with frame encapsulation and manipulation.

Poem

A frame of bytes, now snug inside,
With methods neat, our code’s new pride.
Middleware hops, appends with glee—
An exclamation, then a query!
The tests all pass, the bytes align,
This rabbit’s code is quite divine.
🐇✨

✨ Finishing Touches
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch codex/expand-src/middleware.rs-and-add-unit-tests

🪧 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

@codescene-delta-analysis codescene-delta-analysis bot left a comment

Choose a reason for hiding this comment

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

Gates Passed
6 Quality Gates Passed

See analysis details in CodeScene

Absence of Expected Change Pattern

  • wireframe/src/middleware.rs is usually changed with: wireframe/src/extractor.rs

Quality Gate Profile: Pay Down Tech Debt
Want more control? Customize Code Health rules or catch issues early with our IDE extension and CLI tool.

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:10` </location>
<code_context>
 /// Incoming request wrapper passed through middleware.
 #[derive(Debug)]
-pub struct ServiceRequest;
+pub struct ServiceRequest {
+    frame: Vec<u8>,
+}
</code_context>

<issue_to_address>
The module is missing a containing item comment (`//!`).

Please add a module-level doc comment at the top of the file using `//!` to describe the purpose of this module.
</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.

@leynos
Copy link
Owner Author

leynos commented Jun 19, 2025

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jun 19, 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.

@leynos
Copy link
Owner Author

leynos commented Jun 20, 2025

@sourcery-ai review

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 - here's some feedback:

  • ServiceResponse no longer implements Default, which may break existing code—consider re-deriving Default or providing a default constructor.
  • To improve ergonomics, implement From<Vec> for ServiceRequest and Into<Vec> for ServiceResponse so callers can more easily convert to/from raw frames.
  • If you want to avoid unnecessary buffer copies in middleware chains, consider using a zero-copy buffer type (e.g. Bytes) instead of Vec as the inner frame.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- ServiceResponse no longer implements Default, which may break existing code—consider re-deriving Default or providing a default constructor.
- To improve ergonomics, implement From<Vec<u8>> for ServiceRequest and Into<Vec<u8>> for ServiceResponse so callers can more easily convert to/from raw frames.
- If you want to avoid unnecessary buffer copies in middleware chains, consider using a zero-copy buffer type (e.g. Bytes) instead of Vec<u8> as the inner frame.

## Individual Comments

### Comment 1
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 2
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 3
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 4
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 5
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 6
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</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.

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:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 2
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 3
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 4
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 5
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 6
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 7
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</issue_to_address>

### Comment 8
<location> `src/middleware.rs:17` </location>
<code_context>
+
+impl ServiceRequest {
+    /// Create a new [`ServiceRequest`] from raw frame bytes.
+    #[must_use]
+    pub fn new(frame: Vec<u8>) -> Self { Self { frame } }
+
</code_context>

<issue_to_address>
Function attribute #[must_use] is placed before the doc comment, but it should come after.

Please move the #[must_use] attribute to after the function's doc comment, as per the review instruction.
</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.

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
Copy link
Owner Author

leynos commented Jun 20, 2025

@sourcery-ai resolve

@leynos
Copy link
Owner Author

leynos commented Jun 20, 2025

@sourcery-ai review

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 f8aa1da into main Jun 20, 2025
4 checks passed
@leynos leynos deleted the codex/expand-src/middleware.rs-and-add-unit-tests branch June 20, 2025 02:58
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