aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs98
1 files changed, 97 insertions, 1 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 43cddb504..f06f1ae66 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -1,8 +1,15 @@
1use std::sync::Arc;
2
1use relative_path::RelativePathBuf; 3use relative_path::RelativePathBuf;
2use ra_db::{CrateId, Cancelable, FileId}; 4use ra_db::{CrateId, Cancelable, FileId};
3use ra_syntax::{ast, TreePtr, SyntaxNode}; 5use ra_syntax::{ast, TreePtr, SyntaxNode};
4 6
5use crate::{Name, db::HirDatabase, DefId, Path, PerNs, nameres::ModuleScope}; 7use crate::{
8 Name, DefId, Path, PerNs,
9 type_ref::TypeRef,
10 nameres::ModuleScope,
11 db::HirDatabase,
12};
6 13
7/// hir::Crate describes a single crate. It's the main inteface with which 14/// hir::Crate describes a single crate. It's the main inteface with which
8/// crate's dependencies interact. Mostly, it should be just a proxy for the 15/// crate's dependencies interact. Mostly, it should be just a proxy for the
@@ -111,3 +118,92 @@ impl Module {
111 self.problems_impl(db) 118 self.problems_impl(db)
112 } 119 }
113} 120}
121
122/// A single field of an enum variant or struct
123#[derive(Debug, Clone, PartialEq, Eq)]
124pub struct StructField {
125 pub(crate) name: Name,
126 pub(crate) type_ref: TypeRef,
127}
128
129impl StructField {
130 pub fn name(&self) -> &Name {
131 &self.name
132 }
133 pub fn type_ref(&self) -> &TypeRef {
134 &self.type_ref
135 }
136}
137
138/// Fields of an enum variant or struct
139#[derive(Debug, Clone, PartialEq, Eq)]
140pub enum VariantData {
141 Struct(Vec<StructField>),
142 Tuple(Vec<StructField>),
143 Unit,
144}
145
146impl VariantData {
147 pub fn fields(&self) -> &[StructField] {
148 match self {
149 VariantData::Struct(fields) | VariantData::Tuple(fields) => fields,
150 _ => &[],
151 }
152 }
153 pub fn is_struct(&self) -> bool {
154 match self {
155 VariantData::Struct(..) => true,
156 _ => false,
157 }
158 }
159 pub fn is_tuple(&self) -> bool {
160 match self {
161 VariantData::Tuple(..) => true,
162 _ => false,
163 }
164 }
165 pub fn is_unit(&self) -> bool {
166 match self {
167 VariantData::Unit => true,
168 _ => false,
169 }
170 }
171}
172
173#[derive(Debug, Clone, PartialEq, Eq, Hash)]
174pub struct Struct {
175 pub(crate) def_id: DefId,
176}
177
178impl Struct {
179 pub fn def_id(&self) -> DefId {
180 self.def_id
181 }
182
183 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
184 Ok(db.struct_data(self.def_id)?.name.clone())
185 }
186
187 pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
188 Ok(db.struct_data(self.def_id)?.variant_data.clone())
189 }
190}
191
192#[derive(Debug, Clone, PartialEq, Eq, Hash)]
193pub struct Enum {
194 pub(crate) def_id: DefId,
195}
196
197impl Enum {
198 pub fn def_id(&self) -> DefId {
199 self.def_id
200 }
201
202 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
203 Ok(db.enum_data(self.def_id)?.name.clone())
204 }
205
206 pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, Arc<VariantData>)>> {
207 Ok(db.enum_data(self.def_id)?.variants.clone())
208 }
209}