equi_ty/extractor/visitors/
common.rs

1use std::collections::BTreeSet;
2
3use rustc_hir::VariantData;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
7pub struct GenericInfo {
8    pub name: String,
9    pub bounds: BTreeSet<String>,
10}
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
13pub enum VariantKind {
14    Struct,
15    Tuple,
16    Unit,
17}
18
19/// applies to enum variant
20#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
21pub struct VariantInfo {
22    pub name: String,
23    // For e.g. all the variants of an enum will have the same type (the enum itself) but will have different *kinds*
24    // (struct, or maybe unit)
25    pub kind: VariantKind,
26}
27
28#[derive(Debug, Copy, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
29pub enum TypKind {
30    Generic,
31    Ty,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
35pub struct FieldInfo {
36    pub name: Option<String>,
37    pub type_def_path: String,
38    pub type_kind: TypKind,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
42pub struct TraitInfo {
43    pub name: String,
44    pub def_path: String,
45    pub import_path: String,
46}
47
48#[derive(Debug, Copy, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
49pub enum StructKind {
50    Named, // struct Foo { i: i32 }
51    Tuple, // struct Foo ( i32 )
52    Unit,  // struct Foo
53}
54
55pub(super) fn get_struct_kind(item: &rustc_hir::Item<'_>) -> Option<StructKind> {
56    match item.kind {
57        rustc_hir::ItemKind::Struct(VariantData::Struct { .. }, _) => Some(StructKind::Named),
58        rustc_hir::ItemKind::Struct(VariantData::Tuple(_, _, _), _) => Some(StructKind::Tuple),
59        rustc_hir::ItemKind::Struct(VariantData::Unit(_, _), _) => Some(StructKind::Unit),
60        _ => None,
61    }
62}
63
64/// information about a `Struct`
65/// - name
66/// - crate
67/// - def_path
68/// - import_path
69/// - methods (names)
70/// - associated_methods
71/// - generics (args with bounds)
72/// - fields (name and type)
73/// - traits
74#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
75pub struct StructInfo {
76    pub name: String,
77    pub kind: StructKind,
78    pub crate_: String,
79    pub def_path: String,
80    pub import_path: String,
81    pub fields: Vec<FieldInfo>,
82    pub methods: BTreeSet<String>,
83    pub associated_methods: BTreeSet<String>,
84    pub generics: Vec<GenericInfo>,
85    pub traits: BTreeSet<TraitInfo>,
86}
87
88/// information about a `Enum`
89/// - name
90/// - crate
91/// - def_path
92/// - import_path
93/// - methods (names)
94/// - associated_methods
95/// - generics (args with bounds)
96/// - fields (name and type)
97/// - traits
98#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
99pub struct EnumInfo {
100    pub name: String,
101    pub crate_: String,
102    pub def_path: String,
103    pub import_path: String,
104    pub variants: Vec<VariantInfo>,
105    pub methods: BTreeSet<String>,
106    pub associated_methods: BTreeSet<String>,
107    pub generics: Vec<GenericInfo>,
108    pub traits: BTreeSet<TraitInfo>,
109}
110
111pub type DefPathStr = String;