ty_ext.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. use crate::Ctxt;
  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>(ctxt: &Ctxt, 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(ctxt, ty, seg, bracketed),
  53. "Vec" => generate_vec_ty_info(ctxt, seg, bracketed),
  54. "Option" => generate_option_ty_info(ctxt, 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. ctxt.error_spanned_by(ty, "Unsupported inner type, get inner type fail".to_string());
  69. Ok(None)
  70. }
  71. fn parse_bracketed(bracketed: &AngleBracketedGenericArguments) -> Vec<&syn::Type> {
  72. bracketed
  73. .args
  74. .iter()
  75. .flat_map(|arg| {
  76. if let syn::GenericArgument::Type(ref ty_in_bracket) = arg {
  77. Some(ty_in_bracket)
  78. } else {
  79. None
  80. }
  81. })
  82. .collect::<Vec<&syn::Type>>()
  83. }
  84. pub fn generate_hashmap_ty_info<'a>(
  85. ctxt: &Ctxt,
  86. ty: &'a syn::Type,
  87. path_segment: &'a PathSegment,
  88. bracketed: &'a AngleBracketedGenericArguments,
  89. ) -> Result<Option<TyInfo<'a>>, String> {
  90. // The args of map must greater than 2
  91. if bracketed.args.len() != 2 {
  92. return Ok(None);
  93. }
  94. let types = parse_bracketed(bracketed);
  95. let key = parse_ty(ctxt, types[0])?.unwrap().ident.to_string();
  96. let value = parse_ty(ctxt, types[1])?.unwrap().ident.to_string();
  97. let bracket_ty_info = Box::new(parse_ty(ctxt, types[1])?);
  98. Ok(Some(TyInfo {
  99. ident: &path_segment.ident,
  100. ty,
  101. primitive_ty: PrimitiveTy::Map(MapInfo::new(key, value)),
  102. bracket_ty_info,
  103. }))
  104. }
  105. fn generate_option_ty_info<'a>(
  106. ctxt: &Ctxt,
  107. ty: &'a syn::Type,
  108. path_segment: &'a PathSegment,
  109. bracketed: &'a AngleBracketedGenericArguments,
  110. ) -> Result<Option<TyInfo<'a>>, String> {
  111. assert_eq!(path_segment.ident.to_string(), "Option".to_string());
  112. let types = parse_bracketed(bracketed);
  113. let bracket_ty_info = Box::new(parse_ty(ctxt, types[0])?);
  114. Ok(Some(TyInfo {
  115. ident: &path_segment.ident,
  116. ty,
  117. primitive_ty: PrimitiveTy::Opt,
  118. bracket_ty_info,
  119. }))
  120. }
  121. fn generate_vec_ty_info<'a>(
  122. ctxt: &Ctxt,
  123. path_segment: &'a PathSegment,
  124. bracketed: &'a AngleBracketedGenericArguments,
  125. ) -> Result<Option<TyInfo<'a>>, String> {
  126. if bracketed.args.len() != 1 {
  127. return Ok(None);
  128. }
  129. if let syn::GenericArgument::Type(ref bracketed_type) = bracketed.args.first().unwrap() {
  130. let bracketed_ty_info = Box::new(parse_ty(ctxt, bracketed_type)?);
  131. return Ok(Some(TyInfo {
  132. ident: &path_segment.ident,
  133. ty: bracketed_type,
  134. primitive_ty: PrimitiveTy::Vec,
  135. bracket_ty_info: bracketed_ty_info,
  136. }));
  137. }
  138. Ok(None)
  139. }