| 1 |
/// Provide `RefIdent` and `MaybeIdent` traits that give a shortcut to extract identity reference |
| 2 |
/// (`syn::Ident` struct). |
| 3 |
use proc_macro2::Ident; |
| 4 |
use syn::{FnArg, Pat, PatType, Type}; |
| 5 |
|
| 6 |
pub trait RefIdent { |
| 7 |
/// Return the reference to ident if any |
| 8 |
fn ident(&self) -> &Ident; |
| 9 |
} |
| 10 |
|
| 11 |
pub trait MaybeIdent { |
| 12 |
/// Return the reference to ident if any |
| 13 |
fn maybe_ident(&self) -> Option<&Ident>; |
| 14 |
} |
| 15 |
|
| 16 |
impl<I: RefIdent> MaybeIdent for I { |
| 17 |
fn maybe_ident(&self) -> Option<&Ident> { |
| 18 |
Some(self.ident()) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
impl RefIdent for Ident { |
| 23 |
fn ident(&self) -> &Ident { |
| 24 |
self |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
impl RefIdent for &'_ Ident { |
| 29 |
fn ident(&self) -> &Ident { |
| 30 |
self |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl MaybeIdent for FnArg { |
| 35 |
fn maybe_ident(&self) -> Option<&Ident> { |
| 36 |
match self { |
| 37 |
FnArg::Typed(pat) => pat.maybe_ident(), |
| 38 |
_ => None, |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
impl MaybeIdent for PatType { |
| 44 |
fn maybe_ident(&self) -> Option<&Ident> { |
| 45 |
self.pat.maybe_ident() |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
impl MaybeIdent for Pat { |
| 50 |
fn maybe_ident(&self) -> Option<&Ident> { |
| 51 |
match self { |
| 52 |
Pat::Ident(ident) => Some(&ident.ident), |
| 53 |
_ => None, |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl MaybeIdent for Type { |
| 59 |
fn maybe_ident(&self) -> Option<&Ident> { |
| 60 |
match self { |
| 61 |
Type::Path(tp) if tp.qself.is_none() => tp.path.get_ident(), |
| 62 |
_ => None, |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
pub trait MaybeType { |
| 68 |
/// Return the reference to type if any |
| 69 |
fn maybe_type(&self) -> Option<&Type>; |
| 70 |
} |
| 71 |
|
| 72 |
impl MaybeType for FnArg { |
| 73 |
fn maybe_type(&self) -> Option<&Type> { |
| 74 |
match self { |
| 75 |
FnArg::Typed(PatType { ty, .. }) => Some(ty.as_ref()), |
| 76 |
_ => None, |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl MaybeIdent for syn::GenericParam { |
| 82 |
fn maybe_ident(&self) -> Option<&Ident> { |
| 83 |
match self { |
| 84 |
syn::GenericParam::Type(syn::TypeParam { ident, .. }) |
| 85 |
| syn::GenericParam::Const(syn::ConstParam { ident, .. }) => Some(ident), |
| 86 |
syn::GenericParam::Lifetime(syn::LifetimeParam { lifetime, .. }) => { |
| 87 |
Some(&lifetime.ident) |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl MaybeIdent for crate::parse::Attribute { |
| 94 |
fn maybe_ident(&self) -> Option<&Ident> { |
| 95 |
use crate::parse::Attribute::*; |
| 96 |
match self { |
| 97 |
Attr(ident) | Tagged(ident, _) | Type(ident, _) => Some(ident), |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
pub trait RefPat { |
| 103 |
/// Return the reference to ident if any |
| 104 |
fn pat(&self) -> &Pat; |
| 105 |
} |
| 106 |
|
| 107 |
pub trait MaybePatIdent { |
| 108 |
fn maybe_patident(&self) -> Option<&syn::PatIdent>; |
| 109 |
} |
| 110 |
|
| 111 |
impl MaybePatIdent for FnArg { |
| 112 |
fn maybe_patident(&self) -> Option<&syn::PatIdent> { |
| 113 |
match self { |
| 114 |
FnArg::Typed(PatType { pat, .. }) => match pat.as_ref() { |
| 115 |
Pat::Ident(ident) => Some(ident), |
| 116 |
_ => None, |
| 117 |
}, |
| 118 |
_ => None, |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl MaybePatIdent for Pat { |
| 124 |
fn maybe_patident(&self) -> Option<&syn::PatIdent> { |
| 125 |
match self { |
| 126 |
Pat::Ident(ident) => Some(ident), |
| 127 |
_ => None, |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
pub trait MaybePatType { |
| 133 |
fn maybe_pat_type(&self) -> Option<&syn::PatType>; |
| 134 |
} |
| 135 |
|
| 136 |
impl MaybePatType for FnArg { |
| 137 |
fn maybe_pat_type(&self) -> Option<&syn::PatType> { |
| 138 |
match self { |
| 139 |
FnArg::Typed(pt) => Some(pt), |
| 140 |
_ => None, |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
pub trait MaybePatTypeMut { |
| 146 |
fn maybe_pat_type_mut(&mut self) -> Option<&mut syn::PatType>; |
| 147 |
} |
| 148 |
|
| 149 |
impl MaybePatTypeMut for FnArg { |
| 150 |
fn maybe_pat_type_mut(&mut self) -> Option<&mut syn::PatType> { |
| 151 |
match self { |
| 152 |
FnArg::Typed(pt) => Some(pt), |
| 153 |
_ => None, |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
pub trait MaybePat { |
| 159 |
fn maybe_pat(&self) -> Option<&syn::Pat>; |
| 160 |
} |
| 161 |
|
| 162 |
impl MaybePat for FnArg { |
| 163 |
fn maybe_pat(&self) -> Option<&syn::Pat> { |
| 164 |
match self { |
| 165 |
FnArg::Typed(PatType { pat, .. }) => Some(pat.as_ref()), |
| 166 |
_ => None, |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
pub trait RemoveMutability { |
| 172 |
fn remove_mutability(&mut self); |
| 173 |
} |
| 174 |
|
| 175 |
impl RemoveMutability for FnArg { |
| 176 |
fn remove_mutability(&mut self) { |
| 177 |
if let FnArg::Typed(PatType { pat, .. }) = self { |
| 178 |
if let Pat::Ident(ident) = pat.as_mut() { |
| 179 |
ident.mutability = None |
| 180 |
} |
| 181 |
}; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
pub trait IntoPat { |
| 186 |
fn into_pat(self) -> Pat; |
| 187 |
} |
| 188 |
|
| 189 |
impl IntoPat for Ident { |
| 190 |
fn into_pat(self) -> Pat { |
| 191 |
Pat::Ident(syn::PatIdent { |
| 192 |
attrs: vec![], |
| 193 |
by_ref: None, |
| 194 |
mutability: None, |
| 195 |
ident: self, |
| 196 |
subpat: None, |
| 197 |
}) |
| 198 |
} |
| 199 |
} |