pb_attrs.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #![allow(clippy::all)]
  2. use crate::{symbol::*, ASTResult};
  3. use proc_macro2::{Group, Span, TokenStream, TokenTree};
  4. use quote::ToTokens;
  5. use syn::{
  6. self,
  7. parse::{self, Parse},
  8. Meta::{List, NameValue, Path},
  9. NestedMeta::{Lit, Meta},
  10. };
  11. #[allow(dead_code)]
  12. pub struct PBAttrsContainer {
  13. name: String,
  14. pb_struct_type: Option<syn::Type>,
  15. pb_enum_type: Option<syn::Type>,
  16. }
  17. impl PBAttrsContainer {
  18. /// Extract out the `#[pb(...)]` attributes from an item.
  19. pub fn from_ast(ast_result: &ASTResult, item: &syn::DeriveInput) -> Self {
  20. let mut pb_struct_type = ASTAttr::none(ast_result, PB_STRUCT);
  21. let mut pb_enum_type = ASTAttr::none(ast_result, PB_ENUM);
  22. for meta_item in item
  23. .attrs
  24. .iter()
  25. .flat_map(|attr| get_pb_meta_items(ast_result, attr))
  26. .flatten()
  27. {
  28. match &meta_item {
  29. // Parse `#[pb(struct = "Type")]
  30. Meta(NameValue(m)) if m.path == PB_STRUCT => {
  31. if let Ok(into_ty) = parse_lit_into_ty(ast_result, PB_STRUCT, &m.lit) {
  32. pb_struct_type.set_opt(&m.path, Some(into_ty));
  33. }
  34. }
  35. // Parse `#[pb(enum = "Type")]
  36. Meta(NameValue(m)) if m.path == PB_ENUM => {
  37. if let Ok(into_ty) = parse_lit_into_ty(ast_result, PB_ENUM, &m.lit) {
  38. pb_enum_type.set_opt(&m.path, Some(into_ty));
  39. }
  40. }
  41. Meta(meta_item) => {
  42. let path = meta_item.path().into_token_stream().to_string().replace(' ', "");
  43. ast_result.error_spanned_by(meta_item.path(), format!("unknown container attribute `{}`", path));
  44. }
  45. Lit(lit) => {
  46. ast_result.error_spanned_by(lit, "unexpected literal in container attribute");
  47. }
  48. }
  49. }
  50. match &item.data {
  51. syn::Data::Struct(_) => {
  52. pb_struct_type.set_if_none(default_pb_type(&ast_result, &item.ident));
  53. }
  54. syn::Data::Enum(_) => {
  55. pb_enum_type.set_if_none(default_pb_type(&ast_result, &item.ident));
  56. }
  57. _ => {}
  58. }
  59. PBAttrsContainer {
  60. name: item.ident.to_string(),
  61. pb_struct_type: pb_struct_type.get(),
  62. pb_enum_type: pb_enum_type.get(),
  63. }
  64. }
  65. pub fn pb_struct_type(&self) -> Option<&syn::Type> {
  66. self.pb_struct_type.as_ref()
  67. }
  68. pub fn pb_enum_type(&self) -> Option<&syn::Type> {
  69. self.pb_enum_type.as_ref()
  70. }
  71. }
  72. pub struct ASTAttr<'c, T> {
  73. ast_result: &'c ASTResult,
  74. name: Symbol,
  75. tokens: TokenStream,
  76. value: Option<T>,
  77. }
  78. impl<'c, T> ASTAttr<'c, T> {
  79. pub(crate) fn none(ast_result: &'c ASTResult, name: Symbol) -> Self {
  80. ASTAttr {
  81. ast_result,
  82. name,
  83. tokens: TokenStream::new(),
  84. value: None,
  85. }
  86. }
  87. pub(crate) fn set<A: ToTokens>(&mut self, obj: A, value: T) {
  88. let tokens = obj.into_token_stream();
  89. if self.value.is_some() {
  90. self.ast_result
  91. .error_spanned_by(tokens, format!("duplicate attribute `{}`", self.name));
  92. } else {
  93. self.tokens = tokens;
  94. self.value = Some(value);
  95. }
  96. }
  97. fn set_opt<A: ToTokens>(&mut self, obj: A, value: Option<T>) {
  98. if let Some(value) = value {
  99. self.set(obj, value);
  100. }
  101. }
  102. pub(crate) fn set_if_none(&mut self, value: T) {
  103. if self.value.is_none() {
  104. self.value = Some(value);
  105. }
  106. }
  107. pub(crate) fn get(self) -> Option<T> {
  108. self.value
  109. }
  110. #[allow(dead_code)]
  111. fn get_with_tokens(self) -> Option<(TokenStream, T)> {
  112. match self.value {
  113. Some(v) => Some((self.tokens, v)),
  114. None => None,
  115. }
  116. }
  117. }
  118. pub struct PBStructAttrs {
  119. #[allow(dead_code)]
  120. name: String,
  121. pb_index: Option<syn::LitInt>,
  122. pb_one_of: bool,
  123. skip_pb_serializing: bool,
  124. skip_pb_deserializing: bool,
  125. serialize_pb_with: Option<syn::ExprPath>,
  126. deserialize_pb_with: Option<syn::ExprPath>,
  127. }
  128. pub fn is_recognizable_field(field: &syn::Field) -> bool {
  129. field.attrs.iter().any(|attr| is_recognizable_attribute(attr))
  130. }
  131. impl PBStructAttrs {
  132. /// Extract out the `#[pb(...)]` attributes from a struct field.
  133. pub fn from_ast(ast_result: &ASTResult, index: usize, field: &syn::Field) -> Self {
  134. let mut pb_index = ASTAttr::none(ast_result, PB_INDEX);
  135. let mut pb_one_of = BoolAttr::none(ast_result, PB_ONE_OF);
  136. let mut serialize_pb_with = ASTAttr::none(ast_result, SERIALIZE_PB_WITH);
  137. let mut skip_pb_serializing = BoolAttr::none(ast_result, SKIP_PB_SERIALIZING);
  138. let mut deserialize_pb_with = ASTAttr::none(ast_result, DESERIALIZE_PB_WITH);
  139. let mut skip_pb_deserializing = BoolAttr::none(ast_result, SKIP_PB_DESERIALIZING);
  140. let ident = match &field.ident {
  141. Some(ident) => ident.to_string(),
  142. None => index.to_string(),
  143. };
  144. for meta_item in field
  145. .attrs
  146. .iter()
  147. .flat_map(|attr| get_pb_meta_items(ast_result, attr))
  148. .flatten()
  149. {
  150. match &meta_item {
  151. // Parse `#[pb(skip)]`
  152. Meta(Path(word)) if word == SKIP => {
  153. skip_pb_serializing.set_true(word);
  154. skip_pb_deserializing.set_true(word);
  155. }
  156. // Parse '#[pb(index = x)]'
  157. Meta(NameValue(m)) if m.path == PB_INDEX => {
  158. if let syn::Lit::Int(lit) = &m.lit {
  159. pb_index.set(&m.path, lit.clone());
  160. }
  161. }
  162. // Parse `#[pb(one_of)]`
  163. Meta(Path(path)) if path == PB_ONE_OF => {
  164. pb_one_of.set_true(path);
  165. }
  166. // Parse `#[pb(serialize_pb_with = "...")]`
  167. Meta(NameValue(m)) if m.path == SERIALIZE_PB_WITH => {
  168. if let Ok(path) = parse_lit_into_expr_path(ast_result, SERIALIZE_PB_WITH, &m.lit) {
  169. serialize_pb_with.set(&m.path, path);
  170. }
  171. }
  172. // Parse `#[pb(deserialize_pb_with = "...")]`
  173. Meta(NameValue(m)) if m.path == DESERIALIZE_PB_WITH => {
  174. if let Ok(path) = parse_lit_into_expr_path(ast_result, DESERIALIZE_PB_WITH, &m.lit) {
  175. deserialize_pb_with.set(&m.path, path);
  176. }
  177. }
  178. Meta(meta_item) => {
  179. let path = meta_item.path().into_token_stream().to_string().replace(' ', "");
  180. ast_result.error_spanned_by(meta_item.path(), format!("unknown pb field attribute `{}`", path));
  181. }
  182. Lit(lit) => {
  183. ast_result.error_spanned_by(lit, "unexpected literal in field attribute");
  184. }
  185. }
  186. }
  187. PBStructAttrs {
  188. name: ident,
  189. pb_index: pb_index.get(),
  190. pb_one_of: pb_one_of.get(),
  191. skip_pb_serializing: skip_pb_serializing.get(),
  192. skip_pb_deserializing: skip_pb_deserializing.get(),
  193. serialize_pb_with: serialize_pb_with.get(),
  194. deserialize_pb_with: deserialize_pb_with.get(),
  195. }
  196. }
  197. #[allow(dead_code)]
  198. pub fn pb_index(&self) -> Option<String> {
  199. self.pb_index.as_ref().map(|lit| lit.base10_digits().to_string())
  200. }
  201. pub fn is_one_of(&self) -> bool {
  202. self.pb_one_of
  203. }
  204. pub fn serialize_pb_with(&self) -> Option<&syn::ExprPath> {
  205. self.serialize_pb_with.as_ref()
  206. }
  207. pub fn deserialize_pb_with(&self) -> Option<&syn::ExprPath> {
  208. self.deserialize_pb_with.as_ref()
  209. }
  210. pub fn skip_pb_serializing(&self) -> bool {
  211. self.skip_pb_serializing
  212. }
  213. pub fn skip_pb_deserializing(&self) -> bool {
  214. self.skip_pb_deserializing
  215. }
  216. }
  217. pub enum Default {
  218. /// Field must always be specified because it does not have a default.
  219. None,
  220. /// The default is given by `std::default::Default::default()`.
  221. Default,
  222. /// The default is given by this function.
  223. Path(syn::ExprPath),
  224. }
  225. pub fn is_recognizable_attribute(attr: &syn::Attribute) -> bool {
  226. attr.path == PB_ATTRS || attr.path == EVENT || attr.path == NODE_ATTRS || attr.path == NODES_ATTRS
  227. }
  228. pub fn get_pb_meta_items(cx: &ASTResult, attr: &syn::Attribute) -> Result<Vec<syn::NestedMeta>, ()> {
  229. // Only handle the attribute that we have defined
  230. if attr.path != PB_ATTRS {
  231. return Ok(vec![]);
  232. }
  233. // http://strymon.systems.ethz.ch/typename/syn/enum.Meta.html
  234. match attr.parse_meta() {
  235. Ok(List(meta)) => Ok(meta.nested.into_iter().collect()),
  236. Ok(other) => {
  237. cx.error_spanned_by(other, "expected #[pb(...)]");
  238. Err(())
  239. }
  240. Err(err) => {
  241. cx.error_spanned_by(attr, "attribute must be str, e.g. #[pb(xx = \"xxx\")]");
  242. cx.syn_error(err);
  243. Err(())
  244. }
  245. }
  246. }
  247. pub fn get_node_meta_items(cx: &ASTResult, attr: &syn::Attribute) -> Result<Vec<syn::NestedMeta>, ()> {
  248. // Only handle the attribute that we have defined
  249. if attr.path != NODE_ATTRS && attr.path != NODES_ATTRS {
  250. return Ok(vec![]);
  251. }
  252. // http://strymon.systems.ethz.ch/typename/syn/enum.Meta.html
  253. match attr.parse_meta() {
  254. Ok(List(meta)) => Ok(meta.nested.into_iter().collect()),
  255. Ok(_) => Ok(vec![]),
  256. Err(err) => {
  257. cx.error_spanned_by(attr, "attribute must be str, e.g. #[node(xx = \"xxx\")]");
  258. cx.syn_error(err);
  259. Err(())
  260. }
  261. }
  262. }
  263. pub fn get_event_meta_items(cx: &ASTResult, attr: &syn::Attribute) -> Result<Vec<syn::NestedMeta>, ()> {
  264. // Only handle the attribute that we have defined
  265. if attr.path != EVENT {
  266. return Ok(vec![]);
  267. }
  268. // http://strymon.systems.ethz.ch/typename/syn/enum.Meta.html
  269. match attr.parse_meta() {
  270. Ok(List(meta)) => Ok(meta.nested.into_iter().collect()),
  271. Ok(other) => {
  272. cx.error_spanned_by(other, "expected #[event(...)]");
  273. Err(())
  274. }
  275. Err(err) => {
  276. cx.error_spanned_by(attr, "attribute must be str, e.g. #[event(xx = \"xxx\")]");
  277. cx.syn_error(err);
  278. Err(())
  279. }
  280. }
  281. }
  282. pub fn parse_lit_into_expr_path(
  283. ast_result: &ASTResult,
  284. attr_name: Symbol,
  285. lit: &syn::Lit,
  286. ) -> Result<syn::ExprPath, ()> {
  287. let string = get_lit_str(ast_result, attr_name, lit)?;
  288. parse_lit_str(string)
  289. .map_err(|_| ast_result.error_spanned_by(lit, format!("failed to parse path: {:?}", string.value())))
  290. }
  291. fn get_lit_str<'a>(ast_result: &ASTResult, attr_name: Symbol, lit: &'a syn::Lit) -> Result<&'a syn::LitStr, ()> {
  292. if let syn::Lit::Str(lit) = lit {
  293. Ok(lit)
  294. } else {
  295. ast_result.error_spanned_by(
  296. lit,
  297. format!(
  298. "expected pb {} attribute to be a string: `{} = \"...\"`",
  299. attr_name, attr_name
  300. ),
  301. );
  302. Err(())
  303. }
  304. }
  305. fn parse_lit_into_ty(ast_result: &ASTResult, attr_name: Symbol, lit: &syn::Lit) -> Result<syn::Type, ()> {
  306. let string = get_lit_str(ast_result, attr_name, lit)?;
  307. parse_lit_str(string).map_err(|_| {
  308. ast_result.error_spanned_by(
  309. lit,
  310. format!("failed to parse type: {} = {:?}", attr_name, string.value()),
  311. )
  312. })
  313. }
  314. pub fn parse_lit_str<T>(s: &syn::LitStr) -> parse::Result<T>
  315. where
  316. T: Parse,
  317. {
  318. let tokens = spanned_tokens(s)?;
  319. syn::parse2(tokens)
  320. }
  321. fn spanned_tokens(s: &syn::LitStr) -> parse::Result<TokenStream> {
  322. let stream = syn::parse_str(&s.value())?;
  323. Ok(respan_token_stream(stream, s.span()))
  324. }
  325. fn respan_token_stream(stream: TokenStream, span: Span) -> TokenStream {
  326. stream.into_iter().map(|token| respan_token_tree(token, span)).collect()
  327. }
  328. fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree {
  329. if let TokenTree::Group(g) = &mut token {
  330. *g = Group::new(g.delimiter(), respan_token_stream(g.stream(), span));
  331. }
  332. token.set_span(span);
  333. token
  334. }
  335. fn default_pb_type(ast_result: &ASTResult, ident: &syn::Ident) -> syn::Type {
  336. let take_ident = ident.to_string();
  337. let lit_str = syn::LitStr::new(&take_ident, ident.span());
  338. if let Ok(tokens) = spanned_tokens(&lit_str) {
  339. if let Ok(pb_struct_ty) = syn::parse2(tokens) {
  340. return pb_struct_ty;
  341. }
  342. }
  343. ast_result.error_spanned_by(ident, format!("❌ Can't find {} protobuf struct", take_ident));
  344. panic!()
  345. }
  346. #[allow(dead_code)]
  347. pub fn is_option(ty: &syn::Type) -> bool {
  348. let path = match ungroup(ty) {
  349. syn::Type::Path(ty) => &ty.path,
  350. _ => {
  351. return false;
  352. }
  353. };
  354. let seg = match path.segments.last() {
  355. Some(seg) => seg,
  356. None => {
  357. return false;
  358. }
  359. };
  360. let args = match &seg.arguments {
  361. syn::PathArguments::AngleBracketed(bracketed) => &bracketed.args,
  362. _ => {
  363. return false;
  364. }
  365. };
  366. seg.ident == "Option" && args.len() == 1
  367. }
  368. #[allow(dead_code)]
  369. pub fn ungroup(mut ty: &syn::Type) -> &syn::Type {
  370. while let syn::Type::Group(group) = ty {
  371. ty = &group.elem;
  372. }
  373. ty
  374. }
  375. struct BoolAttr<'c>(ASTAttr<'c, ()>);
  376. impl<'c> BoolAttr<'c> {
  377. fn none(ast_result: &'c ASTResult, name: Symbol) -> Self {
  378. BoolAttr(ASTAttr::none(ast_result, name))
  379. }
  380. fn set_true<A: ToTokens>(&mut self, obj: A) {
  381. self.0.set(obj, ());
  382. }
  383. fn get(&self) -> bool {
  384. self.0.value.is_some()
  385. }
  386. }