From fe8506d3a7fd5de444e3ee54996816d6b6641f79 Mon Sep 17 00:00:00 2001 From: loikki <851651-loikki@users.noreply.gitlab.com> Date: Wed, 7 Aug 2024 21:44:13 +0200 Subject: [PATCH] Apply suggestions --- core/codegen/src/derive/from_param.rs | 15 ++++---- core/codegen/src/lib.rs | 10 ++++- core/codegen/tests/from_param.rs | 2 +- .../tests/ui-fail-nightly/from_param.stderr | 38 +++++++++---------- .../tests/ui-fail-stable/from_param.stderr | 2 +- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/core/codegen/src/derive/from_param.rs b/core/codegen/src/derive/from_param.rs index ac66e7c54f..8767128147 100644 --- a/core/codegen/src/derive/from_param.rs +++ b/core/codegen/src/derive/from_param.rs @@ -11,21 +11,22 @@ pub fn derive_from_param(input: proc_macro::TokenStream) -> TokenStream { .validator(ValidatorBuild::new() .fields_validate(|_, fields| { if !fields.is_empty() { - return Err(fields.span().error("Only empty enums are accepted")); + return Err( + fields.span().error("Only enums without data fields are supported") + ); } Ok(()) }) ) .inner_mapper(MapperBuild::new() .enum_map(|_, data| { - let mut matches = vec![]; - - for field in data.variants() { + let matches = data.variants().map(|field| { let field_name = &field; - matches.push(quote!( + quote!( stringify!(#field_name) => Ok(Self::#field_name), - )) - } + ) + + }); quote! { type Error = &'a str; diff --git a/core/codegen/src/lib.rs b/core/codegen/src/lib.rs index f5217bc07b..3d6b957c39 100644 --- a/core/codegen/src/lib.rs +++ b/core/codegen/src/lib.rs @@ -777,20 +777,26 @@ pub fn derive_from_form(input: TokenStream) -> TokenStream { /// Derive for the [`FromParam`] trait. /// /// The [`FromParam`] derive can be applied to enums with nullary -/// (zero-length) fields: +/// (zero-length) fields. To implement FromParam, the function matches each variant +/// to its stringified field name (case sensitive): /// /// ```rust /// # #[macro_use] extern crate rocket; /// # /// use rocket::request::FromParam; /// -/// #[derive(FromParam)] +/// #[derive(FromParam, Debug, PartialEq)] /// enum MyParam { /// A, /// B, /// } /// /// assert_eq!(MyParam::from_param("A").unwrap(), MyParam::A); +/// assert_eq!(MyParam::from_param("B").unwrap(), MyParam::B); +/// assert!(MyParam::from_param("a").is_err()); +/// assert!(MyParam::from_param("b").is_err()); +/// assert!(MyParam::from_param("c").is_err()); +/// assert!(MyParam::from_param("C").is_err()); /// ``` /// /// Now `MyParam` can be used in an endpoint and will accept either `A` or `B`. diff --git a/core/codegen/tests/from_param.rs b/core/codegen/tests/from_param.rs index 6595e21b6c..a753131619 100644 --- a/core/codegen/tests/from_param.rs +++ b/core/codegen/tests/from_param.rs @@ -15,5 +15,5 @@ fn derive_from_param() { assert_eq!(test2, Test::Test2); let test3 = Test::from_param("not_test"); - assert!(test3.is_err()) + assert!(test3.is_err()); } diff --git a/core/codegen/tests/ui-fail-nightly/from_param.stderr b/core/codegen/tests/ui-fail-nightly/from_param.stderr index 9bb79c62bf..697bb4e4d9 100644 --- a/core/codegen/tests/ui-fail-nightly/from_param.stderr +++ b/core/codegen/tests/ui-fail-nightly/from_param.stderr @@ -1,57 +1,53 @@ error: named structs are not supported - --> tests/ui-fail-stable/from_param.rs:4:1 + --> tests/ui-fail-nightly/from_param.rs:4:1 | 4 | / struct Foo1 { 5 | | a: String 6 | | } | |_^ - -error: [note] error occurred while deriving `FromParam` - --> tests/ui-fail-stable/from_param.rs:3:10 + | +note: error occurred while deriving `FromParam` + --> tests/ui-fail-nightly/from_param.rs:3:10 | 3 | #[derive(FromParam)] | ^^^^^^^^^ - | = note: this error originates in the derive macro `FromParam` (in Nightly builds, run with -Z macro-backtrace for more info) error: named structs are not supported - --> tests/ui-fail-stable/from_param.rs:9:1 + --> tests/ui-fail-nightly/from_param.rs:9:1 | 9 | struct Foo2 {} | ^^^^^^^^^^^^^^ - -error: [note] error occurred while deriving `FromParam` - --> tests/ui-fail-stable/from_param.rs:8:10 + | +note: error occurred while deriving `FromParam` + --> tests/ui-fail-nightly/from_param.rs:8:10 | 8 | #[derive(FromParam)] | ^^^^^^^^^ - | = note: this error originates in the derive macro `FromParam` (in Nightly builds, run with -Z macro-backtrace for more info) -error: Only empty enums are accepted - --> tests/ui-fail-stable/from_param.rs:13:6 +error: Only enums without data fields are supported + --> tests/ui-fail-nightly/from_param.rs:13:6 | 13 | A(String), | ^^^^^^^^ - -error: [note] error occurred while deriving `FromParam` - --> tests/ui-fail-stable/from_param.rs:11:10 + | +note: error occurred while deriving `FromParam` + --> tests/ui-fail-nightly/from_param.rs:11:10 | 11 | #[derive(FromParam)] | ^^^^^^^^^ - | = note: this error originates in the derive macro `FromParam` (in Nightly builds, run with -Z macro-backtrace for more info) error: tuple structs are not supported - --> tests/ui-fail-stable/from_param.rs:18:1 + --> tests/ui-fail-nightly/from_param.rs:18:1 | 18 | struct Foo4(usize); | ^^^^^^^^^^^^^^^^^^^ - -error: [note] error occurred while deriving `FromParam` - --> tests/ui-fail-stable/from_param.rs:17:10 + | +note: error occurred while deriving `FromParam` + --> tests/ui-fail-nightly/from_param.rs:17:10 | 17 | #[derive(FromParam)] | ^^^^^^^^^ - | = note: this error originates in the derive macro `FromParam` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/core/codegen/tests/ui-fail-stable/from_param.stderr b/core/codegen/tests/ui-fail-stable/from_param.stderr index 9bb79c62bf..97a9e65ded 100644 --- a/core/codegen/tests/ui-fail-stable/from_param.stderr +++ b/core/codegen/tests/ui-fail-stable/from_param.stderr @@ -28,7 +28,7 @@ error: [note] error occurred while deriving `FromParam` | = note: this error originates in the derive macro `FromParam` (in Nightly builds, run with -Z macro-backtrace for more info) -error: Only empty enums are accepted +error: Only enums without data fields are supported --> tests/ui-fail-stable/from_param.rs:13:6 | 13 | A(String),