Skip to content

Commit

Permalink
Merge pull request #254 from adwhit/fix-better-http
Browse files Browse the repository at this point in the history
Debug fmt handles unicode boundaries
  • Loading branch information
charypar authored Jul 4, 2024
2 parents 96add81 + 4424c91 commit 8c6e9b4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Run sccache-cache
uses: mozilla-actions/[email protected]

- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v4
with:
version: latest

Expand Down
7 changes: 4 additions & 3 deletions crux_http/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl std::fmt::Debug for HttpRequest {
if s.len() < 50 {
format!("\"{s}\"")
} else {
format!("\"{}\"...", &s[..50])
format!("\"{}\"...", s.chars().take(50).collect::<String>())
}
} else {
format!("<binary data - {} bytes>", self.body.len())
Expand Down Expand Up @@ -310,12 +310,13 @@ mod tests {
{
// big
let req = HttpRequest::post("http://example.com")
.body("abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz")
// we check that we handle unicode boundaries correctly
.body("abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstu😀😀😀😀😀😀")
.build();
let repr = format!("{req:?}");
assert_eq!(
repr,
r#"HttpReqeuest { method: "POST", url: "http://example.com", body: "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvw"... }"#
r#"HttpReqeuest { method: "POST", url: "http://example.com", body: "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstu😀😀"... }"#
);
}

Expand Down
4 changes: 2 additions & 2 deletions crux_kv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl std::fmt::Debug for KeyValueOperation {
match self {
KeyValueOperation::Get { key } => f.debug_struct("Get").field("key", key).finish(),
KeyValueOperation::Set { key, value } => {
let body_repr = if let Ok(s) = std::str::from_utf8(&value) {
let body_repr = if let Ok(s) = std::str::from_utf8(value) {
if s.len() < 50 {
format!("\"{s}\"")
} else {
format!("\"{}\"...", &s[..50])
format!("\"{}\"...", s.chars().take(50).collect::<String>())
}
} else {
format!("<binary data - {} bytes>", value.len())
Expand Down
6 changes: 3 additions & 3 deletions crux_kv/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,13 @@ fn test_kv_operation_debug_repr() {
let op = KeyValueOperation::Set {
key: "my key".into(),
value:
b"abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz"
.to_vec(),
// we check that we handle unicode boundaries correctly
"abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstu😀😀😀😀😀😀".as_bytes().to_vec(),
};
let repr = format!("{op:?}");
assert_eq!(
repr,
r#"Set { key: "my key", value: "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvw"... }"#
r#"Set { key: "my key", value: "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstu😀😀"... }"#
);
}

Expand Down

0 comments on commit 8c6e9b4

Please sign in to comment.