Update transient and use new features in examples

This commit is contained in:
Matthew Pomes 2024-07-12 20:05:00 -05:00
parent dea224ff98
commit 7b8689c348
No known key found for this signature in database
GPG Key ID: B8C0D93B8D8FBDB7
5 changed files with 11 additions and 32 deletions

View File

@ -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"

View File

@ -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"] }

View File

@ -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<Error<'v>>);

View File

@ -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>")]
@ -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>\

View File

@ -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::<i8>().unwrap_err()),
&path.split_once("/").unwrap().1.parse::<i8>().unwrap_err(),
request.uri()
);
let response = request.dispatch();