From 18e935d37e0791dbb3915af2a57adfec0da3eea0 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Sun, 17 Mar 2024 15:29:37 +0100 Subject: [PATCH 1/2] Update attribute-derive --- Cargo.lock | 68 ++++++++++++++++--- crates/bonsaidb-files/src/direct.rs | 4 +- crates/bonsaidb-macros/Cargo.toml | 2 +- crates/bonsaidb-macros/src/lib.rs | 52 ++++++++------ crates/bonsaidb-macros/src/view.rs | 6 +- .../ui/collection/invalid_attribute.stderr | 2 +- .../tests/ui/collection/missing_name.stderr | 2 +- .../tests/ui/schema/invalid_attribute.stderr | 2 +- .../tests/ui/schema/missing_name.stderr | 2 +- .../tests/ui/view/invalid_attribute.stderr | 2 +- .../tests/ui/view/missing_collection.stderr | 2 +- .../tests/ui/view/missing_key.stderr | 2 +- .../tests/ui/view/missing_name.stderr | 2 +- 13 files changed, 103 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 962e6e6cbf..efc1609875 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -336,11 +336,13 @@ dependencies = [ [[package]] name = "attribute-derive" -version = "0.8.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c94f43ede6f25dab1dea046bff84d85dea61bd49aba7a9011ad66c0d449077b" +checksum = "805868cbfccedb5b1a44a9b8d793ddacec69d73bb546a8806f0f9f0ac549bb9c" dependencies = [ "attribute-derive-macro", + "derive-where", + "manyhow 0.10.4", "proc-macro2", "quote", "syn 2.0.48", @@ -348,17 +350,17 @@ dependencies = [ [[package]] name = "attribute-derive-macro" -version = "0.8.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b409e2b2d2dc206d2c0ad3575a93f001ae21a1593e2d0c69b69c308e63f3b422" +checksum = "a5368498ceda51de3fdaf17363b38eb1dd4542e0e66aae15020be917483bc273" dependencies = [ "collection_literals", "interpolator", - "manyhow", + "manyhow 0.10.4", "proc-macro-utils", "proc-macro2", "quote", - "quote-use", + "quote-use 0.8.0", "syn 2.0.48", ] @@ -1181,11 +1183,11 @@ dependencies = [ "attribute-derive", "bonsaidb", "compiletest_rs", - "manyhow", + "manyhow 0.8.1", "proc-macro-crate", "proc-macro2", "quote", - "quote-use", + "quote-use 0.7.2", "serde", "syn 2.0.48", "transmog-bincode", @@ -3285,7 +3287,19 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "516b76546495d933baa165075b95c0a15e8f7ef75e53f56b19b7144d80fd52bd" dependencies = [ - "manyhow-macros", + "manyhow-macros 0.8.1", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "manyhow" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91ea592d76c0b6471965708ccff7e6a5d277f676b90ab31f4d3f3fc77fade64" +dependencies = [ + "manyhow-macros 0.10.4", "proc-macro2", "quote", "syn 2.0.48", @@ -3302,6 +3316,17 @@ dependencies = [ "quote", ] +[[package]] +name = "manyhow-macros" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c64621e2c08f2576e4194ea8be11daf24ac01249a4f53cd8befcbb7077120ead" +dependencies = [ + "proc-macro-utils", + "proc-macro2", + "quote", +] + [[package]] name = "match_cfg" version = "0.1.0" @@ -4078,10 +4103,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7b5abe3fe82fdeeb93f44d66a7b444dedf2e4827defb0a8e69c437b2de2ef94" dependencies = [ "quote", - "quote-use-macros", + "quote-use-macros 0.7.2", "syn 2.0.48", ] +[[package]] +name = "quote-use" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b393938dcaab992375d7b3df7887fa98cc91c2f3590598251e7c609e2b788139" +dependencies = [ + "quote", + "quote-use-macros 0.8.0", +] + [[package]] name = "quote-use-macros" version = "0.7.2" @@ -4094,6 +4129,19 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "quote-use-macros" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d8772387900c205780e2c240cfe4dd01355ab4f96a503d99bdf34ad73180ef" +dependencies = [ + "derive-where", + "proc-macro-utils", + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "radium" version = "0.7.0" diff --git a/crates/bonsaidb-files/src/direct.rs b/crates/bonsaidb-files/src/direct.rs index ed101a5d51..3b6de45f78 100644 --- a/crates/bonsaidb-files/src/direct.rs +++ b/crates/bonsaidb-files/src/direct.rs @@ -117,7 +117,7 @@ where schema::file::File::::summarize_recursive_path_contents(path, database) } - /// Return all direct descendents of this file. For example, consider this + /// Return all direct descendants of this file. For example, consider this /// list of files: /// /// - /top-level @@ -348,7 +348,7 @@ where schema::file::File::::summarize_recursive_path_contents_async(path, database).await } - /// Return all direct descendents of this file. For example, consider this + /// Return all direct descendants of this file. For example, consider this /// list of files: /// /// - /top-level diff --git a/crates/bonsaidb-macros/Cargo.toml b/crates/bonsaidb-macros/Cargo.toml index ff59a909a2..00d7b28cee 100644 --- a/crates/bonsaidb-macros/Cargo.toml +++ b/crates/bonsaidb-macros/Cargo.toml @@ -16,7 +16,7 @@ rust-version = "1.70" proc-macro = true [dependencies] -attribute-derive = "0.8.1" +attribute-derive = "0.9.0" proc-macro-crate = "2.0.0" proc-macro2 = { version = "1.0.37", features = ["nightly"] } quote = "1" diff --git a/crates/bonsaidb-macros/src/lib.rs b/crates/bonsaidb-macros/src/lib.rs index 775a65e3de..a85628498a 100644 --- a/crates/bonsaidb-macros/src/lib.rs +++ b/crates/bonsaidb-macros/src/lib.rs @@ -11,7 +11,8 @@ )] #![cfg_attr(doc, deny(rustdoc::all))] -use attribute_derive::{Attribute, ConvertParsed}; +use attribute_derive::parsing::{AttributeBase, AttributeValue, SpannedValue}; +use attribute_derive::{Attribute, FromAttr}; use manyhow::{bail, error_message, manyhow, JoinToTokensError, Result}; use proc_macro2::{Span, TokenStream}; use proc_macro_crate::{crate_name, FoundCrate}; @@ -19,6 +20,7 @@ use quote::{quote_spanned, ToTokens}; use quote_use::{ format_ident_namespaced as format_ident, parse_quote_use as parse_quote, quote_use as quote, }; +use syn::parse::ParseStream; use syn::punctuated::Punctuated; use syn::spanned::Spanned; use syn::{ @@ -60,7 +62,7 @@ fn core_path() -> Path { } } -#[derive(Attribute)] +#[derive(FromAttr)] #[attribute(ident = collection)] struct CollectionAttribute { authority: Option, @@ -264,7 +266,7 @@ pub fn view_schema_derive(input: proc_macro::TokenStream) -> Result { view::derive_schema(parse(input)?) } -#[derive(Attribute)] +#[derive(FromAttr)] #[attribute(ident = schema)] struct SchemaAttribute { #[attribute(example = "\"name\"")] @@ -328,7 +330,7 @@ pub fn schema_derive(input: proc_macro::TokenStream) -> Result { }) } -#[derive(Attribute)] +#[derive(FromAttr)] #[attribute(ident = key)] struct KeyAttribute { #[attribute(example = "bosaidb::core")] @@ -348,22 +350,30 @@ enum NullHandling { Deny, } -impl ConvertParsed for NullHandling { - type Type = Ident; +impl AttributeBase for NullHandling { + type Partial = Self; +} - fn convert(value: Self::Type) -> syn::Result { - if value == "escape" { - Ok(NullHandling::Escape) - } else if value == "allow" { - Ok(NullHandling::Allow) - } else if value == "deny" { - Ok(NullHandling::Deny) - } else { - Err(syn::Error::new( - Span::call_site(), - "only `escape`, `allow`, and `deny` are allowed for `null_handling`", - )) - } +impl AttributeValue for NullHandling { + fn parse_value( + input: ParseStream, + ) -> syn::Result> { + let ident: Ident = input.parse()?; + + Ok(SpannedValue::new( + match ident.to_string().as_str() { + "escape" => NullHandling::Escape, + "allow" => NullHandling::Allow, + "deny" => NullHandling::Deny, + _ => { + return Err(syn::Error::new( + Span::call_site(), + "only `escape`, `allow`, and `deny` are allowed for `null_handling`", + )) + } + }, + ident.span(), + )) } } @@ -848,7 +858,7 @@ pub fn key_derive(input: proc_macro::TokenStream) -> Result { }) } -#[derive(Attribute)] +#[derive(FromAttr)] #[attribute(ident = api)] struct ApiAttribute { #[attribute(example = "\"name\"")] @@ -933,7 +943,7 @@ fn files_path() -> Path { } } -#[derive(Attribute)] +#[derive(FromAttr)] #[attribute(ident = file_config)] struct FileConfigAttribute { #[attribute(example = "MetadataType")] diff --git a/crates/bonsaidb-macros/src/view.rs b/crates/bonsaidb-macros/src/view.rs index 757d4aae47..929ac6566a 100644 --- a/crates/bonsaidb-macros/src/view.rs +++ b/crates/bonsaidb-macros/src/view.rs @@ -1,4 +1,4 @@ -use attribute_derive::Attribute; +use attribute_derive::FromAttr; use manyhow::Result; use proc_macro2::TokenStream; use quote::quote; @@ -8,7 +8,7 @@ use syn::{DeriveInput, Ident, LitStr, Path, Type, TypeTuple}; use crate::core_path; -#[derive(Attribute)] +#[derive(FromAttr)] #[attribute(ident = view)] struct ViewAttribute { #[attribute(example = "CollectionType")] @@ -87,7 +87,7 @@ pub fn derive( }) } -#[derive(Attribute)] +#[derive(FromAttr)] #[attribute(ident = view_schema)] struct ViewSchemaAttribute { #[attribute(example = "ViewType")] diff --git a/crates/bonsaidb-macros/tests/ui/collection/invalid_attribute.stderr b/crates/bonsaidb-macros/tests/ui/collection/invalid_attribute.stderr index bd1bb86d1f..8ad86a3c6b 100644 --- a/crates/bonsaidb-macros/tests/ui/collection/invalid_attribute.stderr +++ b/crates/bonsaidb-macros/tests/ui/collection/invalid_attribute.stderr @@ -1,4 +1,4 @@ -error: expected identifier +error: supported fields are `authority`, `name`, `views`, `serialization`, `encryption_key`, `encryption_required`, `encryption_optional`, `primary_key`, `natural_id` and `core` --> tests/ui/collection/invalid_attribute.rs:4:48 | 4 | #[collection(name = "hi", authority = "hello", "hi")] diff --git a/crates/bonsaidb-macros/tests/ui/collection/missing_name.stderr b/crates/bonsaidb-macros/tests/ui/collection/missing_name.stderr index 0060e0f7a7..d40d48f560 100644 --- a/crates/bonsaidb-macros/tests/ui/collection/missing_name.stderr +++ b/crates/bonsaidb-macros/tests/ui/collection/missing_name.stderr @@ -1,6 +1,6 @@ error: required `name` is not specified - = help: try `#[collection(name="name")]` + = help: try `#[collection(name = "name")]` --> tests/ui/collection/missing_name.rs:3:10 | 3 | #[derive(Collection)] diff --git a/crates/bonsaidb-macros/tests/ui/schema/invalid_attribute.stderr b/crates/bonsaidb-macros/tests/ui/schema/invalid_attribute.stderr index 3c33649cd9..e8a066fe8d 100644 --- a/crates/bonsaidb-macros/tests/ui/schema/invalid_attribute.stderr +++ b/crates/bonsaidb-macros/tests/ui/schema/invalid_attribute.stderr @@ -1,4 +1,4 @@ -error: expected identifier +error: supported fields are `name`, `authority`, `collections`, `include` and `core` --> tests/ui/schema/invalid_attribute.rs:5:25 | 5 | #[schema(name = "name", "hi")] diff --git a/crates/bonsaidb-macros/tests/ui/schema/missing_name.stderr b/crates/bonsaidb-macros/tests/ui/schema/missing_name.stderr index bdd72fd4cb..7f65c4167a 100644 --- a/crates/bonsaidb-macros/tests/ui/schema/missing_name.stderr +++ b/crates/bonsaidb-macros/tests/ui/schema/missing_name.stderr @@ -1,6 +1,6 @@ error: required `name` is not specified - = help: try `#[schema(name="name")]` + = help: try `#[schema(name = "name")]` --> tests/ui/schema/missing_name.rs:3:10 | 3 | #[derive(Schema)] diff --git a/crates/bonsaidb-macros/tests/ui/view/invalid_attribute.stderr b/crates/bonsaidb-macros/tests/ui/view/invalid_attribute.stderr index bf44022db9..c57febb1bb 100644 --- a/crates/bonsaidb-macros/tests/ui/view/invalid_attribute.stderr +++ b/crates/bonsaidb-macros/tests/ui/view/invalid_attribute.stderr @@ -1,4 +1,4 @@ -error: expected identifier +error: supported fields are `collection`, `key`, `name`, `value`, `core` and `serialization` --> tests/ui/view/invalid_attribute.rs:4:21 | 4 | #[view(name = "hi", "hi")] diff --git a/crates/bonsaidb-macros/tests/ui/view/missing_collection.stderr b/crates/bonsaidb-macros/tests/ui/view/missing_collection.stderr index af785a4d3e..0e229a55e9 100644 --- a/crates/bonsaidb-macros/tests/ui/view/missing_collection.stderr +++ b/crates/bonsaidb-macros/tests/ui/view/missing_collection.stderr @@ -1,6 +1,6 @@ error: required `collection` is not specified - = help: try `#[view(collection=CollectionType)]` + = help: try `#[view(collection = CollectionType)]` --> tests/ui/view/missing_collection.rs:3:10 | 3 | #[derive(View)] diff --git a/crates/bonsaidb-macros/tests/ui/view/missing_key.stderr b/crates/bonsaidb-macros/tests/ui/view/missing_key.stderr index c1570f091a..b38dd52006 100644 --- a/crates/bonsaidb-macros/tests/ui/view/missing_key.stderr +++ b/crates/bonsaidb-macros/tests/ui/view/missing_key.stderr @@ -1,6 +1,6 @@ error: required `key` is not specified - = help: try `#[view(key=KeyType)]` + = help: try `#[view(key = KeyType)]` --> tests/ui/view/missing_key.rs:3:10 | 3 | #[derive(View)] diff --git a/crates/bonsaidb-macros/tests/ui/view/missing_name.stderr b/crates/bonsaidb-macros/tests/ui/view/missing_name.stderr index a613244c9c..17f822f8f4 100644 --- a/crates/bonsaidb-macros/tests/ui/view/missing_name.stderr +++ b/crates/bonsaidb-macros/tests/ui/view/missing_name.stderr @@ -1,6 +1,6 @@ error: required `name` is not specified - = help: try `#[collection(name="name")]` + = help: try `#[collection(name = "name")]` --> tests/ui/view/missing_name.rs:3:10 | 3 | #[derive(Collection)] From 30b3316965da11ed7106d88e61584b2ce03c8d59 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Sun, 17 Mar 2024 16:00:32 +0100 Subject: [PATCH 2/2] fix warning --- crates/bonsaidb-macros/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/bonsaidb-macros/src/lib.rs b/crates/bonsaidb-macros/src/lib.rs index a85628498a..bc93b5df42 100644 --- a/crates/bonsaidb-macros/src/lib.rs +++ b/crates/bonsaidb-macros/src/lib.rs @@ -355,9 +355,7 @@ impl AttributeBase for NullHandling { } impl AttributeValue for NullHandling { - fn parse_value( - input: ParseStream, - ) -> syn::Result> { + fn parse_value(input: ParseStream<'_>) -> syn::Result> { let ident: Ident = input.parse()?; Ok(SpannedValue::new(