]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support `use ...::self` imports
authorMatt Corallo <git@bluematt.me>
Sat, 28 May 2022 01:06:56 +0000 (01:06 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 28 Jun 2022 16:52:23 +0000 (16:52 +0000)
Previously we'd supported `use self:...` but this adds support for
using some path that ends with `self`.

c-bindings-gen/src/types.rs

index 772c34b1ea278e146a1e3c7a41350f8238a92b93..c532b8136dcf7fe9203efe67e8016f203f413605 100644 (file)
@@ -438,6 +438,10 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                                        new_path = format!("{}::{}{}", crate_name, $ident, $path_suffix);
                                        let crate_name_ident = format_ident!("{}", crate_name);
                                        path.push(parse_quote!(#crate_name_ident));
+                               } else if format!("{}", $ident) == "self" {
+                                       let mut path_iter = partial_path.rsplitn(2, "::");
+                                       path_iter.next().unwrap();
+                                       new_path = path_iter.next().unwrap().to_owned();
                                } else {
                                        new_path = format!("{}{}{}", partial_path, $ident, $path_suffix);
                                }
@@ -452,7 +456,8 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                        },
                        syn::UseTree::Name(n) => {
                                push_path!(n.ident, "");
-                               imports.insert(n.ident.clone(), (new_path, syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path }));
+                               let imported_ident = syn::Ident::new(new_path.rsplitn(2, "::").next().unwrap(), Span::call_site());
+                               imports.insert(imported_ident, (new_path, syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path }));
                        },
                        syn::UseTree::Group(g) => {
                                for i in g.items.iter() {