Skip to content

Commit

Permalink
Update transient and use new features in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
the10thWiz committed Jul 13, 2024
1 parent dea224f commit 7b8689c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 32 deletions.
2 changes: 1 addition & 1 deletion core/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ memchr = "2"
stable-pattern = "0.1"
cookie = { version = "0.18", features = ["percent-encode"] }
state = "0.6"
transient = { version = "0.3" }
transient = { version = "0.4" }

[dependencies.serde]
version = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions core/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ http3-preview = ["s2n-quic", "s2n-quic-h3", "tls"]
secrets = ["cookie/private", "cookie/key-expansion"]
json = ["serde_json"]
msgpack = ["rmp-serde"]
uuid = ["uuid_", "rocket_http/uuid"]
uuid = ["uuid_", "rocket_http/uuid", "transient/uuid"]
tls = ["rustls", "tokio-rustls", "rustls-pemfile"]
mtls = ["tls", "x509-parser"]
tokio-macros = ["tokio/macros"]
Expand Down Expand Up @@ -74,7 +74,7 @@ tokio-stream = { version = "0.1.6", features = ["signal", "time"] }
cookie = { version = "0.18", features = ["percent-encode"] }
futures = { version = "0.3.30", default-features = false, features = ["std"] }
state = "0.6"
transient = { version = "0.3" }
transient = { version = "0.4" }

# tracing
tracing = { version = "0.1.40", default-features = false, features = ["std", "attributes"] }
Expand Down
6 changes: 3 additions & 3 deletions core/lib/src/form/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::net::AddrParseError;
use std::borrow::Cow;

use serde::{Serialize, ser::{Serializer, SerializeStruct}};
use transient::Transient;

use crate::http::Status;
use crate::form::name::{NameBuf, Name};
Expand Down Expand Up @@ -55,8 +54,9 @@ use crate::data::ByteUnit;
/// Ok(i)
/// }
/// ```
#[derive(Default, Debug, PartialEq, Serialize, Transient)]
#[variance('v = co)] // TODO: update when Transient v0.4
#[derive(Default, Debug, PartialEq, Serialize)]
// 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)]
pub struct Errors<'v>(Vec<Error<'v>>);

Expand Down
26 changes: 3 additions & 23 deletions examples/error-handling/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,11 @@
use rocket::{Rocket, Build};
use rocket::response::{content, status};
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;

#[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>")]
fn hello(name: &str, age: I8) -> String {
format!("Hello, {} year old named {}!", age.0, name)
fn hello(name: &str, age: i8) -> String {
format!("Hello, {} year old named {}!", age, name)
}

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

// `error` and `status` type. All other params must be `FromOrigin`?
#[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!("\
<p>Sorry, but '{}' is not a valid path!</p>\
<p>Try visiting /hello/&lt;name&gt;/&lt;age&gt; instead.</p>\
Expand Down
5 changes: 2 additions & 3 deletions examples/error-handling/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use rocket::local::blocking::Client;
use rocket::http::Status;
use super::{I8, IntErr};

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

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]
Expand Down Expand Up @@ -50,7 +49,7 @@ fn test_hello_invalid_age() {
for path in &["Ford/-129", "Trillian/128"] {
let request = client.get(format!("/hello/{}", path));
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()
);
let response = request.dispatch();
Expand Down

0 comments on commit 7b8689c

Please sign in to comment.