Skip to content

Add configurable length prefixed framing#87

Merged
leynos merged 5 commits intomainfrom
codex/define-endianness-enum-and-refactor-lengthprefixedprocessor
Jun 20, 2025
Merged

Add configurable length prefixed framing#87
leynos merged 5 commits intomainfrom
codex/define-endianness-enum-and-refactor-lengthprefixedprocessor

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jun 20, 2025

Summary

  • support different endian and size formats for frame length
  • pass length format to processor and update app default
  • test custom prefix sizes
  • document configurable length processor

Testing

  • make fmt
  • make lint
  • make test
  • markdownlint README.md docs/*.md

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

Summary by Sourcery

Add support for configurable length-prefixed framing by abstracting prefix size and endianness into a LengthFormat, update the processor and app defaults accordingly, and provide documentation and tests for custom formats.

New Features:

  • Introduce Endianness enum and LengthFormat struct for configurable frame length prefixes
  • Add LengthPrefixedProcessor::new(format) constructor and update default format to u32 big-endian

Enhancements:

  • Refactor encode/decode logic to delegate length handling to LengthFormat
  • Initialize WireframeApp with the configurable LengthPrefixedProcessor default

Documentation:

  • Document configurable length prefix format in README and design docs
  • Clean up Markdown formatting in AGENTS.md and roadmap docs

Tests:

  • Add roundtrip tests for custom two-byte big-endian and four-byte little-endian prefixes
  • Update existing framing tests to use LengthPrefixedProcessor::default()

Summary by CodeRabbit

  • New Features

    • Added support for configurable length prefix formats in frame processing, allowing selection of prefix size (2 or 4 bytes) and endianness (big or little endian).
  • Bug Fixes

    • Improved error handling for unsupported prefix sizes and out-of-range lengths.
  • Documentation

    • Enhanced and reformatted documentation for clarity and consistency.
  • Tests

    • Introduced new tests to verify encoding and decoding with various length prefix formats.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jun 20, 2025

Reviewer's Guide

This PR introduces a LengthFormat abstraction for configurable length-prefixed framing, refactors LengthPrefixedProcessor to accept and use custom prefix formats, updates the application default, expands tests for different endian and size prefixes, and cleans up related documentation.

Class diagram for LengthFormat and Endianness

classDiagram
    class Endianness {
        <<enum>>
        +Big
        +Little
    }
    class LengthFormat {
        +usize bytes
        +Endianness endianness
        +new(bytes: usize, endianness: Endianness)
        +u16_be()
        +u16_le()
        +u32_be()
        +u32_le()
        +read_len(bytes: &[u8]) io::Result<usize>
        +write_len(len: usize, dst: &mut BytesMut) io::Result<()> 
    }
    LengthFormat --> Endianness
Loading

Class diagram for updated LengthPrefixedProcessor

classDiagram
    class LengthPrefixedProcessor {
        +LengthFormat format
        +new(format: LengthFormat)
        +decode(src: &mut BytesMut) -> Result<Option<Vec<u8>>, io::Error>
        +encode(frame: &Vec<u8>, dst: &mut BytesMut) -> Result<(), io::Error>
    }
    LengthPrefixedProcessor --> LengthFormat
Loading

Class diagram for FrameProcessor trait and implementation

classDiagram
    class FrameProcessor {
        <<trait>>
        +type Frame
        +type Error
        +decode(&self, src: &mut BytesMut) -> Result<Option<Self::Frame>, Self::Error>
        +encode(&self, frame: &Self::Frame, dst: &mut BytesMut) -> Result<(), Self::Error>
    }
    LengthPrefixedProcessor ..|> FrameProcessor
Loading

File-Level Changes

Change Details Files
Implement configurable length prefix format
  • Define Endianness enum to represent byte order
  • Add LengthFormat struct with prefix size and endianness
  • Implement constructors (u16_be, u16_le, u32_be, u32_le) and Default
  • Provide read_len and write_len methods with error handling
src/frame.rs
Refactor LengthPrefixedProcessor to use LengthFormat
  • Add format field and new(format) constructor
  • Provide Default impl delegating to 4-byte big-endian
  • Update decode to use format.bytes and read_len
  • Update encode to reserve format.bytes and call write_len
src/frame.rs
Update application integration and tests for configurable format
  • Initialize default processor with LengthFormat::default() in app setup
  • Replace direct type usage with default() in existing tests
  • Add round-trip tests for 2-byte BE and 4-byte LE prefixes
src/app.rs
tests/response.rs
Document and clean up markdown for configurable framing
  • Describe configurable prefix size and endianness in README
  • Fix formatting and line breaks in AGENTS.md and roadmap
  • Update design doc to reference new LengthFormat
  • Apply consistent markdown lint fixes across docs
README.md
AGENTS.md
docs/roadmap.md
docs/rust-binary-router-library-design.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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 20, 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 changes introduce a configurable length prefix format for frame processing, replacing the previously hardcoded 4-byte big-endian prefix. This is achieved by adding new types for endianness and length format, updating the frame processor to accept these formats, and extending tests to cover various length prefix configurations. Documentation was also reformatted for clarity.

Changes

File(s) Change Summary
src/frame.rs Refactored length-prefixed framing to support configurable prefix size and endianness via new LengthFormat and Endianness types; updated processor logic and defaults.
src/app.rs Updated frame processor initialisation to use the new LengthFormat with explicit configuration.
tests/response.rs Updated imports and instantiation; added tests for custom length prefix formats and roundtrip encoding/decoding.
AGENTS.md, README.md, docs/roadmap.md, docs/rust-binary-router-library-design.md Reformatted documentation for clarity and style; no content or logic changes.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant LengthPrefixedProcessor
    participant LengthFormat

    App->>LengthPrefixedProcessor: new(LengthFormat)
    App->>LengthPrefixedProcessor: encode(frame)
    LengthPrefixedProcessor->>LengthFormat: write_len(frame.len(), buffer)
    LengthPrefixedProcessor-->>App: buffer with length prefix + frame

    App->>LengthPrefixedProcessor: decode(buffer)
    LengthPrefixedProcessor->>LengthFormat: read_len(buffer)
    LengthPrefixedProcessor-->>App: frame (if enough bytes)
Loading

Possibly related PRs

  • leynos/wireframe#68: Implements the initial length-prefixed framing with a fixed 4-byte big-endian prefix; both PRs modify the same processor and related code.
  • leynos/wireframe#70: Refactors frame processor traits and error handling; related as both address frame processing, though with different focuses.

Poem

🐇
I nibbled the code, made prefixes anew,
Big-endian, little—now both will do!
Frames march in line, with length at the start,
Configurable, flexible, a framing work of art.
Docs are now tidy, the tests hop along,
With every new format, the framing is strong!

✨ Finishing Touches
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch codex/define-endianness-enum-and-refactor-lengthprefixedprocessor

🪧 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
Copy Markdown

@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 Failed
Prevent hotspot decline (1 hotspot with Code Duplication)
Enforce advisory code health rules (1 file with Code Duplication)

Gates Passed
4 Quality Gates Passed

See analysis details in CodeScene

Reason for failure
Prevent hotspot decline Violations Code Health Impact
response.rs 1 rule in this hotspot 10.00 → 9.39 Suppress
Enforce advisory code health rules Violations Code Health Impact
response.rs 1 advisory rule 10.00 → 9.39 Suppress

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
Copy Markdown
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 found some issues that need to be addressed.

  • Consider refactoring the almost identical match arms in read_len/write_len (e.g. via a helper or using a byte-order utility) to reduce duplication and make future updates easier.
  • This PR bundles a lot of unrelated markdown formatting changes alongside the framing feature—splitting the docs/format tweaks into a separate commit or PR will keep the framing changes focused.
  • It would be good to add a test for unsupported prefix sizes (e.g. 3-byte prefixes) to verify that the error paths in read_len/write_len are triggered correctly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider refactoring the almost identical match arms in `read_len`/`write_len` (e.g. via a helper or using a byte-order utility) to reduce duplication and make future updates easier.
- This PR bundles a lot of unrelated markdown formatting changes alongside the framing feature—splitting the docs/format tweaks into a separate commit or PR will keep the framing changes focused.
- It would be good to add a test for unsupported prefix sizes (e.g. 3-byte prefixes) to verify that the error paths in `read_len`/`write_len` are triggered correctly.

## Individual Comments

### Comment 1
<location> `src/frame.rs:48` </location>
<code_context>
+    fn read_len(&self, bytes: &[u8]) -> io::Result<usize> {
</code_context>

<issue_to_address>
Potential for panics if 'bytes' slice is too short for the specified length prefix.

Validate that 'bytes' has at least 'self.bytes' elements before accessing, and return an error if it's too short.
</issue_to_address>

### Comment 2
<location> `src/app.rs:155` </location>
<code_context>
             services: Vec::new(),
             middleware: Vec::new(),
-            frame_processor: Box::new(LengthPrefixedProcessor),
+            frame_processor: Box::new(LengthPrefixedProcessor::new(LengthFormat::default())),
             serializer: S::default(),
             app_data: HashMap::new(),
</code_context>

<issue_to_address>
Remove redundant call to LengthFormat::default() as LengthPrefixedProcessor::default() already uses it.

Since LengthPrefixedProcessor::default() already constructs with LengthFormat::default(), you can simply use Box::new(LengthPrefixedProcessor::default()) for clarity and to avoid redundancy.
</issue_to_address>

### Comment 3
<location> `src/frame.rs:6` </location>
<code_context>

 use std::io;

</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, as required by the review instructions.
</issue_to_address>

### Comment 4
<location> `docs/roadmap.md:91` </location>
<code_context>
-      866-899) and introduce a `Next` helper to build the asynchronous call
-      chain. Trait definitions live in
-      [`src/middleware.rs`](../src/middleware.rs#L71-L84).
+    866-899) and introduce a `Next` helper to build the asynchronous call chain.
+    Trait definitions live in
+    [`src/middleware.rs`](../src/middleware.rs#L71-L84).
</code_context>

<issue_to_address>
Bullet point line exceeds 80 columns and is not wrapped as required.

Please wrap this bullet point so that no line exceeds 80 columns, as per the documentation guidelines.
</issue_to_address>

### Comment 5
<location> `docs/roadmap.md:92` </location>
<code_context>
-      chain. Trait definitions live in
-      [`src/middleware.rs`](../src/middleware.rs#L71-L84).
+    866-899) and introduce a `Next` helper to build the asynchronous call chain.
+    Trait definitions live in
+    [`src/middleware.rs`](../src/middleware.rs#L71-L84).
+
</code_context>

<issue_to_address>
Bullet point line is not wrapped to 80 columns, which is required for documentation.

Ensure that all bullet points and paragraphs are wrapped to 80 columns for consistency and readability.
</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 thread src/frame.rs
Comment thread src/app.rs
Comment thread src/frame.rs
Comment thread docs/roadmap.md
Comment thread docs/roadmap.md
coderabbitai Bot added a commit that referenced this pull request Jun 20, 2025
…prefixedprocessor`

Docstrings generation was requested by @leynos.

* #87 (comment)

The following files were modified:

* `src/app.rs`
* `src/frame.rs`
* `tests/response.rs`
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 20, 2025

Note

Generated docstrings for this pull request at #89

…prefixedprocessor` (#89)

* 📝 Add docstrings to `codex/define-endianness-enum-and-refactor-lengthprefixedprocessor`

Docstrings generation was requested by @leynos.

* #87 (comment)

The following files were modified:

* `src/app.rs`
* `src/frame.rs`
* `tests/response.rs`

* Move attributes below doc comments (#90)

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Leynos <leynos@troubledskies.net>
Copy link
Copy Markdown

@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 Failed
Prevent hotspot decline (1 hotspot with Code Duplication)
Enforce advisory code health rules (1 file with Code Duplication)

Gates Passed
4 Quality Gates Passed

See analysis details in CodeScene

Reason for failure
Prevent hotspot decline Violations Code Health Impact
response.rs 1 rule in this hotspot 10.00 → 9.39 Suppress
Enforce advisory code health rules Violations Code Health Impact
response.rs 1 advisory rule 10.00 → 9.39 Suppress

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
Copy Markdown

@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 Failed
Prevent hotspot decline (1 hotspot with Code Duplication)
Enforce advisory code health rules (1 file with Code Duplication)

Gates Passed
4 Quality Gates Passed

See analysis details in CodeScene

Reason for failure
Prevent hotspot decline Violations Code Health Impact
response.rs 1 rule in this hotspot 10.00 → 9.39 Suppress
Enforce advisory code health rules Violations Code Health Impact
response.rs 1 advisory rule 10.00 → 9.39 Suppress

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
Copy Markdown

@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 Failed
Prevent hotspot decline (1 hotspot with Code Duplication)
Enforce advisory code health rules (1 file with Code Duplication)

Gates Passed
4 Quality Gates Passed

See analysis details in CodeScene

Reason for failure
Prevent hotspot decline Violations Code Health Impact
response.rs 1 rule in this hotspot 10.00 → 9.39 Suppress
Enforce advisory code health rules Violations Code Health Impact
response.rs 1 advisory rule 10.00 → 9.39 Suppress

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.

@leynos leynos merged commit e285c72 into main Jun 20, 2025
5 checks passed
@leynos leynos deleted the codex/define-endianness-enum-and-refactor-lengthprefixedprocessor branch June 20, 2025 16:11
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