1 // Take a look at the license at the top of the repository in the LICENSE file.
2 
3 use proc_macro2::TokenStream;
4 use proc_macro_error::abort_call_site;
5 use quote::quote;
6 use syn::Data;
7 
8 use crate::utils::{crate_ident_new, gen_enum_from_glib, parse_name};
9 
impl_gerror_domain(input: &syn::DeriveInput) -> TokenStream10 pub fn impl_gerror_domain(input: &syn::DeriveInput) -> TokenStream {
11     let name = &input.ident;
12 
13     let crate_ident = crate_ident_new();
14 
15     let enum_variants = match input.data {
16         Data::Enum(ref e) => &e.variants,
17         _ => abort_call_site!("GErrorDomain only supports enums"),
18     };
19 
20     let domain_name = match parse_name(input, "gerror_domain") {
21         Ok(v) => v,
22         Err(e) => abort_call_site!(
23             "{}: derive(GErrorDomain) requires #[gerror_domain(name = \"DomainName\")]",
24             e
25         ),
26     };
27     let from_glib = gen_enum_from_glib(name, enum_variants);
28 
29     quote! {
30         impl #crate_ident::error::ErrorDomain for #name {
31             fn domain() -> #crate_ident::Quark {
32                 use #crate_ident::translate::from_glib;
33 
34                 static QUARK: #crate_ident::once_cell::sync::Lazy<#crate_ident::Quark> =
35                     #crate_ident::once_cell::sync::Lazy::new(|| unsafe {
36                         from_glib(#crate_ident::ffi::g_quark_from_static_string(concat!(#domain_name, "\0") as *const str as *const _))
37                     });
38                 *QUARK
39             }
40 
41             fn code(self) -> i32 {
42                 self as i32
43             }
44 
45             fn from(value: i32) -> Option<Self>
46             where
47                 Self: Sized
48             {
49                 #from_glib
50             }
51         }
52     }
53 }
54