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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use {Loc, Pos, RpEnumBody, RpField, RpInterfaceBody, RpName, RpServiceBody, RpSubType,
RpTupleBody, RpTypeBody, RpVariant};
use errors::Result;
use std::fmt;
use std::rc::Rc;
#[derive(Debug, Clone)]
pub enum RpReg {
Type(Rc<Loc<RpTypeBody>>),
Tuple(Rc<Loc<RpTupleBody>>),
Interface(Rc<Loc<RpInterfaceBody>>),
SubType(Rc<Loc<RpInterfaceBody>>, Rc<Loc<RpSubType>>),
Enum(Rc<Loc<RpEnumBody>>),
EnumVariant(Rc<Loc<RpEnumBody>>, Rc<Loc<RpVariant>>),
Service(Rc<Loc<RpServiceBody>>),
}
impl RpReg {
pub fn name(&self) -> &RpName {
use self::RpReg::*;
match *self {
Type(ref target) => &target.name,
Tuple(ref target) => &target.name,
Service(ref target) => &target.name,
Interface(ref target) => &target.name,
Enum(ref target) => &target.name,
SubType(_, ref target) => &target.name,
EnumVariant(_, ref target) => &target.name,
}
}
pub fn pos(&self) -> &Pos {
use self::RpReg::*;
match *self {
Type(ref target) => Loc::pos(target),
Tuple(ref target) => Loc::pos(target),
Service(ref target) => Loc::pos(target),
Interface(ref target) => Loc::pos(target),
Enum(ref target) => Loc::pos(target),
SubType(_, ref target) => Loc::pos(target),
EnumVariant(_, ref target) => Loc::pos(target),
}
}
pub fn fields<'a>(&'a self) -> Result<Box<Iterator<Item = &Loc<RpField>> + 'a>> {
use self::RpReg::*;
let fields: Box<Iterator<Item = &Loc<RpField>>> = match *self {
Type(ref target) => Box::new(target.fields.iter()),
Tuple(ref target) => Box::new(target.fields.iter()),
Interface(ref target) => Box::new(target.fields.iter()),
SubType(ref parent, ref target) => {
Box::new(parent.fields.iter().chain(target.fields.iter()))
}
_ => return Err(format!("{}: type doesn't have fields", self).into()),
};
Ok(fields)
}
pub fn local_name<PackageFn, InnerFn>(
&self,
name: &RpName,
package_fn: PackageFn,
inner_fn: InnerFn,
) -> String
where
PackageFn: Fn(Vec<&str>) -> String,
InnerFn: Fn(Vec<&str>) -> String,
{
use self::RpReg::*;
match *self {
Type(_) | Interface(_) | Enum(_) | Tuple(_) | Service(_) => {
let p = name.parts.iter().map(String::as_str).collect();
package_fn(p)
}
SubType { .. } | EnumVariant { .. } => {
let mut v: Vec<&str> = name.parts.iter().map(String::as_str).collect();
let at = v.len().saturating_sub(2);
let last = inner_fn(v.split_off(at));
let mut parts = v.clone();
parts.push(last.as_str());
inner_fn(parts)
}
}
}
pub fn kind(&self) -> (&str, Option<&RpReg>) {
use self::RpReg::*;
let result = match *self {
Type(_) => "type",
Interface(_) => "interface",
Enum(_) => "enum",
Tuple(_) => "tuple",
Service(_) => "service",
SubType(_, _) => return ("interface", Some(self)),
EnumVariant(_, _) => return ("enum", Some(self)),
};
(result, None)
}
pub fn is_enum(&self) -> bool {
use self::RpReg::*;
match *self {
Enum(_) => true,
_ => false,
}
}
}
impl fmt::Display for RpReg {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use self::RpReg::*;
match *self {
Type(ref body) => write!(fmt, "type {}", body.name),
Interface(ref body) => write!(fmt, "interface {}", body.name),
Enum(ref body) => write!(fmt, "enum {}", body.name),
Tuple(ref body) => write!(fmt, "tuple {}", body.name),
Service(ref body) => write!(fmt, "service {}", body.name),
SubType(_, ref sub_type) => write!(fmt, "subtype {}", sub_type.name),
EnumVariant(_, ref variant) => write!(fmt, "variant {}", variant.name),
}
}
}