Skip to content

Commit 7b8689c

Browse files
committed
Update transient and use new features in examples
1 parent dea224f commit 7b8689c

File tree

5 files changed

+11
-32
lines changed

5 files changed

+11
-32
lines changed

core/http/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ memchr = "2"
3636
stable-pattern = "0.1"
3737
cookie = { version = "0.18", features = ["percent-encode"] }
3838
state = "0.6"
39-
transient = { version = "0.3" }
39+
transient = { version = "0.4" }
4040

4141
[dependencies.serde]
4242
version = "1.0"

core/lib/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ http3-preview = ["s2n-quic", "s2n-quic-h3", "tls"]
2929
secrets = ["cookie/private", "cookie/key-expansion"]
3030
json = ["serde_json"]
3131
msgpack = ["rmp-serde"]
32-
uuid = ["uuid_", "rocket_http/uuid"]
32+
uuid = ["uuid_", "rocket_http/uuid", "transient/uuid"]
3333
tls = ["rustls", "tokio-rustls", "rustls-pemfile"]
3434
mtls = ["tls", "x509-parser"]
3535
tokio-macros = ["tokio/macros"]
@@ -74,7 +74,7 @@ tokio-stream = { version = "0.1.6", features = ["signal", "time"] }
7474
cookie = { version = "0.18", features = ["percent-encode"] }
7575
futures = { version = "0.3.30", default-features = false, features = ["std"] }
7676
state = "0.6"
77-
transient = { version = "0.3" }
77+
transient = { version = "0.4" }
7878

7979
# tracing
8080
tracing = { version = "0.1.40", default-features = false, features = ["std", "attributes"] }

core/lib/src/form/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::net::AddrParseError;
88
use std::borrow::Cow;
99

1010
use serde::{Serialize, ser::{Serializer, SerializeStruct}};
11-
use transient::Transient;
1211

1312
use crate::http::Status;
1413
use crate::form::name::{NameBuf, Name};
@@ -55,8 +54,9 @@ use crate::data::ByteUnit;
5554
/// Ok(i)
5655
/// }
5756
/// ```
58-
#[derive(Default, Debug, PartialEq, Serialize, Transient)]
59-
#[variance('v = co)] // TODO: update when Transient v0.4
57+
#[derive(Default, Debug, PartialEq, Serialize)]
58+
// TODO: this is invariant wrt 'v, since Cow<'a, T> is invariant wrt T.
59+
// We need it to be covariant wrt 'v, so we can use it as an error type.
6060
#[serde(transparent)]
6161
pub struct Errors<'v>(Vec<Error<'v>>);
6262

examples/error-handling/src/main.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,11 @@
55
use rocket::{Rocket, Build};
66
use rocket::response::{content, status};
77
use rocket::http::{Status, uri::Origin};
8-
9-
// Custom impl so I can implement Static (or Transient) ---
10-
// We should upstream implementations for most common error types
11-
// in transient itself
12-
use rocket::catcher::{Static};
138
use std::num::ParseIntError;
149

15-
#[derive(Debug)]
16-
#[allow(unused)]
17-
struct IntErr(ParseIntError);
18-
impl Static for IntErr {}
19-
20-
struct I8(i8);
21-
use rocket::request::FromParam;
22-
impl FromParam<'_> for I8 {
23-
type Error = IntErr;
24-
fn from_param(param: &str) -> Result<Self, Self::Error> {
25-
param.parse::<i8>().map(Self).map_err(IntErr)
26-
}
27-
}
28-
// ------------------------------
29-
3010
#[get("/hello/<name>/<age>")]
31-
fn hello(name: &str, age: I8) -> String {
32-
format!("Hello, {} year old named {}!", age.0, name)
11+
fn hello(name: &str, age: i8) -> String {
12+
format!("Hello, {} year old named {}!", age, name)
3313
}
3414

3515
#[get("/<code>")]
@@ -60,7 +40,7 @@ fn hello_not_found(uri: &Origin<'_>) -> content::RawHtml<String> {
6040

6141
// `error` and `status` type. All other params must be `FromOrigin`?
6242
#[catch(422, error = "<e>" /*, status = "<_s>"*/)]
63-
fn param_error(e: &IntErr, uri: &Origin<'_>) -> content::RawHtml<String> {
43+
fn param_error(e: &ParseIntError, uri: &Origin<'_>) -> content::RawHtml<String> {
6444
content::RawHtml(format!("\
6545
<p>Sorry, but '{}' is not a valid path!</p>\
6646
<p>Try visiting /hello/&lt;name&gt;/&lt;age&gt; instead.</p>\

examples/error-handling/src/tests.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rocket::local::blocking::Client;
22
use rocket::http::Status;
3-
use super::{I8, IntErr};
43

54
#[test]
65
fn test_hello() {
@@ -11,7 +10,7 @@ fn test_hello() {
1110
let response = client.get(uri).dispatch();
1211

1312
assert_eq!(response.status(), Status::Ok);
14-
assert_eq!(response.into_string().unwrap(), super::hello(name, I8(age)));
13+
assert_eq!(response.into_string().unwrap(), super::hello(name, age));
1514
}
1615

1716
#[test]
@@ -50,7 +49,7 @@ fn test_hello_invalid_age() {
5049
for path in &["Ford/-129", "Trillian/128"] {
5150
let request = client.get(format!("/hello/{}", path));
5251
let expected = super::param_error(
53-
&IntErr(path.split_once("/").unwrap().1.parse::<i8>().unwrap_err()),
52+
&path.split_once("/").unwrap().1.parse::<i8>().unwrap_err(),
5453
request.uri()
5554
);
5655
let response = request.dispatch();

0 commit comments

Comments
 (0)