blob: 50f6ebd1c6087345550c56ead3534fb17a5a8c5d [file] [log] [blame]
David Tolnay4a79b7f2020-12-30 19:48:42 -08001use crate::syntax::{Lifetimes, Pair, RustName, Symbol, Types};
David Tolnay60e7aa62020-11-01 12:34:51 -08002use proc_macro2::{Ident, Span};
David Tolnay8faec772020-11-02 00:18:19 -08003use std::iter;
David Tolnay4a79b7f2020-12-30 19:48:42 -08004use syn::punctuated::Punctuated;
David Tolnay60e7aa62020-11-01 12:34:51 -08005
6impl Pair {
David Tolnay8faec772020-11-02 00:18:19 -08007 pub fn to_symbol(&self) -> Symbol {
8 Symbol::from_idents(self.iter_all_segments())
9 }
10
11 pub fn to_fully_qualified(&self) -> String {
David Tolnaydee9fa32020-12-21 14:40:50 -080012 let mut fully_qualified = String::new();
13 for segment in self.iter_all_segments() {
14 fully_qualified += "::";
15 fully_qualified += &segment.to_string();
16 }
17 fully_qualified
David Tolnay8faec772020-11-02 00:18:19 -080018 }
19
20 fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> {
21 self.namespace.iter().chain(iter::once(&self.cxx))
22 }
David Tolnay60e7aa62020-11-01 12:34:51 -080023}
24
David Tolnay75ea17c2020-12-06 21:08:34 -080025impl RustName {
David Tolnay7cd9fec2020-12-21 15:03:18 -080026 pub fn new(rust: Ident) -> Self {
David Tolnay4a79b7f2020-12-30 19:48:42 -080027 let generics = Lifetimes {
28 lt_token: None,
29 lifetimes: Punctuated::new(),
30 gt_token: None,
31 };
32 RustName { rust, generics }
David Tolnay60e7aa62020-11-01 12:34:51 -080033 }
34
David Tolnay60e7aa62020-11-01 12:34:51 -080035 pub fn span(&self) -> Span {
36 self.rust.span()
37 }
38
39 pub fn to_symbol(&self, types: &Types) -> Symbol {
40 types.resolve(self).to_symbol()
41 }
42}