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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use super::{Loc, Pos, RpEnumBody, RpInterfaceBody, RpName, RpReg, RpServiceBody, RpTupleBody,
RpTypeBody};
use std::fmt;
use std::rc::Rc;
use std::vec;
pub struct Decls<'a> {
iter: vec::IntoIter<&'a RpDecl>,
}
impl<'a> Iterator for Decls<'a> {
type Item = &'a RpDecl;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RpDecl {
Type(Rc<Loc<RpTypeBody>>),
Tuple(Rc<Loc<RpTupleBody>>),
Interface(Rc<Loc<RpInterfaceBody>>),
Enum(Rc<Loc<RpEnumBody>>),
Service(Rc<Loc<RpServiceBody>>),
}
impl RpDecl {
pub fn decls(&self) -> Decls {
use self::RpDecl::*;
let iter = match *self {
Type(ref body) => body.decls.iter().collect::<Vec<_>>().into_iter(),
Interface(ref body) => {
let mut decls = body.decls.iter().collect::<Vec<_>>();
decls.extend(body.sub_types.values().flat_map(|s| s.decls.iter()));
decls.into_iter()
}
Enum(ref body) => body.decls.iter().collect::<Vec<_>>().into_iter(),
Tuple(ref body) => body.decls.iter().collect::<Vec<_>>().into_iter(),
Service(ref body) => body.decls.iter().collect::<Vec<_>>().into_iter(),
};
Decls { iter: iter }
}
pub fn local_name(&self) -> &str {
use self::RpDecl::*;
match *self {
Type(ref body) => body.local_name.as_str(),
Interface(ref body) => body.local_name.as_str(),
Enum(ref body) => body.local_name.as_str(),
Tuple(ref body) => body.local_name.as_str(),
Service(ref body) => body.local_name.as_str(),
}
}
pub fn name(&self) -> &RpName {
use self::RpDecl::*;
match *self {
Type(ref body) => &body.name,
Interface(ref body) => &body.name,
Enum(ref body) => &body.name,
Tuple(ref body) => &body.name,
Service(ref body) => &body.name,
}
}
pub fn comment(&self) -> &[String] {
use self::RpDecl::*;
match *self {
Type(ref body) => &body.comment,
Interface(ref body) => &body.comment,
Enum(ref body) => &body.comment,
Tuple(ref body) => &body.comment,
Service(ref body) => &body.comment,
}
}
pub fn to_reg(&self) -> Vec<RpReg> {
use self::RpDecl::*;
let mut out = Vec::new();
match *self {
Type(ref ty) => {
out.push(RpReg::Type(Rc::clone(ty)));
}
Interface(ref interface) => {
for sub_type in interface.sub_types.values() {
out.push(RpReg::SubType(Rc::clone(interface), Rc::clone(sub_type)));
}
out.push(RpReg::Interface(Rc::clone(interface)));
}
Enum(ref en) => {
for variant in &en.variants {
out.push(RpReg::EnumVariant(Rc::clone(en), Rc::clone(variant)));
}
out.push(RpReg::Enum(Rc::clone(en)));
}
Tuple(ref tuple) => {
out.push(RpReg::Tuple(Rc::clone(tuple)));
}
Service(ref service) => {
out.push(RpReg::Service(Rc::clone(service)));
}
}
out.extend(self.decls().flat_map(|d| d.to_reg()));
out
}
pub fn kind(&self) -> &str {
use self::RpDecl::*;
match *self {
Type(_) => "type",
Interface(_) => "interface",
Enum(_) => "enum",
Tuple(_) => "tuple",
Service(_) => "service",
}
}
pub fn pos(&self) -> &Pos {
use self::RpDecl::*;
match *self {
Type(ref body) => Loc::pos(body),
Interface(ref body) => Loc::pos(body),
Enum(ref body) => Loc::pos(body),
Tuple(ref body) => Loc::pos(body),
Service(ref body) => Loc::pos(body),
}
}
}
impl fmt::Display for RpDecl {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::RpDecl::*;
match *self {
Type(ref body) => write!(f, "type {}", body.name),
Interface(ref body) => write!(f, "interface {}", body.name),
Enum(ref body) => write!(f, "enum {}", body.name),
Tuple(ref body) => write!(f, "tuple {}", body.name),
Service(ref body) => write!(f, "service {}", body.name),
}
}
}