-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
509a033
commit 38cebeb
Showing
11 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::exports::*; | ||
use devise::ext::SpanDiagnosticExt; | ||
use devise::Support; | ||
use devise::*; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::ext::IdentExt; | ||
|
||
pub fn derive_from_param(input: proc_macro::TokenStream) -> TokenStream { | ||
DeriveGenerator::build_for(input, quote!(impl<'a> #_request::FromParam<'a>)) | ||
.support(Support::Enum) | ||
.validator(ValidatorBuild::new().fields_validate(|_, fields| { | ||
if !fields.is_empty() { | ||
return Err(fields | ||
.span() | ||
.error("Only enums without data fields are supported")); | ||
} | ||
Ok(()) | ||
})) | ||
.inner_mapper(MapperBuild::new().enum_map(|_, data| { | ||
let matches = data.variants().map(|field| { | ||
let field_name = field.ident.unraw(); | ||
quote!( | ||
stringify!(#field_name) => Ok(Self::#field), | ||
) | ||
}); | ||
let names = data.variants().map(|field| { | ||
let field_name = field.ident.unraw(); | ||
quote!( | ||
#_Cow::Borrowed(stringify!(#field_name)), | ||
) | ||
}); | ||
|
||
quote! { | ||
type Error = #_request::EnumFromParamError<'a>; | ||
fn from_param(param: &'a str) -> Result<Self, Self::Error> { | ||
match param { | ||
#(#matches)* | ||
_ => Err(#_request::EnumFromParamError::new( | ||
#_Cow::Borrowed(param), | ||
#_Cow::Borrowed(&[#(#names)*]), | ||
)), | ||
} | ||
} | ||
} | ||
})) | ||
.to_tokens() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pub mod from_form; | |
pub mod from_form_field; | ||
pub mod responder; | ||
pub mod uri_display; | ||
pub mod from_param; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use rocket::request::FromParam; | ||
|
||
#[derive(Debug, FromParam, PartialEq)] | ||
enum Test { | ||
Test1, | ||
Test2, | ||
r#for, | ||
} | ||
|
||
#[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 test2 = Test::from_param("for").expect("Should be valid"); | ||
assert_eq!(test2, Test::r#for); | ||
|
||
let test3 = Test::from_param("not_test"); | ||
assert!(test3.is_err()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../ui-fail/from_param.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
error: named structs are not supported | ||
--> tests/ui-fail-nightly/from_param.rs:4:1 | ||
| | ||
4 | / struct Foo1 { | ||
5 | | a: String | ||
6 | | } | ||
| |_^ | ||
| | ||
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-nightly/from_param.rs:9:1 | ||
| | ||
9 | struct Foo2 {} | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
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 enums without data fields are supported | ||
--> tests/ui-fail-nightly/from_param.rs:13:6 | ||
| | ||
13 | A(String), | ||
| ^^^^^^^^ | ||
| | ||
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-nightly/from_param.rs:18:1 | ||
| | ||
18 | struct Foo4(usize); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../ui-fail/from_param.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
error: named structs are not supported | ||
--> tests/ui-fail-stable/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 | ||
| | ||
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 | ||
| | ||
9 | struct Foo2 {} | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: [note] error occurred while deriving `FromParam` | ||
--> tests/ui-fail-stable/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 enums without data fields are supported | ||
--> tests/ui-fail-stable/from_param.rs:13:6 | ||
| | ||
13 | A(String), | ||
| ^^^^^^^^ | ||
|
||
error: [note] error occurred while deriving `FromParam` | ||
--> tests/ui-fail-stable/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 | ||
| | ||
18 | struct Foo4(usize); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: [note] error occurred while deriving `FromParam` | ||
--> tests/ui-fail-stable/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use rocket::request::FromParam; | ||
|
||
#[derive(FromParam)] | ||
struct Foo1 { | ||
a: String | ||
} | ||
|
||
#[derive(FromParam)] | ||
struct Foo2 {} | ||
|
||
#[derive(FromParam)] | ||
enum Foo3 { | ||
A(String), | ||
B(String) | ||
} | ||
|
||
#[derive(FromParam)] | ||
struct Foo4(usize); | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters