node_attrs.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. use crate::{get_node_meta_items, parse_lit_into_expr_path, symbol::*, ASTAttr, ASTResult};
  2. use quote::ToTokens;
  3. use syn::{
  4. self, LitStr,
  5. Meta::NameValue,
  6. NestedMeta::{Lit, Meta},
  7. };
  8. pub struct NodeStructAttrs {
  9. pub rename: Option<LitStr>,
  10. pub has_child: bool,
  11. pub child_name: Option<LitStr>,
  12. pub child_index: Option<syn::LitInt>,
  13. pub get_node_value_with: Option<syn::ExprPath>,
  14. pub set_node_value_with: Option<syn::ExprPath>,
  15. pub with_children: Option<syn::ExprPath>,
  16. }
  17. impl NodeStructAttrs {
  18. /// Extract out the `#[node(...)]` attributes from a struct field.
  19. pub fn from_ast(ast_result: &ASTResult, _index: usize, field: &syn::Field) -> Self {
  20. let mut rename = ASTAttr::none(ast_result, RENAME_NODE);
  21. let mut child_name = ASTAttr::none(ast_result, CHILD_NODE_NAME);
  22. let mut child_index = ASTAttr::none(ast_result, CHILD_NODE_INDEX);
  23. let mut get_node_value_with = ASTAttr::none(ast_result, GET_NODE_VALUE_WITH);
  24. let mut set_node_value_with = ASTAttr::none(ast_result, SET_NODE_VALUE_WITH);
  25. let mut with_children = ASTAttr::none(ast_result, WITH_CHILDREN);
  26. for meta_item in field
  27. .attrs
  28. .iter()
  29. .flat_map(|attr| get_node_meta_items(ast_result, attr))
  30. .flatten()
  31. {
  32. match &meta_item {
  33. // Parse '#[node(rename = x)]'
  34. Meta(NameValue(m)) if m.path == RENAME_NODE => {
  35. if let syn::Lit::Str(lit) = &m.lit {
  36. rename.set(&m.path, lit.clone());
  37. }
  38. },
  39. // Parse '#[node(child_name = x)]'
  40. Meta(NameValue(m)) if m.path == CHILD_NODE_NAME => {
  41. if let syn::Lit::Str(lit) = &m.lit {
  42. child_name.set(&m.path, lit.clone());
  43. }
  44. },
  45. // Parse '#[node(child_index = x)]'
  46. Meta(NameValue(m)) if m.path == CHILD_NODE_INDEX => {
  47. if let syn::Lit::Int(lit) = &m.lit {
  48. child_index.set(&m.path, lit.clone());
  49. }
  50. },
  51. // Parse `#[node(get_node_value_with = "...")]`
  52. Meta(NameValue(m)) if m.path == GET_NODE_VALUE_WITH => {
  53. if let Ok(path) = parse_lit_into_expr_path(ast_result, GET_NODE_VALUE_WITH, &m.lit) {
  54. get_node_value_with.set(&m.path, path);
  55. }
  56. },
  57. // Parse `#[node(set_node_value_with= "...")]`
  58. Meta(NameValue(m)) if m.path == SET_NODE_VALUE_WITH => {
  59. if let Ok(path) = parse_lit_into_expr_path(ast_result, SET_NODE_VALUE_WITH, &m.lit) {
  60. set_node_value_with.set(&m.path, path);
  61. }
  62. },
  63. // Parse `#[node(with_children= "...")]`
  64. Meta(NameValue(m)) if m.path == WITH_CHILDREN => {
  65. if let Ok(path) = parse_lit_into_expr_path(ast_result, WITH_CHILDREN, &m.lit) {
  66. with_children.set(&m.path, path);
  67. }
  68. },
  69. Meta(meta_item) => {
  70. let path = meta_item
  71. .path()
  72. .into_token_stream()
  73. .to_string()
  74. .replace(' ', "");
  75. ast_result.error_spanned_by(
  76. meta_item.path(),
  77. format!("unknown node field attribute `{}`", path),
  78. );
  79. },
  80. Lit(lit) => {
  81. ast_result.error_spanned_by(lit, "unexpected literal in field attribute");
  82. },
  83. }
  84. }
  85. let child_name = child_name.get();
  86. NodeStructAttrs {
  87. rename: rename.get(),
  88. child_index: child_index.get(),
  89. has_child: child_name.is_some(),
  90. child_name,
  91. get_node_value_with: get_node_value_with.get(),
  92. set_node_value_with: set_node_value_with.get(),
  93. with_children: with_children.get(),
  94. }
  95. }
  96. }