Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
loikki committed Aug 2, 2024
1 parent 9746832 commit 079ee9f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
6 changes: 3 additions & 3 deletions core/codegen/src/derive/from_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ pub fn derive_from_param(input: proc_macro::TokenStream) -> TokenStream {
)
.inner_mapper(MapperBuild::new()
.enum_map(|_, data| {
let mut matches = quote!();
let mut matches = vec![];

for field in data.variants() {
let field_name = &field;
matches.extend(quote!(
matches.push(quote!(
stringify!(#field_name) => Ok(Self::#field_name),
))
}
Expand All @@ -32,7 +32,7 @@ pub fn derive_from_param(input: proc_macro::TokenStream) -> TokenStream {
type Error = &'a str;
fn from_param(param: &'a str) -> Result<Self, Self::Error> {
match param {
#matches
#(#matches)*
_ => Err("Failed to find enum")
}
}
Expand Down
5 changes: 4 additions & 1 deletion core/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,17 @@ pub fn derive_from_form(input: TokenStream) -> TokenStream {
/// ```rust
/// # #[macro_use] extern crate rocket;
/// #
/// use rocket::request::FromParam;
///
/// #[derive(FromParam)]
/// enum MyParam {
/// A,
/// B,
/// }
/// ```
///
/// Now `MyParam` can be used in an endpoint and will accept either 'A' or 'B'.
/// Now `MyParam` can be used in an endpoint and will accept either `A` or `B`.
/// [`FromParam`]: ../rocket/request/trait.FromParam.html
///
#[proc_macro_derive(FromParam)]
pub fn derive_from_param(input: TokenStream) -> TokenStream {
Expand Down
5 changes: 3 additions & 2 deletions core/codegen/tests/from_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ enum Test {
Test2
}



#[test]
fn derive_from_param() {
let test1 = Test::from_param("Test1").expect("Should be valid");
assert_eq!(test1, Test::Test1);

let test2 = Test::from_param("Test2").expect("Should be valid");
assert_eq!(test2, Test::Test2);

let test3 = Test::from_param("not_test");
assert!(test3.is_err())
}
9 changes: 9 additions & 0 deletions core/codegen/tests/ui-fail/from_param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use rocket::request::FromParam;

#[derive(FromParam)]
enum Foo1 {
A,
B
}

fn main() {}

0 comments on commit 079ee9f

Please sign in to comment.