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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Automatically generated rust module for 'message.proto' file

#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(unused_imports)]
#![allow(unknown_lints)]
#![allow(clippy::all)]
#![cfg_attr(rustfmt, rustfmt_skip)]


use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result};
use quick_protobuf::sizeofs::*;
use super::super::*;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Status {
    OK = 100,
    RESERVATION_REFUSED = 200,
    RESOURCE_LIMIT_EXCEEDED = 201,
    PERMISSION_DENIED = 202,
    CONNECTION_FAILED = 203,
    NO_RESERVATION = 204,
    MALFORMED_MESSAGE = 400,
    UNEXPECTED_MESSAGE = 401,
}

impl Default for Status {
    fn default() -> Self {
        Status::OK
    }
}

impl From<i32> for Status {
    fn from(i: i32) -> Self {
        match i {
            100 => Status::OK,
            200 => Status::RESERVATION_REFUSED,
            201 => Status::RESOURCE_LIMIT_EXCEEDED,
            202 => Status::PERMISSION_DENIED,
            203 => Status::CONNECTION_FAILED,
            204 => Status::NO_RESERVATION,
            400 => Status::MALFORMED_MESSAGE,
            401 => Status::UNEXPECTED_MESSAGE,
            _ => Self::default(),
        }
    }
}

impl<'a> From<&'a str> for Status {
    fn from(s: &'a str) -> Self {
        match s {
            "OK" => Status::OK,
            "RESERVATION_REFUSED" => Status::RESERVATION_REFUSED,
            "RESOURCE_LIMIT_EXCEEDED" => Status::RESOURCE_LIMIT_EXCEEDED,
            "PERMISSION_DENIED" => Status::PERMISSION_DENIED,
            "CONNECTION_FAILED" => Status::CONNECTION_FAILED,
            "NO_RESERVATION" => Status::NO_RESERVATION,
            "MALFORMED_MESSAGE" => Status::MALFORMED_MESSAGE,
            "UNEXPECTED_MESSAGE" => Status::UNEXPECTED_MESSAGE,
            _ => Self::default(),
        }
    }
}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct HopMessage {
    pub type_pb: message_v2::pb::mod_HopMessage::Type,
    pub peer: Option<message_v2::pb::Peer>,
    pub reservation: Option<message_v2::pb::Reservation>,
    pub limit: Option<message_v2::pb::Limit>,
    pub status: Option<message_v2::pb::Status>,
}

impl<'a> MessageRead<'a> for HopMessage {
    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
        let mut msg = Self::default();
        while !r.is_eof() {
            match r.next_tag(bytes) {
                Ok(8) => msg.type_pb = r.read_enum(bytes)?,
                Ok(18) => msg.peer = Some(r.read_message::<message_v2::pb::Peer>(bytes)?),
                Ok(26) => msg.reservation = Some(r.read_message::<message_v2::pb::Reservation>(bytes)?),
                Ok(34) => msg.limit = Some(r.read_message::<message_v2::pb::Limit>(bytes)?),
                Ok(40) => msg.status = Some(r.read_enum(bytes)?),
                Ok(t) => { r.read_unknown(bytes, t)?; }
                Err(e) => return Err(e),
            }
        }
        Ok(msg)
    }
}

impl MessageWrite for HopMessage {
    fn get_size(&self) -> usize {
        0
        + 1 + sizeof_varint(*(&self.type_pb) as u64)
        + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
        + self.reservation.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
        + self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
        + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
    }

    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
        w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?;
        if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; }
        if let Some(ref s) = self.reservation { w.write_with_tag(26, |w| w.write_message(s))?; }
        if let Some(ref s) = self.limit { w.write_with_tag(34, |w| w.write_message(s))?; }
        if let Some(ref s) = self.status { w.write_with_tag(40, |w| w.write_enum(*s as i32))?; }
        Ok(())
    }
}

pub mod mod_HopMessage {


#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Type {
    RESERVE = 0,
    CONNECT = 1,
    STATUS = 2,
}

impl Default for Type {
    fn default() -> Self {
        Type::RESERVE
    }
}

impl From<i32> for Type {
    fn from(i: i32) -> Self {
        match i {
            0 => Type::RESERVE,
            1 => Type::CONNECT,
            2 => Type::STATUS,
            _ => Self::default(),
        }
    }
}

impl<'a> From<&'a str> for Type {
    fn from(s: &'a str) -> Self {
        match s {
            "RESERVE" => Type::RESERVE,
            "CONNECT" => Type::CONNECT,
            "STATUS" => Type::STATUS,
            _ => Self::default(),
        }
    }
}

}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct StopMessage {
    pub type_pb: message_v2::pb::mod_StopMessage::Type,
    pub peer: Option<message_v2::pb::Peer>,
    pub limit: Option<message_v2::pb::Limit>,
    pub status: Option<message_v2::pb::Status>,
}

