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 write!(out, "{}.{}", imported.module, imported.name.clone())?;
46 }
47 }
48 Name::BuiltIn(ref built_in) => out.write_str(&built_in.name)?,
49 Name::Local(ref local) => out.write_str(&local.name)?,
50 }
51
52 Ok(())
53 }
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
57pub struct ImportedName {
58 pub module: String,
59 pub name: String,
60 pub alias: Option<String>,
61}
62
63#[derive(Debug, Clone)]
64pub struct BuiltInName {
65 pub name: String,
66}
67
68#[derive(Debug, Clone)]
69pub struct LocalName {
70 pub name: String,
71}
72
73impl<'a, T> From<&'a T> for Name
74where
75 T: Into<Name> + Clone,
76{
77 fn from(value: &'a T) -> Name {
78 value.clone().into()
79 }
80}
81
82impl From<ImportedName> for Name {
83 fn from(value: ImportedName) -> Name {
84 Name::Imported(value)
85 }
86}
87
88impl From<BuiltInName> for Name {
89 fn from(value: BuiltInName) -> Name {
90 Name::BuiltIn(value)
91 }
92}
93
94impl From<LocalName> for Name {
95 fn from(value: LocalName) -> Name {
96 Name::Local(value)
97 }
98}
99
100impl From<Name> for Variable {
101 fn from(value: Name) -> Variable {
102 Variable::Name(value)
103 }
104}
105
106impl From<ImportedName> for Variable {
107 fn from(value: ImportedName) -> Variable {
108 Variable::Name(value.into())
109 }
110}
111
112impl From<BuiltInName> for Variable {
113 fn from(value: BuiltInName) -> Variable {
114 Variable::Name(value.into())
115 }
116}
117
118impl From<LocalName> for Variable {
119 fn from(value: LocalName) -> Variable {
120 Variable::Name(value.into())
121 }
122}