Skip to content

Commit

Permalink
Introduce ByteUnit (#147)
Browse files Browse the repository at this point in the history
Manipulating bytes as `usize`s is... clunky. 
I find myself commenting on relevant lines with the value converted in
the most relevant unit (e.g. MB or GB). That's a clear-cut code smell if
I've ever seen one.

We introduce `ByteUnit` by re-exporting from the `ubyte` crate. It is
used in
[`rocket`](https://api.rocket.rs/master/rocket/data/struct.ByteUnit.html)
and it fits our needs perfectly.

⚠️ Breaking change: I renamed `max_n_bytes` to `max_size` in
`BodySizeLimit`. The type has also changed from `usize` to `ByteUnit`.
This impacts you if you were registering a custom constructor for
`BodySizeLimit`. The same applies to the `SizeLimitExceeded` error.
  • Loading branch information
LukeMathWalker authored Jan 4, 2024
1 parent e18c421 commit c9e1478
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 28 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
use pavex::blueprint::{constructor::Lifecycle, Blueprint};
use pavex::f;
use pavex::request::body::BodySizeLimit;
use pavex::unit::ToByteUnit;
pub fn body_size_limit() -> BodySizeLimit {
BodySizeLimit::Enabled {
max_n_bytes: 10_485_760, // 10 MBs
max_size: 2.megabytes(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use pavex::blueprint::{constructor::Lifecycle, router::POST, Blueprint};
use pavex::f;
use pavex::request::body::BodySizeLimit;
use pavex::unit::ToByteUnit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
Expand All @@ -21,7 +22,7 @@ fn upload_bp() -> Blueprint {
pub fn upload_size_limit() -> BodySizeLimit {
BodySizeLimit::Enabled {
max_n_bytes: 1_073_741_824, // 1GB
max_size: 1.gigabytes(),
}
}
```
10 changes: 10 additions & 0 deletions doc_examples/guide/request_data/buffered_body/project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use pavex::blueprint::{constructor::Lifecycle, Blueprint};
use pavex::f;
use pavex::request::body::BodySizeLimit;
use pavex::unit::ToByteUnit;

pub fn body_size_limit() -> BodySizeLimit {
BodySizeLimit::Enabled {
max_n_bytes: 10_485_760, // 10 MBs
max_size: 2.megabytes(),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use pavex::blueprint::{constructor::Lifecycle, router::POST, Blueprint};
use pavex::f;
use pavex::request::body::BodySizeLimit;
use pavex::unit::ToByteUnit;

pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
Expand All @@ -20,6 +21,6 @@ fn upload_bp() -> Blueprint {

pub fn upload_size_limit() -> BodySizeLimit {
BodySizeLimit::Enabled {
max_n_bytes: 1_073_741_824, // 1GB
max_size: 1.gigabytes(),
}
}
2 changes: 1 addition & 1 deletion doc_examples/guide/request_data/buffered_body/tutorial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ snippets:
hl_lines: [ 4 ]
- name: "custom_limit"
source_path: "src/custom_limit/blueprint.rs"
ranges: [ "0..13", "19..20" ]
ranges: [ "0..14", "20..21" ]
- name: "no_limit"
source_path: "src/no_limit/blueprint.rs"
ranges: [ "0..11", "17..18" ]
Expand Down
10 changes: 10 additions & 0 deletions doc_examples/guide/request_data/json/project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions doc_examples/guide/request_data/path/project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions doc_examples/guide/request_data/query/project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions doc_examples/guide/request_data/query_params/project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions doc_examples/guide/request_data/request_target/project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions doc_examples/guide/request_data/route_params/project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions libs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/pavex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ paste = "1"
tracing = "0.1"
http-body-util = "0.1"
pin-project-lite = "0.2"
ubyte = { version = "0.10.4", features = ["serde"] }

# Route parameters
matchit = { git = "https://github.com/ibraheemdev/matchit", branch = "master" }
Expand Down
1 change: 1 addition & 0 deletions libs/pavex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ pub mod router;
pub mod serialization;
#[cfg(feature = "server")]
pub mod server;
pub mod unit;
Loading

0 comments on commit c9e1478

Please sign in to comment.