codeviz_rust/
name.rs

1use codeviz_common::ElementFormat;
2use super::*;
3
4#[derive(Debug, Clone)]
5pub enum Name {
6    Imported(ImportedName),
7    BuiltIn(BuiltInName),
8    Local(LocalName),
9}
10
11impl Name {
12    pub fn imported(module: &str, name: &str) -> ImportedName {
13        ImportedName {
14            module: module.to_owned(),
15            name: name.to_owned(),
16            alias: None,
17        }
18    }
19
20    pub fn imported_alias(module: &str, name: &str, alias: &str) -> ImportedName {
21        ImportedName {
22            module: module.to_owned(),
23            name: name.to_owned(),
24            alias: Some(alias.to_owned()),
25        }
26    }
27
28    pub fn built_in(name: &str) -> BuiltInName {
29        BuiltInName { name: name.to_owned() }
30    }
31
32    pub fn local(name: &str) -> LocalName {
33        LocalName { name: name.to_owned() }
34    }
35
36    pub fn format<E>(&self, out: &mut E) -> Result<()>
37    where
38        E: ElementFormat,
39    {
40        match *self {
41            Name::Imported(ref imported) => {
42                if let Some(ref alias) = imported.alias {
43                    write!(out, "{}::{}", alias, imported.name.clone())?;
44                } else {
45                    if let Some(last) = imported.module.split("::").last() {
46                        write!(out, "{}::{}", last, imported.name.clone())?;
47                    }
48                }
49            }
50            Name::BuiltIn(ref built_in) => out.write_str(&built_in.name)?,
51            Name::Local(ref local) => out.write_str(&local.name)?,
52        }
53
54        Ok(())
55    }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
59pub struct ImportedName {
60    pub module: String,
61    pub name: String,
62    pub alias: Option<String>,
63}
64
65#[derive(Debug, Clone)]
66pub struct BuiltInName {
67    pub name: String,
68}
69
70#[derive(Debug, Clone)]
71pub struct LocalName {
72    pub name: String,
73}
74
75impl<'a, T> From<&'a T> for Name
76where
77    T: Into<Name> + Clone,
78{
79    fn from(value: &'a T) -> Name {
80        value.clone().into()
81    }
82}
83
84impl From<ImportedName> for Name {
85    fn from(value: ImportedName) -> Name {
86        Name::Imported(value)
87    }
88}
89
90impl From<BuiltInName> for Name {
91    fn from(value: BuiltInName) -> Name {
92        Name::BuiltIn(value)
93    }
94}
95
96impl From<LocalName> for Name {
97    fn from(value: LocalName) -> Name {
98        Name::Local(value)
99    }
100}
101
102impl From<Name> for Variable {
103    fn from(value: Name) -> Variable {
104        Variable::Name(value)
105    }
106}
107
108impl From<ImportedName> for Variable {
109    fn from(value: ImportedName) -> Variable {
110        Variable::Name(value.into())
111    }
112}
113
114impl From<BuiltInName> for Variable {
115    fn from(value: BuiltInName) -> Variable {
116        Variable::Name(value.into())
117    }
118}
119
120impl From<LocalName> for Variable {
121    fn from(value: LocalName) -> Variable {
122        Variable::Name(value.into())
123    }
124}