Debian Codegraph @

src/error_domain_derive.rs

raw symbols SCIP
1 // Take a look at the license at the top of the repository in the LICENSE file.
2
3 use proc_macro2rust-analyzer cargo proc-macro2 1.0.106 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#;
4 use quoterust-analyzer cargo quote 1.0.44 crate/::quoterust-analyzer cargo quote 1.0.44 quote!;
5 use synrust-analyzer cargo syn 2.0.117 crate/::Datarust-analyzer cargo syn 2.0.117 derive/Data#;
6
7 use craterust-analyzer cargo glib-macros 0.22.6 crate/::utilsrust-analyzer cargo glib-macros 0.22.6 utils/::{NestedMetaItemrust-analyzer cargo glib-macros 0.22.6 utils/NestedMetaItem#, crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new()., gen_enum_from_glibrust-analyzer cargo glib-macros 0.22.6 utils/gen_enum_from_glib()., parse_nested_meta_itemsrust-analyzer cargo glib-macros 0.22.6 utils/parse_nested_meta_items().};
8
9 pub fn impl_error_domainrust-analyzer cargo glib-macros 0.22.6 error_domain_derive/impl_error_domain().(input: &synrust-analyzer cargo syn 2.0.117 crate/::DeriveInputrust-analyzer cargo syn 2.0.117 derive/DeriveInput#) -> synrust-analyzer cargo syn 2.0.117 crate/::Resultrust-analyzer cargo syn 2.0.117 error/Result#<TokenStreamrust-analyzer cargo proc-macro2 1.0.106 TokenStream#> {
10 let name = &input.identrust-analyzer cargo syn 2.0.117 derive/DeriveInput#ident.;
11
12 let enum_variants = match input.datarust-analyzer cargo syn 2.0.117 derive/DeriveInput#data. {
13 Datarust-analyzer cargo syn 2.0.117 derive/Data#::Enumrust-analyzer cargo syn 2.0.117 derive/Data#Enum#(ref e) => &e.variantsrust-analyzer cargo syn 2.0.117 derive/DataEnum#variants.,
14 _ => {
15 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.117 crate/::Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().(
16 input,
17 "#[derive(glib::ErrorDomain)] only supports enums",
18 ));
19 }
20 };
21
22 let mut domain_name = NestedMetaItemrust-analyzer cargo glib-macros 0.22.6 utils/NestedMetaItem#::<synrust-analyzer cargo syn 2.0.117 crate/::LitStrrust-analyzer cargo syn 2.0.117 lit/LitStr#>::newrust-analyzer cargo glib-macros 0.22.6 utils/impl#[`NestedMetaItem<T>`]new().("name")
23 .requiredrust-analyzer cargo glib-macros 0.22.6 utils/impl#[`NestedMetaItem<T>`]required().()
24 .value_requiredrust-analyzer cargo glib-macros 0.22.6 utils/impl#[`NestedMetaItem<T>`]value_required().();
25 let found = parse_nested_meta_itemsrust-analyzer cargo glib-macros 0.22.6 utils/parse_nested_meta_items().(&input.attrsrust-analyzer cargo syn 2.0.117 derive/DeriveInput#attrs., "error_domain", &mut [&mut domain_name])?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
26
27 if found.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
28 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(synrust-analyzer cargo syn 2.0.117 crate/::Errorrust-analyzer cargo syn 2.0.117 error/Error#::new_spannedrust-analyzer cargo syn 2.0.117 error/impl#[Error]new_spanned().(
29 input,
30 "#[derive(glib::ErrorDomain)] requires #[error_domain(name = \"domain-name\")]",
31 ));
32 };
33 let domain_name = domain_name.valuerust-analyzer cargo glib-macros 0.22.6 utils/NestedMetaItem#value..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
34 let crate_ident = crate_ident_newrust-analyzer cargo glib-macros 0.22.6 utils/crate_ident_new().();
35
36 let from_glib = gen_enum_from_glibrust-analyzer cargo glib-macros 0.22.6 utils/gen_enum_from_glib().(name, enum_variants);
37
38 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(quoterust-analyzer cargo quote 1.0.44 quote!! {
39 impl #crate_ident::error::ErrorDomain for #name {
40 #[inline]
41 fn domain() -> #crate_ident::Quark {
42 use #crate_ident::translate::from_glib;
43
44 static QUARK: ::std::sync::OnceLock<#crate_ident::Quark> = ::std::sync::OnceLock::new();
45 *QUARK.get_or_init(|| unsafe {
46 from_glib(#crate_ident::ffi::g_quark_from_static_string(concat!(#domain_name, "\0") as *const ::core::primitive::str as *const _))
47 })
48 }
49
50 #[inline]
51 fn code(self) -> i32 {
52 self as i32
53 }
54
55 #[inline]
56 fn from(value: i32) -> ::core::option::Option<Self>
57 where
58 Self: ::std::marker::Sized
59 {
60 #from_glib
61 }
62 }
63 })
64 }