1use crate::syntax::symbol::Segment; 2use crate::syntax::{Lifetimes, NamedType, Pair, Symbol}; 3use proc_macro2::{Ident, Span}; 4use std::fmt::{self, Display}; 5use std::iter; 6use syn::ext::IdentExt; 7use syn::parse::{Error, Parser, Result}; 8use syn::punctuated::Punctuated; 9 10#[derive(Clone)] 11pub struct ForeignName { 12 text: String, 13} 14 15impl Pair { 16 pub fn to_symbol(&self) -> Symbol { 17 let segments = self 18 .namespace 19 .iter() 20 .map(|ident| ident as &dyn Segment) 21 .chain(iter::once(&self.cxx as &dyn Segment)); 22 Symbol::from_idents(segments) 23 } 24} 25 26impl NamedType { 27 pub fn new(rust: Ident) -> Self { 28 let generics = Lifetimes { 29 lt_token: None, 30 lifetimes: Punctuated::new(), 31 gt_token: None, 32 }; 33 NamedType { rust, generics } 34 } 35 36 pub fn span(&self) -> Span { 37 self.rust.span() 38 } 39} 40 41impl ForeignName { 42 pub fn parse(text: &str, span: Span) -> Result<Self> { 43 // TODO: support C++ names containing whitespace (`unsigned int`) or 44 // non-alphanumeric characters (`operator++`). 45 match Ident::parse_any.parse_str(text) { 46 Ok(ident) => { 47 let text = ident.to_string(); 48 Ok(ForeignName { text }) 49 } 50 Err(err) => Err(Error::new(span, err)), 51 } 52 } 53} 54 55impl Display for ForeignName { 56 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 57 formatter.write_str(&self.text) 58 } 59} 60 61impl PartialEq<str> for ForeignName { 62 fn eq(&self, rhs: &str) -> bool { 63 self.text == rhs 64 } 65} 66