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
use core::fmt;
use serde::{Deserialize, Serialize};
use crate::compile::Component;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ComponentRef<'a> {
Crate(&'a str),
Str(&'a str),
Id(usize),
}
impl ComponentRef<'_> {
pub fn id(self) -> Option<usize> {
match self {
Self::Id(n) => Some(n),
_ => None,
}
}
pub fn to_owned(&self) -> Component {
match *self {
ComponentRef::Crate(s) => Component::Crate(s.into()),
ComponentRef::Str(s) => Component::Str(s.into()),
ComponentRef::Id(id) => Component::Id(id),
}
}
}
impl fmt::Display for ComponentRef<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Crate(s) => write!(fmt, "::{}", s),
Self::Str(s) => write!(fmt, "{}", s),
Self::Id(n) => write!(fmt, "${}", n),
}
}
}