Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
loikki committed Aug 7, 2024
1 parent 01f6762 commit fe8506d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
15 changes: 8 additions & 7 deletions core/codegen/src/derive/from_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions core/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion core/codegen/tests/from_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
38 changes: 17 additions & 21 deletions core/codegen/tests/ui-fail-nightly/from_param.stderr
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion core/codegen/tests/ui-fail-stable/from_param.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit fe8506d

Please sign in to comment.