mirror of https://github.com/rwf2/Rocket.git
Update transient and use new features in examples
This commit is contained in:
parent
dea224ff98
commit
7b8689c348
|
@ -36,7 +36,7 @@ memchr = "2"
|
||||||
stable-pattern = "0.1"
|
stable-pattern = "0.1"
|
||||||
cookie = { version = "0.18", features = ["percent-encode"] }
|
cookie = { version = "0.18", features = ["percent-encode"] }
|
||||||
state = "0.6"
|
state = "0.6"
|
||||||
transient = { version = "0.3" }
|
transient = { version = "0.4" }
|
||||||
|
|
||||||
[dependencies.serde]
|
[dependencies.serde]
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
|
|
|
@ -29,7 +29,7 @@ http3-preview = ["s2n-quic", "s2n-quic-h3", "tls"]
|
||||||
secrets = ["cookie/private", "cookie/key-expansion"]
|
secrets = ["cookie/private", "cookie/key-expansion"]
|
||||||
json = ["serde_json"]
|
json = ["serde_json"]
|
||||||
msgpack = ["rmp-serde"]
|
msgpack = ["rmp-serde"]
|
||||||
uuid = ["uuid_", "rocket_http/uuid"]
|
uuid = ["uuid_", "rocket_http/uuid", "transient/uuid"]
|
||||||
tls = ["rustls", "tokio-rustls", "rustls-pemfile"]
|
tls = ["rustls", "tokio-rustls", "rustls-pemfile"]
|
||||||
mtls = ["tls", "x509-parser"]
|
mtls = ["tls", "x509-parser"]
|
||||||
tokio-macros = ["tokio/macros"]
|
tokio-macros = ["tokio/macros"]
|
||||||
|
@ -74,7 +74,7 @@ tokio-stream = { version = "0.1.6", features = ["signal", "time"] }
|
||||||
cookie = { version = "0.18", features = ["percent-encode"] }
|
cookie = { version = "0.18", features = ["percent-encode"] }
|
||||||
futures = { version = "0.3.30", default-features = false, features = ["std"] }
|
futures = { version = "0.3.30", default-features = false, features = ["std"] }
|
||||||
state = "0.6"
|
state = "0.6"
|
||||||
transient = { version = "0.3" }
|
transient = { version = "0.4" }
|
||||||
|
|
||||||
# tracing
|
# tracing
|
||||||
tracing = { version = "0.1.40", default-features = false, features = ["std", "attributes"] }
|
tracing = { version = "0.1.40", default-features = false, features = ["std", "attributes"] }
|
||||||
|
|
|
@ -8,7 +8,6 @@ use std::net::AddrParseError;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use serde::{Serialize, ser::{Serializer, SerializeStruct}};
|
use serde::{Serialize, ser::{Serializer, SerializeStruct}};
|
||||||
use transient::Transient;
|
|
||||||
|
|
||||||
use crate::http::Status;
|
use crate::http::Status;
|
||||||
use crate::form::name::{NameBuf, Name};
|
use crate::form::name::{NameBuf, Name};
|
||||||
|
@ -55,8 +54,9 @@ use crate::data::ByteUnit;
|
||||||
/// Ok(i)
|
/// Ok(i)
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Default, Debug, PartialEq, Serialize, Transient)]
|
#[derive(Default, Debug, PartialEq, Serialize)]
|
||||||
#[variance('v = co)] // TODO: update when Transient v0.4
|
// TODO: this is invariant wrt 'v, since Cow<'a, T> is invariant wrt T.
|
||||||
|
// We need it to be covariant wrt 'v, so we can use it as an error type.
|
||||||
#[serde(transparent)]
|
#[serde(transparent)]
|
||||||
pub struct Errors<'v>(Vec<Error<'v>>);
|
pub struct Errors<'v>(Vec<Error<'v>>);
|
||||||
|
|
||||||
|
|
|
@ -5,31 +5,11 @@
|
||||||
use rocket::{Rocket, Build};
|
use rocket::{Rocket, Build};
|
||||||
use rocket::response::{content, status};
|
use rocket::response::{content, status};
|
||||||
use rocket::http::{Status, uri::Origin};
|
use rocket::http::{Status, uri::Origin};
|
||||||
|
|
||||||
// Custom impl so I can implement Static (or Transient) ---
|
|
||||||
// We should upstream implementations for most common error types
|
|
||||||
// in transient itself
|
|
||||||
use rocket::catcher::{Static};
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
#[allow(unused)]
|
|
||||||
struct IntErr(ParseIntError);
|
|
||||||
impl Static for IntErr {}
|
|
||||||
|
|
||||||
struct I8(i8);
|
|
||||||
use rocket::request::FromParam;
|
|
||||||
impl FromParam<'_> for I8 {
|
|
||||||
type Error = IntErr;
|
|
||||||
fn from_param(param: &str) -> Result<Self, Self::Error> {
|
|
||||||
param.parse::<i8>().map(Self).map_err(IntErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ------------------------------
|
|
||||||
|
|
||||||
#[get("/hello/<name>/<age>")]
|
#[get("/hello/<name>/<age>")]
|
||||||
fn hello(name: &str, age: I8) -> String {
|
fn hello(name: &str, age: i8) -> String {
|
||||||
format!("Hello, {} year old named {}!", age.0, name)
|
format!("Hello, {} year old named {}!", age, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<code>")]
|
#[get("/<code>")]
|
||||||
|
@ -60,7 +40,7 @@ fn hello_not_found(uri: &Origin<'_>) -> content::RawHtml<String> {
|
||||||
|
|
||||||
// `error` and `status` type. All other params must be `FromOrigin`?
|
// `error` and `status` type. All other params must be `FromOrigin`?
|
||||||
#[catch(422, error = "<e>" /*, status = "<_s>"*/)]
|
#[catch(422, error = "<e>" /*, status = "<_s>"*/)]
|
||||||
fn param_error(e: &IntErr, uri: &Origin<'_>) -> content::RawHtml<String> {
|
fn param_error(e: &ParseIntError, uri: &Origin<'_>) -> content::RawHtml<String> {
|
||||||
content::RawHtml(format!("\
|
content::RawHtml(format!("\
|
||||||
<p>Sorry, but '{}' is not a valid path!</p>\
|
<p>Sorry, but '{}' is not a valid path!</p>\
|
||||||
<p>Try visiting /hello/<name>/<age> instead.</p>\
|
<p>Try visiting /hello/<name>/<age> instead.</p>\
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use rocket::local::blocking::Client;
|
use rocket::local::blocking::Client;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use super::{I8, IntErr};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hello() {
|
fn test_hello() {
|
||||||
|
@ -11,7 +10,7 @@ fn test_hello() {
|
||||||
let response = client.get(uri).dispatch();
|
let response = client.get(uri).dispatch();
|
||||||
|
|
||||||
assert_eq!(response.status(), Status::Ok);
|
assert_eq!(response.status(), Status::Ok);
|
||||||
assert_eq!(response.into_string().unwrap(), super::hello(name, I8(age)));
|
assert_eq!(response.into_string().unwrap(), super::hello(name, age));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -50,7 +49,7 @@ fn test_hello_invalid_age() {
|
||||||
for path in &["Ford/-129", "Trillian/128"] {
|
for path in &["Ford/-129", "Trillian/128"] {
|
||||||
let request = client.get(format!("/hello/{}", path));
|
let request = client.get(format!("/hello/{}", path));
|
||||||
let expected = super::param_error(
|
let expected = super::param_error(
|
||||||
&IntErr(path.split_once("/").unwrap().1.parse::<i8>().unwrap_err()),
|
&path.split_once("/").unwrap().1.parse::<i8>().unwrap_err(),
|
||||||
request.uri()
|
request.uri()
|
||||||
);
|
);
|
||||||
let response = request.dispatch();
|
let response = request.dispatch();
|
||||||
|
|
Loading…
Reference in New Issue