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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::{
borrow::Borrow,
fmt::{Display, Formatter, Result as FmtResult},
hash::{Hash, Hasher},
ops::Deref,
};
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Identifier {
Owned(String),
Refer(&'static str),
}
impl Default for Identifier {
fn default() -> Self {
Identifier::Refer("")
}
}
impl Display for Identifier {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
f.write_str(&self)
}
}
impl Eq for Identifier {}
impl PartialEq for Identifier {
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}
impl Hash for Identifier {
fn hash<H: Hasher>(&self, state: &mut H) {
use self::Identifier::*;
match self {
Owned(s) => s.hash(state),
Refer(s) => s.hash(state),
}
}
}
impl AsRef<str> for Identifier {
fn as_ref(&self) -> &str {
use self::Identifier::*;
match self {
Owned(s) => &s,
Refer(s) => s,
}
}
}
impl Borrow<str> for Identifier {
#[inline]
fn borrow(&self) -> &str {
self.as_ref()
}
}
impl Deref for Identifier {
type Target = str;
fn deref(&self) -> &str {
self.as_ref()
}
}
impl From<&'static str> for Identifier {
fn from(s: &'static str) -> Self {
Identifier::Refer(s)
}
}
impl From<String> for Identifier {
fn from(s: String) -> Self {
Identifier::Owned(s)
}
}
impl<'a> From<&'a String> for Identifier {
fn from(s: &String) -> Self {
Identifier::Owned(s.clone())
}
}