pax_global_header00006660000000000000000000000064146750615210014521gustar00rootroot0000000000000052 comment=9760a462833907f6b40a41e95358f38f47e35838 rust-gtk3-macros-0.18-0.18.0/000077500000000000000000000000001467506152100153205ustar00rootroot00000000000000rust-gtk3-macros-0.18-0.18.0/.cargo_vcs_info.json000066400000000000000000000001511467506152100212470ustar00rootroot00000000000000{ "git": { "sha1": "3ec2253489cded691d0ac63cba492c379287ee9a" }, "path_in_vcs": "gtk3-macros" }rust-gtk3-macros-0.18-0.18.0/COPYRIGHT000066400000000000000000000012131467506152100166100ustar00rootroot00000000000000The gtk-rs Project is licensed under the MIT license, see the LICENSE file or . Copyrights in the gtk-rs Project project are retained by their contributors. No copyright assignment is required to contribute to the gtk-rs Project project. For full authorship information, see the version control history. This project provides interoperability with various GNOME libraries but doesn't distribute any parts of them. Distributing compiled libraries and executables that link to those libraries may be subject to terms of the GNU LGPL or other licenses. For more information check the license of each GNOME library. rust-gtk3-macros-0.18-0.18.0/Cargo.toml000066400000000000000000000024661467506152100172600ustar00rootroot00000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.70" name = "gtk3-macros" version = "0.18.0" authors = ["The gtk-rs Project Developers"] exclude = [ "gir-files/*", "src/composite_template.ui", ] description = "Rust bindings for the GTK 3 library" homepage = "https://gtk-rs.org/" documentation = "https://gtk-rs.org/gtk3-rs/stable/latest/docs/gtk3_macros/" keywords = [ "gtk", "gtk3", "gtk-rs", "gnome", "GUI", ] categories = [ "api-bindings", "gui", ] license = "MIT" repository = "https://github.com/gtk-rs/gtk3-rs" resolver = "1" [lib] proc-macro = true [dependencies.proc-macro-crate] version = "1.0" [dependencies.proc-macro-error] version = "1.0" [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.syn] version = "2.0" features = ["full"] [dev-dependencies.gtk] version = "0.18" rust-gtk3-macros-0.18-0.18.0/Cargo.toml.orig000066400000000000000000000013571467506152100202150ustar00rootroot00000000000000[package] documentation = "https://gtk-rs.org/gtk3-rs/stable/latest/docs/gtk3_macros/" categories = ["api-bindings", "gui"] license = "MIT" description = "Rust bindings for the GTK 3 library" homepage = "https://gtk-rs.org/" name = "gtk3-macros" version = "0.18.0" authors = ["The gtk-rs Project Developers"] edition = "2021" keywords = ["gtk", "gtk3", "gtk-rs", "gnome", "GUI"] repository = "https://github.com/gtk-rs/gtk3-rs" exclude = [ "gir-files/*", "src/composite_template.ui" ] rust-version = "1.70" [lib] proc-macro = true [dependencies] proc-macro-error = "1.0" proc-macro2 = "1.0" quote = "1.0" syn = { version = "2.0", features = ["full"] } proc-macro-crate = "1.0" [dev-dependencies] gtk = { version = "0.18", path = "../gtk" } rust-gtk3-macros-0.18-0.18.0/LICENSE000066400000000000000000000020001467506152100163150ustar00rootroot00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rust-gtk3-macros-0.18-0.18.0/src/000077500000000000000000000000001467506152100161075ustar00rootroot00000000000000rust-gtk3-macros-0.18-0.18.0/src/attribute_parser.rs000066400000000000000000000134211467506152100220350ustar00rootroot00000000000000// Take a look at the license at the top of the repository in the LICENSE file. use proc_macro2::Span; use syn::spanned::Spanned; use syn::{ parse::{Error, Parse, ParseStream}, punctuated::Punctuated, }; use syn::{Attribute, DeriveInput, Field, Fields, Ident, LitStr, Meta, Token, Type}; mod kw { syn::custom_keyword!(file); syn::custom_keyword!(resource); syn::custom_keyword!(string); syn::custom_keyword!(id); } pub enum TemplateSource { File(String), Resource(String), String(String), } impl Parse for TemplateSource { fn parse(input: ParseStream<'_>) -> syn::Result { let lookahead = input.lookahead1(); let variant = if lookahead.peek(kw::file) { let _: kw::file = input.parse()?; TemplateSource::File } else if lookahead.peek(kw::resource) { let _: kw::resource = input.parse()?; TemplateSource::Resource } else if lookahead.peek(kw::string) { let _: kw::string = input.parse()?; TemplateSource::String } else { return Err(lookahead.error()); }; let _: Token![=] = input.parse()?; let lit: LitStr = input.parse()?; Ok(variant(lit.value())) } } #[derive(Debug)] pub enum ParseTemplateSourceError { MissingAttribute, Parse(syn::Error), } impl std::fmt::Display for ParseTemplateSourceError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::MissingAttribute => write!(f, "Missing 'template' attribute"), Self::Parse(err) => write!(f, "{}", err), } } } impl std::error::Error for ParseTemplateSourceError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { Self::MissingAttribute => None, Self::Parse(err) => Some(err), } } } pub fn parse_template_source( input: &DeriveInput, ) -> Result { input .attrs .iter() .find(|a| a.path().is_ident("template")) .ok_or(ParseTemplateSourceError::MissingAttribute)? .parse_args() .map_err(ParseTemplateSourceError::Parse) } pub enum FieldAttributeArg { Id(String), } impl Parse for FieldAttributeArg { fn parse(input: ParseStream<'_>) -> syn::Result { let lookahead = input.lookahead1(); if lookahead.peek(kw::id) { let _: kw::id = input.parse()?; let _: Token![=] = input.parse()?; let lit: LitStr = input.parse()?; Ok(Self::Id(lit.value())) } else { Err(lookahead.error()) } } } #[derive(Debug)] pub enum FieldAttributeType { TemplateChild, } pub struct FieldAttribute { pub ty: FieldAttributeType, pub args: Vec, pub path_span: Span, pub span: Span, } pub struct AttributedField { pub ident: Ident, pub ty: Type, pub attr: FieldAttribute, } fn parse_field_attr_args(attr: &Attribute) -> Result, Error> { let mut field_attribute_args = Vec::new(); match &attr.meta { Meta::List(list) => { let args = list.parse_args_with(Punctuated::::parse_terminated)?; for arg in args { for prev_arg in &field_attribute_args { // Comparison of enum variants, not data if std::mem::discriminant(prev_arg) == std::mem::discriminant(&arg) { return Err(Error::new( attr.span(), "two instances of the same attribute \ argument, each argument must be specified only once", )); } } field_attribute_args.push(arg); } } Meta::Path(_) => (), meta => { return Err(Error::new( meta.span(), "invalid attribute argument type, expected `name = value` list or nothing", )) } } Ok(field_attribute_args) } fn parse_field(field: &Field) -> Result, Error> { let field_attrs = &field.attrs; let ident = match &field.ident { Some(ident) => ident, None => return Err(Error::new(field.span(), "expected identifier")), }; let ty = &field.ty; let mut attr = None; for field_attr in field_attrs { let span = field_attr.span(); let path_span = field_attr.path().span(); let ty = if field_attr.path().is_ident("template_child") { Some(FieldAttributeType::TemplateChild) } else { None }; if let Some(ty) = ty { let args = parse_field_attr_args(field_attr)?; if attr.is_none() { attr = Some(FieldAttribute { ty, args, path_span, span, }) } else { return Err(Error::new( span, "multiple attributes on the same field are not supported", )); } } } if let Some(attr) = attr { Ok(Some(AttributedField { ident: ident.clone(), ty: ty.clone(), attr, })) } else { Ok(None) } } pub fn parse_fields(fields: &Fields) -> Result, Error> { let mut attributed_fields = Vec::new(); for field in fields { if !field.attrs.is_empty() { if let Some(attributed_field) = parse_field(field)? { attributed_fields.push(attributed_field) } } } Ok(attributed_fields) } rust-gtk3-macros-0.18-0.18.0/src/composite_template_derive.rs000066400000000000000000000051161467506152100237130ustar00rootroot00000000000000// Take a look at the license at the top of the repository in the LICENSE file. use proc_macro2::TokenStream; use proc_macro_error::{abort, abort_call_site}; use quote::quote; use syn::Data; use std::string::ToString; use crate::attribute_parser::*; use crate::util::*; fn gen_set_template(source: TemplateSource) -> TokenStream { match source { TemplateSource::File(file) => quote! { let t = include_bytes!(#file); klass.set_template(t); }, TemplateSource::Resource(resource) => quote! { klass.set_template_from_resource(&#resource); }, TemplateSource::String(template) => quote! { klass.set_template(&#template); }, } } fn gen_template_child_bindings(fields: &syn::Fields) -> TokenStream { let crate_ident = crate_ident_new(); let attributed_fields = match parse_fields(fields) { Ok(fields) => fields, Err(err) => abort!(err.span(), err.to_string()), }; let recurse = attributed_fields.iter().map(|field| match field.attr.ty { FieldAttributeType::TemplateChild => { let mut value_id = &field.ident.to_string(); let ident = &field.ident; field.attr.args.iter().for_each(|arg| match arg { FieldAttributeArg::Id(value) => { value_id = value; } }); quote! { klass.bind_template_child_with_offset( &#value_id, #crate_ident::offset_of!(Self => #ident), ); } } }); quote! { #(#recurse)* } } pub fn impl_composite_template(input: &syn::DeriveInput) -> TokenStream { let name = &input.ident; let crate_ident = crate_ident_new(); let source = match parse_template_source(input) { Ok(v) => v, Err(e) => abort_call_site!( "{}: derive(CompositeTemplate) requires #[template(...)] to specify 'file', 'resource', or 'string'", e ), }; let set_template = gen_set_template(source); let fields = match input.data { Data::Struct(ref s) => &s.fields, _ => abort_call_site!("derive(CompositeTemplate) only supports structs"), }; let template_children = gen_template_child_bindings(fields); quote! { impl #crate_ident::subclass::widget::CompositeTemplate for #name { fn bind_template(klass: &mut Self::Class) { #set_template unsafe { #template_children } } } } } rust-gtk3-macros-0.18-0.18.0/src/lib.rs000066400000000000000000000057641467506152100172370ustar00rootroot00000000000000// Take a look at the license at the top of the repository in the LICENSE file. mod attribute_parser; mod composite_template_derive; mod util; use proc_macro::TokenStream; use proc_macro_error::proc_macro_error; use syn::{parse_macro_input, DeriveInput}; /// Derive macro for using a composite template in a widget. /// /// The `template` attribute specifies where the template should be loaded /// from; it can be a `file`, a `resource`, or a `string`. /// /// The `template_child` attribute is used to mark all internal widgets /// we need to have programmatic access to. /// /// # Example /// /// Specify that `MyWidget` is using a composite template and load the /// template file the `composite_template.ui` file. /// /// Then, in the [`ObjectSubclass`] implementation you will need to call /// [`bind_template`] in the [`class_init`] function, and [`init_template`] in /// [`instance_init`] function. /// /// [`ObjectSubclass`]: ../glib/subclass/types/trait.ObjectSubclass.html /// [`bind_template`]: ../gtk/subclass/widget/trait.CompositeTemplate.html#tymethod.bind_template /// [`class_init`]: ../glib/subclass/types/trait.ObjectSubclass.html#method.class_init /// [`init_template`]: ../gtk/prelude/trait.InitializingWidgetExt.html#tymethod.init_template /// [`instance_init`]: ../glib/subclass/types/trait.ObjectSubclass.html#method.instance_init /// /// ```no_run /// use gtk::prelude::*; /// use gtk::glib; /// use gtk::subclass::prelude::*; /// /// mod imp { /// use super::*; /// /// #[derive(Debug, Default, gtk::CompositeTemplate)] /// #[template(file = "composite_template.ui")] /// pub struct MyWidget { /// #[template_child] /// pub label: TemplateChild, /// #[template_child(id = "my_button_id")] /// pub button: TemplateChild, /// } /// /// #[glib::object_subclass] /// impl ObjectSubclass for MyWidget { /// const NAME: &'static str = "MyWidget"; /// type Type = super::MyWidget; /// type ParentType = gtk::Box; /// /// fn class_init(klass: &mut Self::Class) { /// Self::bind_template(klass); /// } /// /// fn instance_init(obj: &glib::subclass::InitializingObject) { /// obj.init_template(); /// } /// } /// /// impl ObjectImpl for MyWidget {} /// impl WidgetImpl for MyWidget {} /// impl ContainerImpl for MyWidget {} /// impl BoxImpl for MyWidget {} /// } /// /// glib::wrapper! { /// pub struct MyWidget(ObjectSubclass) @extends gtk::Widget, gtk::Container, gtk::Box; /// } /// /// impl MyWidget { /// pub fn new() -> Self { /// glib::Object::new() /// } /// } /// # fn main() {} /// ``` #[proc_macro_derive(CompositeTemplate, attributes(template, template_child))] #[proc_macro_error] pub fn composite_template_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let gen = composite_template_derive::impl_composite_template(&input); gen.into() } rust-gtk3-macros-0.18-0.18.0/src/util.rs000066400000000000000000000007201467506152100174310ustar00rootroot00000000000000// Take a look at the license at the top of the repository in the LICENSE file. use proc_macro2::{Ident, Span}; use proc_macro_crate::crate_name; pub fn crate_ident_new() -> Ident { use proc_macro_crate::FoundCrate; let crate_name = match crate_name("gtk").expect("missing gtk dependency in `Cargo.toml`") { FoundCrate::Name(name) => name, FoundCrate::Itself => "gtk".to_owned(), }; Ident::new(&crate_name, Span::call_site()) }