- Notifications
You must be signed in to change notification settings - Fork516
feat(derive): add#[postgres(allow_mismatch)]
#1026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use postgres_types::{FromSql, ToSql}; | ||
#[derive(ToSql, Debug)] | ||
#[postgres(allow_mismatch)] | ||
struct ToSqlAllowMismatchStruct { | ||
a: i32, | ||
} | ||
#[derive(FromSql, Debug)] | ||
#[postgres(allow_mismatch)] | ||
struct FromSqlAllowMismatchStruct { | ||
a: i32, | ||
} | ||
#[derive(ToSql, Debug)] | ||
#[postgres(allow_mismatch)] | ||
struct ToSqlAllowMismatchTupleStruct(i32, i32); | ||
#[derive(FromSql, Debug)] | ||
#[postgres(allow_mismatch)] | ||
struct FromSqlAllowMismatchTupleStruct(i32, i32); | ||
#[derive(FromSql, Debug)] | ||
#[postgres(transparent, allow_mismatch)] | ||
struct TransparentFromSqlAllowMismatchStruct(i32); | ||
#[derive(FromSql, Debug)] | ||
#[postgres(allow_mismatch, transparent)] | ||
struct AllowMismatchFromSqlTransparentStruct(i32); | ||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
error: #[postgres(allow_mismatch)] may only be applied to enums | ||
--> src/compile-fail/invalid-allow-mismatch.rs:4:1 | ||
| | ||
4 | / #[postgres(allow_mismatch)] | ||
5 | | struct ToSqlAllowMismatchStruct { | ||
6 | | a: i32, | ||
7 | | } | ||
| |_^ | ||
error: #[postgres(allow_mismatch)] may only be applied to enums | ||
--> src/compile-fail/invalid-allow-mismatch.rs:10:1 | ||
| | ||
10 | / #[postgres(allow_mismatch)] | ||
11 | | struct FromSqlAllowMismatchStruct { | ||
12 | | a: i32, | ||
13 | | } | ||
| |_^ | ||
error: #[postgres(allow_mismatch)] may only be applied to enums | ||
--> src/compile-fail/invalid-allow-mismatch.rs:16:1 | ||
| | ||
16 | / #[postgres(allow_mismatch)] | ||
17 | | struct ToSqlAllowMismatchTupleStruct(i32, i32); | ||
| |_______________________________________________^ | ||
error: #[postgres(allow_mismatch)] may only be applied to enums | ||
--> src/compile-fail/invalid-allow-mismatch.rs:20:1 | ||
| | ||
20 | / #[postgres(allow_mismatch)] | ||
21 | | struct FromSqlAllowMismatchTupleStruct(i32, i32); | ||
| |_________________________________________________^ | ||
error: #[postgres(transparent)] is not allowed with #[postgres(allow_mismatch)] | ||
--> src/compile-fail/invalid-allow-mismatch.rs:24:25 | ||
| | ||
24 | #[postgres(transparent, allow_mismatch)] | ||
| ^^^^^^^^^^^^^^ | ||
error: #[postgres(allow_mismatch)] is not allowed with #[postgres(transparent)] | ||
--> src/compile-fail/invalid-allow-mismatch.rs:28:28 | ||
| | ||
28 | #[postgres(allow_mismatch, transparent)] | ||
| ^^^^^^^^^^^ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -31,31 +31,37 @@ pub fn domain_body(name: &str, field: &syn::Field) -> TokenStream { | ||
} | ||
} | ||
pub fn enum_body(name: &str, variants: &[Variant], allow_mismatch: bool) -> TokenStream { | ||
let num_variants = variants.len(); | ||
let variant_names = variants.iter().map(|v| &v.name); | ||
if allow_mismatch { | ||
quote! { | ||
type_.name() == #name | ||
} | ||
} else { | ||
quote! { | ||
if type_.name() != #name { | ||
return false; | ||
} | ||
match *type_.kind() { | ||
::postgres_types::Kind::Enum(ref variants) => { | ||
if variants.len() != #num_variants { | ||
return false; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'd rather just not generate the match below at all if allow_mismatch is set. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. true, fixed! | ||
variants.iter().all(|v| { | ||
match &**v { | ||
#( | ||
#variant_names => true, | ||
)* | ||
_ => false, | ||
} | ||
}) | ||
} | ||
_ => false, | ||
} | ||
} | ||
} | ||
} | ||