ty_ext.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. use crate::ASTResult;
  2. use syn::{self, AngleBracketedGenericArguments, PathSegment};
  3. #[derive(Eq, PartialEq, Debug)]
  4. pub enum PrimitiveTy {
  5. Map(MapInfo),
  6. Vec,
  7. Opt,
  8. Other,
  9. }
  10. #[derive(Debug)]
  11. pub struct TyInfo<'a> {
  12. pub ident: &'a syn::Ident,
  13. pub ty: &'a syn::Type,
  14. pub primitive_ty: PrimitiveTy,
  15. pub bracket_ty_info: Box<Option<TyInfo<'a>>>,
  16. }
  17. #[derive(Debug, Eq, PartialEq)]
  18. pub struct MapInfo {
  19. pub key: String,
  20. pub value: String,
  21. }
  22. impl MapInfo {
  23. fn new(key: String, value: String) -> Self {
  24. MapInfo { key, value }
  25. }
  26. }
  27. impl<'a> TyInfo<'a> {
  28. #[allow(dead_code)]
  29. pub fn bracketed_ident(&'a self) -> &'a syn::Ident {
  30. match self.bracket_ty_info.as_ref() {
  31. Some(b_ty) => b_ty.ident,
  32. None => {
  33. panic!()
  34. }
  35. }
  36. }
  37. }
  38. pub fn parse_ty<'a>(ast_result: &ASTResult, ty: &'a syn::Type) -> Result<Option<TyInfo<'a>>, String> {
  39. // Type -> TypePath -> Path -> PathSegment -> PathArguments ->
  40. // AngleBracketedGenericArguments -> GenericArgument -> Type.
  41. if let syn::Type::Path(ref p) = ty {
  42. if p.path.segments.len() != 1 {
  43. return Ok(None);
  44. }
  45. let seg = match p.path.segments.last() {
  46. Some(seg) => seg,
  47. None => return Ok(None),
  48. };
  49. let _is_option = seg.ident == "Option";
  50. return if let syn::PathArguments::AngleBracketed(ref bracketed) = seg.arguments {
  51. match seg.ident.to_string().as_ref() {
  52. "HashMap" => generate_hashmap_ty_info(ast_result, ty, seg, bracketed),
  53. "Vec" => generate_vec_ty_info(ast_result, seg, bracketed),
  54. "Option" => generate_option_ty_info(ast_result, ty, seg, bracketed),
  55. _ => {
  56. return Err(format!("Unsupported ty {}", seg.ident));
  57. }
  58. }
  59. } else {
  60. return Ok(Some(TyInfo {
  61. ident: &seg.ident,
  62. ty,
  63. primitive_ty: PrimitiveTy::Other,
  64. bracket_ty_info: Box::new(None),
  65. }));
  66. };
  67. }
  68. Err("Unsupported inner type, get inner type fail".to_string())
  69. }
  70. fn parse_bracketed(bracketed: &AngleBracketedGenericArguments) -> Vec<&syn::Type> {
  71. bracketed
  72. .args
  73. .iter()
  74. .flat_map(|arg| {
  75. if let syn::GenericArgument::Type(ref ty_in_bracket) = arg {
  76. Some(ty_in_bracket)
  77. } else {
  78. None
  79. }
  80. })
  81. .collect::<Vec<&syn::Type>>()
  82. }
  83. pub fn generate_hashmap_ty_info<'a>(
  84. ast_result: &ASTResult,
  85. ty: &'a syn::Type,
  86. path_segment: &'a PathSegment,
  87. bracketed: &'a AngleBracketedGenericArguments,
  88. ) -> Result<Option<TyInfo<'a>>, String> {
  89. // The args of map must greater than 2
  90. if bracketed.args.len() != 2 {
  91. return Ok(None);
  92. }
  93. let types = parse_bracketed(bracketed);
  94. let key = parse_ty(ast_result, types[0])?.unwrap().ident.to_string();
  95. let value = parse_ty(ast_result, types[1])?.unwrap().ident.to_string();
  96. let bracket_ty_info = Box::new(parse_ty(ast_result, types[1])?);
  97. Ok(Some(TyInfo {
  98. ident: &path_segment.ident,
  99. ty,
  100. primitive_ty: PrimitiveTy::Map(MapInfo::new(key, value)),
  101. bracket_ty_info,
  102. }))
  103. }
  104. fn generate_option_ty_info<'a>(
  105. ast_result: &ASTResult,
  106. ty: &'a syn::Type,
  107. path_segment: &'a PathSegment,
  108. bracketed: &'a AngleBracketedGenericArguments,
  109. ) -> Result<Option<TyInfo<'a>>, String> {
  110. assert_eq!(path_segment.ident.to_string(), "Option".to_string());
  111. let types = parse_bracketed(bracketed);
  112. let bracket_ty_info = Box::new(parse_ty(ast_result, types[0])?);
  113. Ok(Some(TyInfo {
  114. ident: &path_segment.ident,
  115. ty,
  116. primitive_ty: PrimitiveTy::Opt,
  117. bracket_ty_info,
  118. }))
  119. }
  120. fn generate_vec_ty_info<'a>(
  121. ast_result: &ASTResult,
  122. path_segment: &'a PathSegment,
  123. bracketed: &'a AngleBracketedGenericArguments,
  124. ) -> Result<Option<TyInfo<'a>>, String> {
  125. if bracketed.args.len() != 1 {
  126. return Ok(None);
  127. }
  128. if let syn::GenericArgument::Type(ref bracketed_type) = bracketed.args.first().unwrap() {
  129. let bracketed_ty_info = Box::new(parse_ty(ast_result, bracketed_type)?);
  130. return Ok(Some(TyInfo {
  131. ident: &path_segment.ident,
  132. ty: bracketed_type,
  133. primitive_ty: PrimitiveTy::Vec,
  134. bracket_ty_info: bracketed_ty_info,
  135. }));
  136. }
  137. Ok(None)
  138. }