aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/name.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/name.rs')
-rw-r--r--crates/ra_hir/src/name.rs97
1 files changed, 97 insertions, 0 deletions
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs
new file mode 100644
index 000000000..e4fc141a6
--- /dev/null
+++ b/crates/ra_hir/src/name.rs
@@ -0,0 +1,97 @@
1use std::fmt;
2
3use ra_syntax::{ast, SmolStr};
4
5/// `Name` is a wrapper around string, which is used in hir for both references
6/// and declarations. In theory, names should also carry hygene info, but we are
7/// not there yet!
8#[derive(Clone, PartialEq, Eq, Hash)]
9pub struct Name {
10 text: SmolStr,
11}
12
13impl fmt::Display for Name {
14 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15 fmt::Display::fmt(&self.text, f)
16 }
17}
18
19impl fmt::Debug for Name {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 fmt::Debug::fmt(&self.text, f)
22 }
23}
24
25impl Name {
26 pub(crate) fn as_known_name(&self) -> Option<KnownName> {
27 let name = match self.text.as_str() {
28 "isize" => KnownName::Isize,
29 "i8" => KnownName::I8,
30 "i16" => KnownName::I16,
31 "i32" => KnownName::I32,
32 "i64" => KnownName::I64,
33 "i128" => KnownName::I128,
34 "usize" => KnownName::Usize,
35 "u8" => KnownName::U8,
36 "u16" => KnownName::U16,
37 "u32" => KnownName::U32,
38 "u64" => KnownName::U64,
39 "u128" => KnownName::U128,
40 "f32" => KnownName::F32,
41 "f64" => KnownName::F64,
42 _ => return None,
43 };
44 Some(name)
45 }
46
47 fn new(text: SmolStr) -> Name {
48 Name { text }
49 }
50}
51
52pub(crate) trait AsName {
53 fn as_name(&self) -> Name;
54}
55
56impl AsName for ast::NameRef<'_> {
57 fn as_name(&self) -> Name {
58 Name::new(self.text())
59 }
60}
61
62impl AsName for ast::Name<'_> {
63 fn as_name(&self) -> Name {
64 Name::new(self.text())
65 }
66}
67
68impl AsName for ra_db::Dependency {
69 fn as_name(&self) -> Name {
70 Name::new(self.name.clone())
71 }
72}
73
74// Ideally, should be replaced with
75// ```
76// const ISIZE: Name = Name::new("isize")
77// ```
78// but const-fn is not that powerful yet.
79#[derive(Debug)]
80pub(crate) enum KnownName {
81 Isize,
82 I8,
83 I16,
84 I32,
85 I64,
86 I128,
87
88 Usize,
89 U8,
90 U16,
91 U32,
92 U64,
93 U128,
94
95 F32,
96 F64,
97}