From 7b8689c348ef0e682f0124fae9025d16d8515523 Mon Sep 17 00:00:00 2001 From: Matthew Pomes Date: Fri, 12 Jul 2024 20:05:00 -0500 Subject: [PATCH] Update transient and use new features in examples --- core/http/Cargo.toml | 2 +- core/lib/Cargo.toml | 4 ++-- core/lib/src/form/error.rs | 6 +++--- examples/error-handling/src/main.rs | 26 +++----------------------- examples/error-handling/src/tests.rs | 5 ++--- 5 files changed, 11 insertions(+), 32 deletions(-) diff --git a/core/http/Cargo.toml b/core/http/Cargo.toml index 1c0afafc..033b1f8a 100644 --- a/core/http/Cargo.toml +++ b/core/http/Cargo.toml @@ -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" diff --git a/core/lib/Cargo.toml b/core/lib/Cargo.toml index 9853f654..a1eb28d3 100644 --- a/core/lib/Cargo.toml +++ b/core/lib/Cargo.toml @@ -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"] @@ -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"] } diff --git a/core/lib/src/form/error.rs b/core/lib/src/form/error.rs index 002c2f28..5a01570b 100644 --- a/core/lib/src/form/error.rs +++ b/core/lib/src/form/error.rs @@ -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}; @@ -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>); diff --git a/examples/error-handling/src/main.rs b/examples/error-handling/src/main.rs index cdde474b..8f14eb42 100644 --- a/examples/error-handling/src/main.rs +++ b/examples/error-handling/src/main.rs @@ -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 { - param.parse::().map(Self).map_err(IntErr) - } -} -// ------------------------------ - #[get("/hello//")] -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("/")] @@ -60,7 +40,7 @@ fn hello_not_found(uri: &Origin<'_>) -> content::RawHtml { // `error` and `status` type. All other params must be `FromOrigin`? #[catch(422, error = "" /*, status = "<_s>"*/)] -fn param_error(e: &IntErr, uri: &Origin<'_>) -> content::RawHtml { +fn param_error(e: &ParseIntError, uri: &Origin<'_>) -> content::RawHtml { content::RawHtml(format!("\

Sorry, but '{}' is not a valid path!

\

Try visiting /hello/<name>/<age> instead.

\ diff --git a/examples/error-handling/src/tests.rs b/examples/error-handling/src/tests.rs index ad6e421a..a0f90dc1 100644 --- a/examples/error-handling/src/tests.rs +++ b/examples/error-handling/src/tests.rs @@ -1,6 +1,5 @@ use rocket::local::blocking::Client; use rocket::http::Status; -use super::{I8, IntErr}; #[test] fn test_hello() { @@ -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] @@ -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::().unwrap_err()), + &path.split_once("/").unwrap().1.parse::().unwrap_err(), request.uri() ); let response = request.dispatch();