aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/name.rs
blob: 7f42c9f04185fada484f07c44684261bc7df9279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::fmt;

use ra_syntax::{ast, SmolStr};

/// `Name` is a wrapper around string, which is used in hir for both references
/// and declarations. In theory, names should also carry hygene info, but we are
/// not there yet!
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Name {
    text: SmolStr,
}

impl fmt::Display for Name {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.text, f)
    }
}

impl Name {
    // TODO: get rid of this?
    pub(crate) fn as_str(&self) -> &str {
        self.text.as_str()
    }

    #[cfg(not(test))]
    fn new(text: SmolStr) -> Name {
        Name { text }
    }

    #[cfg(test)]
    pub(crate) fn new(text: SmolStr) -> Name {
        Name { text }
    }
}

pub(crate) trait AsName {
    fn as_name(&self) -> Name;
}

impl AsName for ast::NameRef<'_> {
    fn as_name(&self) -> Name {
        Name::new(self.text())
    }
}

impl AsName for ast::Name<'_> {
    fn as_name(&self) -> Name {
        Name::new(self.text())
    }
}

impl AsName for ra_db::Dependency {
    fn as_name(&self) -> Name {
        Name::new(self.name.clone())
    }
}