Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: glib-macros: port attribute parsing to use deluxe crate #970

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion glib-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ heck = "0.4"
proc-macro-error = "1.0"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ["full"], default-features = false }
syn = { version = "1.0", features = ["full", "visit-mut"], default-features = false }
proc-macro-crate = "1.0"
deluxe = "0.4.1"

[lib]
proc-macro = true

[dev-dependencies]
futures-channel = "0.3"
futures-executor = "0.3"
futures-util = "0.3"
glib = { path = "../glib" }
trybuild2 = "1.0"
once_cell = "1.9.0"
32 changes: 17 additions & 15 deletions glib-macros/src/boxed_derive.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use proc_macro2::{Ident, TokenStream};
use proc_macro_error::abort_call_site;
use quote::quote;

use crate::utils::{crate_ident_new, find_attribute_meta, find_nested_meta, parse_name};
use crate::utils::crate_ident_new;

fn gen_option_to_ptr() -> TokenStream {
quote! {
Expand Down Expand Up @@ -91,21 +90,22 @@ fn gen_impl_to_value_optional(name: &Ident, crate_ident: &TokenStream) -> TokenS
}
}

pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
let name = &input.ident;
#[derive(deluxe::ExtractAttributes, Default)]
#[deluxe(attributes(boxed_type))]
struct BoxedType {
name: String,
#[deluxe(default)]
nullable: bool,
}

let gtype_name = match parse_name(input, "boxed_type") {
Ok(name) => name,
Err(e) => abort_call_site!(
"{}: #[derive(glib::Boxed)] requires #[boxed_type(name = \"BoxedTypeName\")]",
e
),
};
pub fn impl_boxed(mut input: syn::DeriveInput) -> TokenStream {
let name = &input.ident;

let meta = find_attribute_meta(&input.attrs, "boxed_type")
.unwrap()
.unwrap();
let nullable = find_nested_meta(&meta, "nullable").is_some();
let errors = deluxe::Errors::new();
let BoxedType {
name: gtype_name,
nullable,
} = deluxe::extract_attributes_optional(&mut input.attrs, &errors);

let crate_ident = crate_ident_new();

Expand All @@ -121,6 +121,8 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
};

quote! {
#errors

impl #crate_ident::subclass::boxed::BoxedType for #name {
const NAME: &'static str = #gtype_name;
}
Expand Down
Loading