aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/from_id.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/from_id.rs')
-rw-r--r--crates/ra_hir/src/from_id.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
new file mode 100644
index 000000000..089dbc908
--- /dev/null
+++ b/crates/ra_hir/src/from_id.rs
@@ -0,0 +1,63 @@
1//! Utility module for converting between hir_def ids and code_model wrappers.
2//!
3//! It's unclear if we need this long-term, but it's definitelly useful while we
4//! are splitting the hir.
5
6use hir_def::{AdtId, EnumVariantId, ModuleDefId};
7
8use crate::{Adt, EnumVariant, ModuleDef};
9
10macro_rules! from_id {
11 ($(($id:path, $ty:path)),*) => {$(
12 impl From<$id> for $ty {
13 fn from(id: $id) -> $ty {
14 $ty { id }
15 }
16 }
17 )*}
18}
19
20from_id![
21 (hir_def::ModuleId, crate::Module),
22 (hir_def::StructId, crate::Struct),
23 (hir_def::UnionId, crate::Union),
24 (hir_def::EnumId, crate::Enum),
25 (hir_def::TypeAliasId, crate::TypeAlias),
26 (hir_def::TraitId, crate::Trait),
27 (hir_def::StaticId, crate::Static),
28 (hir_def::ConstId, crate::Const),
29 (hir_def::FunctionId, crate::Function),
30 (hir_expand::MacroDefId, crate::MacroDef)
31];
32
33impl From<AdtId> for Adt {
34 fn from(id: AdtId) -> Self {
35 match id {
36 AdtId::StructId(it) => Adt::Struct(it.into()),
37 AdtId::UnionId(it) => Adt::Union(it.into()),
38 AdtId::EnumId(it) => Adt::Enum(it.into()),
39 }
40 }
41}
42
43impl From<EnumVariantId> for EnumVariant {
44 fn from(id: EnumVariantId) -> Self {
45 EnumVariant { parent: id.parent.into(), id: id.local_id }
46 }
47}
48
49impl From<ModuleDefId> for ModuleDef {
50 fn from(id: ModuleDefId) -> Self {
51 match id {
52 ModuleDefId::ModuleId(it) => ModuleDef::Module(it.into()),
53 ModuleDefId::FunctionId(it) => ModuleDef::Function(it.into()),
54 ModuleDefId::AdtId(it) => ModuleDef::Adt(it.into()),
55 ModuleDefId::EnumVariantId(it) => ModuleDef::EnumVariant(it.into()),
56 ModuleDefId::ConstId(it) => ModuleDef::Const(it.into()),
57 ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()),
58 ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()),
59 ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()),
60 ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it),
61 }
62 }
63}