impl<'a> MessageRead<'a> for StopMessage {
    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
        let mut msg = Self::default();
        while !r.is_eof() {
            match r.next_tag(bytes) {
                Ok(8) => msg.type_pb = r.read_enum(bytes)?,
                Ok(18) => msg.peer = Some(r.read_message::<message_v2::pb::Peer>(bytes)?),
                Ok(26) => msg.limit = Some(r.read_message::<message_v2::pb::Limit>(bytes)?),
                Ok(32) => msg.status = Some(r.read_enum(bytes)?),
                Ok(t) => { r.read_unknown(bytes, t)?; }
                Err(e) => return Err(e),
            }
        }
        Ok(msg)
    }
}

impl MessageWrite for StopMessage {
    fn get_size(&self) -> usize {
        0
        + 1 + sizeof_varint(*(&self.type_pb) as u64)
        + self.peer.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
        + self.limit.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
        + self.status.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
    }

    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
        w.write_with_tag(8, |w| w.write_enum(*&self.type_pb as i32))?;
        if let Some(ref s) = self.peer { w.write_with_tag(18, |w| w.write_message(s))?; }
        if let Some(ref s) = self.limit { w.write_with_tag(26, |w| w.write_message(s))?; }
        if let Some(ref s) = self.status { w.write_with_tag(32, |w| w.write_enum(*s as i32))?; }
        Ok(())
    }
}

pub mod mod_StopMessage {


#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Type {
    CONNECT = 0,
    STATUS = 1,
}

impl Default for Type {
    fn default() -> Self {
        Type::CONNECT
    }
}

impl From<i32> for Type {
    fn from(i: i32) -> Self {
        match i {
            0 => Type::CONNECT,
            1 => Type::STATUS,
            _ => Self::default(),
        }
    }
}

impl<'a> From<&'a str> for Type {
    fn from(s: &'a str) -> Self {
        match s {
            "CONNECT" => Type::CONNECT,
            "STATUS" => Type::STATUS,
            _ => Self::default(),
        }
    }
}

}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Peer {
    pub id: Vec<u8>,
    pub addrs: Vec<Vec<u8>>,
}

impl<'a> MessageRead<'a> for Peer {
    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
        let mut msg = Self::default();
        while !r.is_eof() {
            match r.next_tag(bytes) {
                Ok(10) => msg.id = r.read_bytes(bytes)?.to_owned(),
                Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()),
                Ok(t) => { r.read_unknown(bytes, t)?; }
                Err(e) => return Err(e),
            }
        }
        Ok(msg)
    }
}

impl MessageWrite for Peer {
    fn get_size(&self) -> usize {
        0
        + 1 + sizeof_len((&self.id).len())
        + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::<usize>()
    }

    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
        w.write_with_tag(10, |w| w.write_bytes(&**&self.id))?;
        for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; }
        Ok(())
    }
}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Reservation {
    pub expire: u64,
    pub addrs: Vec<Vec<u8>>,
    pub voucher: Option<Vec<u8>>,
}

impl<'a> MessageRead<'a> for Reservation {
    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
        let mut msg = Self::default();
        while !r.is_eof() {
            match r.next_tag(bytes) {
                Ok(8) => msg.expire = r.read_uint64(bytes)?,
                Ok(18) => msg.addrs.push(r.read_bytes(bytes)?.to_owned()),
                Ok(26) => msg.voucher = Some(r.read_bytes(bytes)?.to_owned()),
                Ok(t) => { r.read_unknown(bytes, t)?; }
                Err(e) => return Err(e),
            }
        }
        Ok(msg)
    }
}

impl MessageWrite for Reservation {
    fn get_size(&self) -> usize {
        0
        + 1 + sizeof_varint(*(&self.expire) as u64)
        + self.addrs.iter().map(|s| 1 + sizeof_len((s).len())).sum::<usize>()
        + self.voucher.as_ref().map_or(0, |m| 1 + sizeof_len((m).len()))
    }

    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
        w.write_with_tag(8, |w| w.write_uint64(*&self.expire))?;
        for s in &self.addrs { w.write_with_tag(18, |w| w.write_bytes(&**s))?; }
        if let Some(ref s) = self.voucher { w.write_with_tag(26, |w| w.write_bytes(&**s))?; }
        Ok(())
    }
}

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Limit {
    pub duration: Option<u32>,
    pub data: Option<u64>,
}

impl<'a> MessageRead<'a> for Limit {
    fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
        let mut msg = Self::default();
        while !r.is_eof() {
            match r.next_tag(bytes) {
                Ok(8) => msg.duration = Some(r.read_uint32(bytes)?),
                Ok(16) => msg.data = Some(r.read_uint64(bytes)?),
                Ok(t) => { r.read_unknown(bytes, t)?; }
                Err(e) => return Err(e),
            }
        }
        Ok(msg)
    }
}

impl MessageWrite for Limit {
    fn get_size(&self) -> usize {
        0
        + self.duration.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
        + self.data.as_ref().map_or(0, |m| 1 + sizeof_varint(*(m) as u64))
    }

    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
        if let Some(ref s) = self.duration { w.write_with_tag(8, |w| w.write_uint32(*s))?; }
        if let Some(ref s) = self.data { w.write_with_tag(16, |w| w.write_uint64(*s))?; }
        Ok(())
    }
}