Skip to content

Commit

Permalink
Merge pull request #316 from ModProg/update-attribute-derive
Browse files Browse the repository at this point in the history
Update attribute-derive
  • Loading branch information
ecton authored Mar 19, 2024
2 parents 5518fc2 + 30b3316 commit f0def27
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 45 deletions.
68 changes: 58 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/bonsaidb-files/src/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ where
schema::file::File::<Config>::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
Expand Down Expand Up @@ -348,7 +348,7 @@ where
schema::file::File::<Config>::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
Expand Down
2 changes: 1 addition & 1 deletion crates/bonsaidb-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
50 changes: 29 additions & 21 deletions crates/bonsaidb-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
)]
#![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};
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::{
Expand Down Expand Up @@ -60,7 +62,7 @@ fn core_path() -> Path {
}
}

#[derive(Attribute)]
#[derive(FromAttr)]
#[attribute(ident = collection)]
struct CollectionAttribute {
authority: Option<Expr>,
Expand Down Expand Up @@ -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\"")]
Expand Down Expand Up @@ -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")]
Expand All @@ -348,22 +350,28 @@ enum NullHandling {
Deny,
}

impl ConvertParsed for NullHandling {
type Type = Ident;
impl AttributeBase for NullHandling {
type Partial = Self;
}

fn convert(value: Self::Type) -> syn::Result<Self> {
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<SpannedValue<Self>> {
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(),
))
}
}

Expand Down Expand Up @@ -848,7 +856,7 @@ pub fn key_derive(input: proc_macro::TokenStream) -> Result {
})
}

#[derive(Attribute)]
#[derive(FromAttr)]
#[attribute(ident = api)]
struct ApiAttribute {
#[attribute(example = "\"name\"")]
Expand Down Expand Up @@ -933,7 +941,7 @@ fn files_path() -> Path {
}
}

#[derive(Attribute)]
#[derive(FromAttr)]
#[attribute(ident = file_config)]
struct FileConfigAttribute {
#[attribute(example = "MetadataType")]
Expand Down
6 changes: 3 additions & 3 deletions crates/bonsaidb-macros/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use attribute_derive::Attribute;
use attribute_derive::FromAttr;
use manyhow::Result;
use proc_macro2::TokenStream;
use quote::quote;
Expand All @@ -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")]
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn derive(
})
}

#[derive(Attribute)]
#[derive(FromAttr)]
#[attribute(ident = view_schema)]
struct ViewSchemaAttribute {
#[attribute(example = "ViewType")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion crates/bonsaidb-macros/tests/ui/schema/missing_name.stderr
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/bonsaidb-macros/tests/ui/view/missing_key.stderr
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/bonsaidb-macros/tests/ui/view/missing_name.stderr
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down

0 comments on commit f0def27

Please sign in to comment.