| 1 | use proc_macro2::{Ident, Span, TokenStream}; |
| 2 | use quote::ToTokens; |
| 3 | use std::cmp::Ordering; |
| 4 | use std::fmt::{self, Display}; |
| 5 | use std::hash::{Hash, Hasher}; |
| 6 | use syn::ext::IdentExt as _; |
| 7 | use syn::parse::{Parse, ParseStream, Result}; |
| 8 | use syn::Index; |
| 9 | |
| 10 | #[derive (Clone)] |
| 11 | #[repr (transparent)] |
| 12 | pub struct IdentUnraw(Ident); |
| 13 | |
| 14 | impl IdentUnraw { |
| 15 | pub fn new(ident: Ident) -> Self { |
| 16 | IdentUnraw(ident) |
| 17 | } |
| 18 | |
| 19 | pub fn to_local(&self) -> Ident { |
| 20 | let unraw: Ident = self.0.unraw(); |
| 21 | let repr: String = unraw.to_string(); |
| 22 | if syn::parse_str::<Ident>(&repr).is_err() { |
| 23 | if let "_" | "super" | "self" | "Self" | "crate" = repr.as_str() { |
| 24 | // Some identifiers are never allowed to appear as raw, like r#self and r#_. |
| 25 | } else { |
| 26 | return Ident::new_raw(&repr, Span::call_site()); |
| 27 | } |
| 28 | } |
| 29 | unraw |
| 30 | } |
| 31 | |
| 32 | pub fn set_span(&mut self, span: Span) { |
| 33 | self.0.set_span(span); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl Display for IdentUnraw { |
| 38 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 39 | Display::fmt(&self.0.unraw(), f:formatter) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl Eq for IdentUnraw {} |
| 44 | |
| 45 | impl PartialEq for IdentUnraw { |
| 46 | fn eq(&self, other: &Self) -> bool { |
| 47 | PartialEq::eq(&self.0.unraw(), &other.0.unraw()) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl PartialEq<str> for IdentUnraw { |
| 52 | fn eq(&self, other: &str) -> bool { |
| 53 | self.0 == other |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | impl Ord for IdentUnraw { |
| 58 | fn cmp(&self, other: &Self) -> Ordering { |
| 59 | Ord::cmp(&self.0.unraw(), &other.0.unraw()) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl PartialOrd for IdentUnraw { |
| 64 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 65 | Some(Self::cmp(self, other)) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl Parse for IdentUnraw { |
| 70 | fn parse(input: ParseStream) -> Result<Self> { |
| 71 | input.call(Ident::parse_any).map(op:IdentUnraw::new) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl ToTokens for IdentUnraw { |
| 76 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 77 | self.0.unraw().to_tokens(tokens); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[derive (Clone)] |
| 82 | pub enum MemberUnraw { |
| 83 | Named(IdentUnraw), |
| 84 | Unnamed(Index), |
| 85 | } |
| 86 | |
| 87 | impl MemberUnraw { |
| 88 | pub fn span(&self) -> Span { |
| 89 | match self { |
| 90 | MemberUnraw::Named(ident: &IdentUnraw) => ident.0.span(), |
| 91 | MemberUnraw::Unnamed(index: &Index) => index.span, |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl Display for MemberUnraw { |
| 97 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 98 | match self { |
| 99 | MemberUnraw::Named(this: &IdentUnraw) => Display::fmt(self:this, f:formatter), |
| 100 | MemberUnraw::Unnamed(this: &Index) => Display::fmt(&this.index, f:formatter), |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl Eq for MemberUnraw {} |
| 106 | |
| 107 | impl PartialEq for MemberUnraw { |
| 108 | fn eq(&self, other: &Self) -> bool { |
| 109 | match (self, other) { |
| 110 | (MemberUnraw::Named(this: &IdentUnraw), MemberUnraw::Named(other: &IdentUnraw)) => this == other, |
| 111 | (MemberUnraw::Unnamed(this: &Index), MemberUnraw::Unnamed(other: &Index)) => this == other, |
| 112 | _ => false, |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | impl PartialEq<str> for MemberUnraw { |
| 118 | fn eq(&self, other: &str) -> bool { |
| 119 | match self { |
| 120 | MemberUnraw::Named(this: &IdentUnraw) => this == other, |
| 121 | MemberUnraw::Unnamed(_) => false, |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | impl Hash for MemberUnraw { |
| 127 | fn hash<H: Hasher>(&self, hasher: &mut H) { |
| 128 | match self { |
| 129 | MemberUnraw::Named(ident: &IdentUnraw) => ident.0.unraw().hash(state:hasher), |
| 130 | MemberUnraw::Unnamed(index: &Index) => index.hash(state:hasher), |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | impl ToTokens for MemberUnraw { |
| 136 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 137 | match self { |
| 138 | MemberUnraw::Named(ident: &IdentUnraw) => ident.to_local().to_tokens(tokens), |
| 139 | MemberUnraw::Unnamed(index: &Index) => index.to_tokens(tokens), |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |