-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
356 lines (320 loc) · 11.8 KB
/
main.rs
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
347
348
349
350
351
352
353
354
355
356
use quick_protobuf;
use serde_json;
use std::fs::File;
use std::io::{stdin, stdout, BufReader, Read, Write};
use std::path::PathBuf;
use structopt::*;
use rusp::{
usp_decoder::{decode_msg, decode_record},
usp_generator,
usp_types::NotifyType,
};
#[derive(StructOpt)]
#[structopt(name = "rusp", about = "the Rust USP toolkit")]
enum Rusp {
/// Decode a single raw USP message from standard input and print to standard output
#[structopt(name = "decode_msg")]
DecodeMsg {},
/// Decode a multiple USP messages from specified filenames and print to standard output
#[structopt(name = "decode_msg_files")]
DecodeMsgFiles {
#[structopt(parse(from_os_str), required = true)]
/// Filenames of USP protobuf messages to decode
files: Vec<PathBuf>,
},
/// Decode a single raw USP record from standard input and print to standard output
#[structopt(name = "decode_record")]
DecodeRecord {},
/// Decode a multiple USP records from specified filenames and print to standard output
#[structopt(name = "decode_record_files")]
DecodeRecordFiles {
#[structopt(parse(from_os_str), required = true)]
/// Filenames of USP protobuf records to decode
files: Vec<PathBuf>,
},
/// Encode command line input into a single raw USP message
#[structopt(name = "encode_msg")]
EncodeMsg {
/// The message ID to use in the USP Msg header
msgid: String,
/// Filename (will output to standard output if omitted)
#[structopt(parse(from_os_str), short = "f", long = "file")]
/// Output filename of file to encode USP protobuf message to
filename: Option<PathBuf>,
/// Type of message
#[structopt(subcommand)]
typ: MsgType,
},
/// Encode command line input into a single raw USP message body
#[structopt(name = "encode_msg_body")]
EncodeMsgBody {
/// Filename (will output to standard output if omitted)
#[structopt(parse(from_os_str), short = "f", long = "file")]
/// Output filename of file to encode USP protobuf message to
filename: Option<PathBuf>,
/// Type of message
#[structopt(subcommand)]
typ: MsgType,
},
/// Extract the USP message from an USP record
#[structopt(name = "extract_msg")]
ExtractMsg {
#[structopt(parse(from_os_str))]
/// Input filename of USP protobuf record to decode
in_file: PathBuf,
/// Output filename of USP protobuf message to write into
#[structopt(parse(from_os_str))]
out_file: PathBuf,
},
/// Extract the USP message body from an USP record
#[structopt(name = "extract_msg_body")]
ExtractMsgBody {
#[structopt(parse(from_os_str))]
/// Input filename of USP protobuf record to decode
in_file: PathBuf,
/// Output filename of USP protobuf message body to write into
#[structopt(parse(from_os_str))]
out_file: PathBuf,
},
/// Wrap msg from stdin into a single raw USP record
#[structopt(name = "wrap_msg_raw")]
WrapMsgRaw {
#[structopt(long = "version")]
/// USP specification version
version: Option<String>,
#[structopt(long = "from")]
/// Sender Id
from: Option<String>,
#[structopt(long = "to")]
/// Recipient Id
to: Option<String>,
/// Filename (will output to standard output if omitted)
#[structopt(parse(from_os_str), short = "f", long = "file")]
/// Output filename of file to encode USP protobuf record to
filename: Option<PathBuf>,
},
}
#[derive(StructOpt, Debug)]
enum MsgType {
/// Generate an USP Error message
USPError {
/// The USP error code (MUST be between 7000 and 7999)
code: u32,
/// An (optional) error message. Standard error messages will be computed from the error
/// code if not provided
message: Option<String>,
},
/// Generate an USP Get request message
USPGet {
/// A JSON array of Strings resembling the paths for the Get operation
#[structopt(multiple = true)]
paths: Vec<String>,
},
/// Generate an USP GetResp response message
USPGetResp {
/// A JSON array of Strings resembling the result data for the GetResp operation
#[structopt(multiple = true)]
result: Vec<String>,
},
/// Generate an USP Notify "request" message
USPNotify {
/// Subscription ID
sub_id: String,
/// Do we expect a resonse?
send_resp: bool,
/// Type of notification
#[structopt(subcommand)]
typ: NotifyType,
},
/// Generate an USP Notify "response" message
USPNotifyResp {
/// Subscription ID
sub_id: String,
},
}
fn decode_msg_files(files: Vec<PathBuf>) {
for file in files {
let fp = File::open(&file).unwrap_or_else(|_| panic!("Could not open file {:?}", file));
let mut buf_reader = BufReader::new(fp);
let mut contents = Vec::new();
buf_reader
.read_to_end(&mut contents)
.unwrap_or_else(|_| panic!("Could not read from file {:?}", file));
println!("{}", decode_msg(&contents));
}
}
fn decode_msg_stdin() {
let mut contents = Vec::new();
stdin()
.read_to_end(&mut contents)
.expect("Couldn't read USP Msg from stdin");
println!("{}", decode_msg(&contents));
}
fn decode_record_files(files: Vec<PathBuf>) {
for file in files {
let fp = File::open(&file).unwrap_or_else(|_| panic!("Could not open file {:?}", file));
let mut buf_reader = BufReader::new(fp);
let mut contents = Vec::new();
buf_reader
.read_to_end(&mut contents)
.unwrap_or_else(|_| panic!("Could not read from file {:?}", file));
println!("{}", decode_record(&contents));
}
}
fn decode_record_stdin() {
let mut contents = Vec::new();
stdin()
.read_to_end(&mut contents)
.expect("Couldn't read USP Record from stdin");
println!("{}", decode_record(&contents));
}
fn encode_msg_body_buf(typ: MsgType) -> Vec<u8> {
use quick_protobuf::serialize_into_vec;
match typ {
MsgType::USPError { code, message } => {
serialize_into_vec(&usp_generator::usp_simple_error(code, message))
}
MsgType::USPGet { paths } => {
let paths = paths.join(" ");
let v: Vec<&str> = serde_json::from_str(&paths).unwrap();
serialize_into_vec(&usp_generator::usp_get_request(v.as_slice()))
}
MsgType::USPGetResp { result } => {
let result = result.join(" ");
let getresp_json: usp_generator::GetResp = serde_json::from_str(&result).unwrap();
serialize_into_vec(&usp_generator::usp_get_response_from_json(&getresp_json))
}
MsgType::USPNotify {
sub_id,
send_resp,
typ,
} => serialize_into_vec(&usp_generator::usp_notify_request(&sub_id, send_resp, typ)),
MsgType::USPNotifyResp { sub_id } => {
serialize_into_vec(&usp_generator::usp_notify_response(&sub_id))
}
}
.expect("Cannot encode message")
}
fn encode_msg_body(filename: Option<PathBuf>, typ: MsgType) {
use quick_protobuf::{deserialize_from_slice, message::MessageWrite, Writer};
let mut buf = Vec::new();
let mut writer = Writer::new(&mut buf);
let encoded_body = encode_msg_body_buf(typ);
let body: rusp::usp::Body = deserialize_from_slice(&encoded_body).unwrap();
body.write_message(&mut writer)
.expect("Failed encoding USP Msg");
if filename.is_some() {
std::fs::write(filename.unwrap(), buf).unwrap();
} else {
stdout().write_all(&buf).unwrap();
}
}
fn encode_msg(msgid: String, filename: Option<PathBuf>, typ: MsgType) {
use quick_protobuf::{deserialize_from_slice, message::MessageWrite, Writer};
let mut buf = Vec::new();
let mut writer = Writer::new(&mut buf);
let encoded_body = encode_msg_body_buf(typ);
let body: rusp::usp::Body = deserialize_from_slice(&encoded_body).unwrap();
usp_generator::usp_msg(msgid, body)
.write_message(&mut writer)
.expect("Failed encoding USP Msg");
if filename.is_some() {
std::fs::write(filename.unwrap(), buf).unwrap();
} else {
stdout().write_all(&buf).unwrap();
}
}
fn extract_msg(in_file: &PathBuf, out_file: &PathBuf) {
use rusp::usp_record::mod_Record::OneOfrecord_type::*;
let fp = File::open(&in_file).unwrap_or_else(|_| panic!("Could not open file {:?}", in_file));
let mut buf_reader = BufReader::new(fp);
let mut contents = Vec::new();
buf_reader
.read_to_end(&mut contents)
.unwrap_or_else(|_| panic!("Could not read from file {:?}", in_file));
let record = decode_record(&contents);
match record.record_type {
no_session_context(context) => {
let msg = context.payload;
std::fs::write(&out_file, &msg).unwrap();
}
session_context(_) => unreachable!(),
None => unreachable!(),
}
}
fn extract_msg_body(in_file: &PathBuf, out_file: &PathBuf) {
use quick_protobuf::{message::MessageWrite, Writer};
use rusp::usp_record::mod_Record::OneOfrecord_type::*;
let fp = File::open(&in_file).unwrap_or_else(|_| panic!("Could not open file {:?}", in_file));
let mut buf_reader = BufReader::new(fp);
let mut contents = Vec::new();
buf_reader
.read_to_end(&mut contents)
.unwrap_or_else(|_| panic!("Could not read from file {:?}", in_file));
let record = decode_record(&contents);
match record.record_type {
no_session_context(context) => {
let mut buf = Vec::new();
let mut writer = Writer::new(&mut buf);
let payload = context.payload;
let msg = decode_msg(&payload);
let body = msg.body.unwrap();
body.write_message(&mut writer)
.expect("Cannot encode message");
std::fs::write(&out_file, buf).unwrap();
}
session_context(_) => unreachable!(),
None => unreachable!(),
}
}
fn wrap_msg_raw(
version: Option<String>,
from: Option<String>,
to: Option<String>,
filename: Option<PathBuf>,
) {
use quick_protobuf::{message::MessageWrite, Writer};
let mut msg = Vec::new();
stdin()
.read_to_end(&mut msg)
.expect("Couldn't read USP Msg from stdin");
let mut buf = Vec::new();
let mut writer = Writer::new(&mut buf);
usp_generator::usp_no_session_context_record(
version.as_ref().map(String::as_str).unwrap(),
from.as_ref().map(String::as_str).unwrap(),
to.as_ref().map(String::as_str).unwrap(),
&msg,
)
.write_message(&mut writer)
.expect("Failed encoding USP Record");
if filename.is_some() {
std::fs::write(filename.unwrap(), buf).unwrap();
} else {
stdout().write_all(&buf).unwrap();
}
}
#[paw::main]
fn main(opt: Rusp) {
color_backtrace::install();
match opt {
Rusp::DecodeRecordFiles { files } => decode_record_files(files),
Rusp::DecodeRecord {} => decode_record_stdin(),
Rusp::DecodeMsgFiles { files } => decode_msg_files(files),
Rusp::DecodeMsg {} => decode_msg_stdin(),
Rusp::EncodeMsgBody { filename, typ } => encode_msg_body(filename, typ),
Rusp::EncodeMsg {
msgid,
filename,
typ,
} => encode_msg(msgid, filename, typ),
Rusp::ExtractMsg { in_file, out_file } => extract_msg(&in_file, &out_file),
Rusp::ExtractMsgBody { in_file, out_file } => extract_msg_body(&in_file, &out_file),
Rusp::WrapMsgRaw {
version,
from,
to,
filename,
} => wrap_msg_raw(version, from, to, filename),
}
}