attr.rs 16 KB

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