From 0242a4c25fbdae3a4c38520293f06171ce5bf13f Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 24 Jul 2022 00:05:31 -0700 Subject: [PATCH 1/8] Add tests capturing existing behavior --- tests/from_meta.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/from_meta.rs diff --git a/tests/from_meta.rs b/tests/from_meta.rs new file mode 100644 index 0000000..294035a --- /dev/null +++ b/tests/from_meta.rs @@ -0,0 +1,40 @@ +use darling::FromMeta; +use syn::parse_quote; + +#[derive(FromMeta)] +struct Meta { + #[darling(default)] + meta1: Option, + #[darling(default)] + meta2: bool, +} + +#[test] +fn nested_meta_meta_value() { + let meta = Meta::from_list(&vec![parse_quote! { + meta1 = "thefeature" + }]) + .unwrap(); + assert_eq!(meta.meta1, Some("thefeature".to_string())); + assert_eq!(meta.meta2, false); +} + +#[test] +fn nested_meta_meta_bool() { + let meta = Meta::from_list(&vec![parse_quote! { + meta2 + }]) + .unwrap(); + assert_eq!(meta.meta1, None); + assert_eq!(meta.meta2, true); +} + +#[test] +fn nested_meta_lit_errors() { + let meta = Meta::from_list(&vec![parse_quote! { + "meta2" + }]) + .unwrap(); + assert_eq!(meta.meta1, None); + assert_eq!(meta.meta2, false); +} From 8224ffbd5c15097d45c2c8f839d7b38f0fdc455d Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 24 Jul 2022 00:15:11 -0700 Subject: [PATCH 2/8] write a failing test for desired behavior --- tests/from_meta.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/from_meta.rs b/tests/from_meta.rs index 294035a..d8daf3f 100644 --- a/tests/from_meta.rs +++ b/tests/from_meta.rs @@ -1,7 +1,7 @@ -use darling::FromMeta; +use darling::{Error, FromMeta}; use syn::parse_quote; -#[derive(FromMeta)] +#[derive(Debug, FromMeta)] struct Meta { #[darling(default)] meta1: Option, @@ -31,10 +31,9 @@ fn nested_meta_meta_bool() { #[test] fn nested_meta_lit_errors() { - let meta = Meta::from_list(&vec![parse_quote! { + let err = Meta::from_list(&vec![parse_quote! { "meta2" }]) - .unwrap(); - assert_eq!(meta.meta1, None); - assert_eq!(meta.meta2, false); + .unwrap_err(); + assert_eq!(err.to_string(), Error::unknown_value("meta2").to_string()); } From f97a0b6d980a60f36ac6d63a3f216a73d315132b Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 24 Jul 2022 08:32:16 -0700 Subject: [PATCH 3/8] expect unsupported literal instead which is more consistent with other error generation in darling --- tests/from_meta.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/from_meta.rs b/tests/from_meta.rs index d8daf3f..c38d6d5 100644 --- a/tests/from_meta.rs +++ b/tests/from_meta.rs @@ -35,5 +35,5 @@ fn nested_meta_lit_errors() { "meta2" }]) .unwrap_err(); - assert_eq!(err.to_string(), Error::unknown_value("meta2").to_string()); + assert_eq!(err.to_string(), Error::unsupported_format("literal").to_string()); } From 385c547e87e0749ad5d7c504be28aff96da0c0ca Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 24 Jul 2022 08:32:43 -0700 Subject: [PATCH 4/8] handle the literal case --- core/src/codegen/variant_data.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/codegen/variant_data.rs b/core/src/codegen/variant_data.rs index c36a263..54c1dc0 100644 --- a/core/src/codegen/variant_data.rs +++ b/core/src/codegen/variant_data.rs @@ -64,6 +64,8 @@ impl<'a> FieldsGen<'a> { #(#arms)* __other => { #handle_unknown } } + } else { + __errors.push(::darling::Error::unsupported_format("literal").with_span(__item)); } } ) From a9cf414c34c8c4a343761a7642225218500c2e39 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 24 Jul 2022 08:36:53 -0700 Subject: [PATCH 5/8] refactor if-else to match --- core/src/codegen/variant_data.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/core/src/codegen/variant_data.rs b/core/src/codegen/variant_data.rs index 54c1dc0..8b86b1f 100644 --- a/core/src/codegen/variant_data.rs +++ b/core/src/codegen/variant_data.rs @@ -58,14 +58,17 @@ impl<'a> FieldsGen<'a> { quote!( for __item in __items { - if let ::syn::NestedMeta::Meta(ref __inner) = *__item { - let __name = ::darling::util::path_to_string(__inner.path()); - match __name.as_str() { - #(#arms)* - __other => { #handle_unknown } + match *__item { + ::syn::NestedMeta::Meta(ref __inner) => { + let __name = ::darling::util::path_to_string(__inner.path()); + match __name.as_str() { + #(#arms)* + __other => { #handle_unknown } + } + } + ::syn::NestedMeta::Lit(_) => { + __errors.push(::darling::Error::unsupported_format("literal").with_span(__item)); } - } else { - __errors.push(::darling::Error::unsupported_format("literal").with_span(__item)); } } ) From 04ef35178562bcec5633b0b041795579edf58687 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 24 Jul 2022 08:37:52 -0700 Subject: [PATCH 6/8] fix fmting of test --- tests/from_meta.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/from_meta.rs b/tests/from_meta.rs index c38d6d5..76a217e 100644 --- a/tests/from_meta.rs +++ b/tests/from_meta.rs @@ -35,5 +35,8 @@ fn nested_meta_lit_errors() { "meta2" }]) .unwrap_err(); - assert_eq!(err.to_string(), Error::unsupported_format("literal").to_string()); + assert_eq!( + err.to_string(), + Error::unsupported_format("literal").to_string() + ); } From 6877e1cce2cf269c0b75e99460f96a90d5277b67 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 24 Jul 2022 08:40:13 -0700 Subject: [PATCH 7/8] add some some additional but non-exhaustive test cases --- tests/from_meta.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/from_meta.rs b/tests/from_meta.rs index 76a217e..7523de3 100644 --- a/tests/from_meta.rs +++ b/tests/from_meta.rs @@ -30,7 +30,7 @@ fn nested_meta_meta_bool() { } #[test] -fn nested_meta_lit_errors() { +fn nested_meta_lit_string_errors() { let err = Meta::from_list(&vec![parse_quote! { "meta2" }]) @@ -40,3 +40,27 @@ fn nested_meta_lit_errors() { Error::unsupported_format("literal").to_string() ); } + +#[test] +fn nested_meta_lit_integer_errors() { + let err = Meta::from_list(&vec![parse_quote! { + 2 + }]) + .unwrap_err(); + assert_eq!( + err.to_string(), + Error::unsupported_format("literal").to_string() + ); +} + +#[test] +fn nested_meta_lit_bool_errors() { + let err = Meta::from_list(&vec![parse_quote! { + true + }]) + .unwrap_err(); + assert_eq!( + err.to_string(), + Error::unsupported_format("literal").to_string() + ); +} From 5c6c206366f258e7865caf11f84f5c067a877284 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Sun, 24 Jul 2022 08:42:18 -0700 Subject: [PATCH 8/8] tighten span on literal --- core/src/codegen/variant_data.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/src/codegen/variant_data.rs b/core/src/codegen/variant_data.rs index 8b86b1f..b1f4450 100644 --- a/core/src/codegen/variant_data.rs +++ b/core/src/codegen/variant_data.rs @@ -66,8 +66,9 @@ impl<'a> FieldsGen<'a> { __other => { #handle_unknown } } } - ::syn::NestedMeta::Lit(_) => { - __errors.push(::darling::Error::unsupported_format("literal").with_span(__item)); + ::syn::NestedMeta::Lit(ref __inner) => { + __errors.push(::darling::Error::unsupported_format("literal") + .with_span(__inner)); } } }