diff options
Diffstat (limited to 'crates/ra_hir/src/adt.rs')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs new file mode 100644 index 000000000..a2d228593 --- /dev/null +++ b/crates/ra_hir/src/adt.rs | |||
@@ -0,0 +1,114 @@ | |||
1 | use ra_syntax::{SmolStr, ast::{self, NameOwner}}; | ||
2 | |||
3 | use crate::{ | ||
4 | DefId, Cancelable, | ||
5 | db::{HirDatabase}, | ||
6 | ty::{Ty}, | ||
7 | }; | ||
8 | |||
9 | pub struct Struct { | ||
10 | def_id: DefId, | ||
11 | } | ||
12 | |||
13 | impl Struct { | ||
14 | pub(crate) fn new(def_id: DefId) -> Self { | ||
15 | Struct { def_id } | ||
16 | } | ||
17 | |||
18 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<SmolStr> { | ||
19 | Ok(db.struct_data(self.def_id)?.name.clone()) | ||
20 | } | ||
21 | } | ||
22 | |||
23 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
24 | pub struct StructData { | ||
25 | name: SmolStr, | ||
26 | variant_data: VariantData, | ||
27 | } | ||
28 | |||
29 | impl StructData { | ||
30 | pub(crate) fn new(struct_def: ast::StructDef) -> StructData { | ||
31 | let name = struct_def | ||
32 | .name() | ||
33 | .map(|n| n.text()) | ||
34 | .unwrap_or(SmolStr::new("[error]")); | ||
35 | let variant_data = VariantData::Unit; // TODO implement this | ||
36 | StructData { name, variant_data } | ||
37 | } | ||
38 | } | ||
39 | |||
40 | pub struct Enum { | ||
41 | def_id: DefId, | ||
42 | } | ||
43 | |||
44 | impl Enum { | ||
45 | pub(crate) fn new(def_id: DefId) -> Self { | ||
46 | Enum { def_id } | ||
47 | } | ||
48 | |||
49 | pub fn name(&self, db: &impl HirDatabase) -> Cancelable<SmolStr> { | ||
50 | Ok(db.enum_data(self.def_id)?.name.clone()) | ||
51 | } | ||
52 | } | ||
53 | |||
54 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
55 | pub struct EnumData { | ||
56 | name: SmolStr, | ||
57 | variants: Vec<(SmolStr, VariantData)>, | ||
58 | } | ||
59 | |||
60 | impl EnumData { | ||
61 | pub(crate) fn new(enum_def: ast::EnumDef) -> Self { | ||
62 | let name = enum_def | ||
63 | .name() | ||
64 | .map(|n| n.text()) | ||
65 | .unwrap_or(SmolStr::new("[error]")); | ||
66 | let variants = Vec::new(); // TODO implement this | ||
67 | EnumData { name, variants } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /// A single field of an enum variant or struct | ||
72 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
73 | pub struct StructField { | ||
74 | name: SmolStr, | ||
75 | ty: Ty, | ||
76 | } | ||
77 | |||
78 | /// Fields of an enum variant or struct | ||
79 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
80 | pub enum VariantData { | ||
81 | Struct(Vec<StructField>), | ||
82 | Tuple(Vec<StructField>), | ||
83 | Unit, | ||
84 | } | ||
85 | |||
86 | impl VariantData { | ||
87 | pub fn fields(&self) -> &[StructField] { | ||
88 | match *self { | ||
89 | VariantData::Struct(ref fields) | VariantData::Tuple(ref fields) => fields, | ||
90 | _ => &[], | ||
91 | } | ||
92 | } | ||
93 | pub fn is_struct(&self) -> bool { | ||
94 | if let VariantData::Struct(..) = *self { | ||
95 | true | ||
96 | } else { | ||
97 | false | ||
98 | } | ||
99 | } | ||
100 | pub fn is_tuple(&self) -> bool { | ||
101 | if let VariantData::Tuple(..) = *self { | ||
102 | true | ||
103 | } else { | ||
104 | false | ||
105 | } | ||
106 | } | ||
107 | pub fn is_unit(&self) -> bool { | ||
108 | if let VariantData::Unit = *self { | ||
109 | true | ||
110 | } else { | ||
111 | false | ||
112 | } | ||
113 | } | ||
114 | } | ||