Switch unwrap/expect to use die crate for proper messages and error codes

This commit is contained in:
Travis Burtrum 2019-03-02 13:19:48 -05:00
parent 12ea8c5531
commit 37b5efd4b6
3 changed files with 230 additions and 267 deletions

440
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "sendxmpp"
version = "1.0.0"
version = "1.0.1"
authors = ["moparisthebest <admin@moparisthebest.com>"]
description = "Send XMPP messages from the command line."
@ -25,3 +25,4 @@ tokio-xmpp = "1.0.0"
futures = "0.1"
tokio = "0.1"
xmpp-parsers = "0.12.2"
die = "0.2.0"

View File

@ -4,6 +4,7 @@ use std::io::{stdin, Read};
use std::iter::Iterator;
use std::path::Path;
use die::{die, Die};
use gumdrop::Options;
use serde_derive::Deserialize;
@ -62,50 +63,56 @@ fn main() {
// Remember to skip the first argument. That's the program name.
let opts = match MyOptions::parse_args_default(&args[1..]) {
Ok(opts) => opts,
Err(e) => {
println!("{}: {}", args[0], e);
println!("Usage: {} [OPTIONS] [ARGUMENTS]", args[0]);
println!();
println!("{}", MyOptions::usage());
return;
}
Err(e) => die!(
"{}: {}\nUsage: {} [OPTIONS] [ARGUMENTS]\n\n{}",
args[0],
e,
args[0],
MyOptions::usage()
),
};
if opts.help {
println!("Usage: {} [OPTIONS] [ARGUMENTS]", args[0]);
println!();
println!("{}", MyOptions::usage());
return;
die!(
"Usage: {} [OPTIONS] [ARGUMENTS]\n\n{}",
args[0],
MyOptions::usage()
);
}
let recipients: Vec<Jid> = opts
.recipients
.iter()
.map(|s| s.parse::<Jid>().expect("invalid recipient jid"))
.map(|s| s.parse::<Jid>().die("invalid recipient jid"))
.collect();
if recipients.is_empty() {
die!("no recipients specified!");
}
let recipients = &recipients;
let cfg = match opts.config {
Some(config) => parse_cfg(&config).expect("provided config cannot be found/parsed"),
Some(config) => parse_cfg(&config).die("provided config cannot be found/parsed"),
None => parse_cfg(
dirs::config_dir()
.expect("cannot find home directory")
.die("cannot find home directory")
.join("sendxmpp.toml"),
)
.or_else(|| parse_cfg("/etc/sendxmpp/sendxmpp.toml"))
.expect("valid config file not found"),
.die("valid config file not found"),
};
let mut data = String::new();
stdin()
.read_to_string(&mut data)
.expect("error reading from stdin");
.die("error reading from stdin");
let data = data.trim();
// tokio_core context
let mut rt = Runtime::new().unwrap();
let mut rt = Runtime::new().die("unknown error");
// Client instance
let client = Client::new(&cfg.jid, &cfg.password).expect("could not connect to xmpp server");
let client = Client::new(&cfg.jid, &cfg.password).die("could not connect to xmpp server");
// Make the two interfaces for sending and receiving independent
// of each other so we can move one into a closure.
@ -115,13 +122,13 @@ fn main() {
// Main loop, processes events
let done = stream.for_each(move |event| {
if event.is_online() {
let mut sink = sink_state.take().unwrap();
let mut sink = sink_state.take().die("unknown error");
for recipient in recipients {
let reply = make_reply(recipient.clone(), &data);
sink.start_send(Packet::Stanza(reply)).expect("send failed");
sink.start_send(Packet::Stanza(reply)).die("send failed");
}
sink.start_send(Packet::StreamEnd)
.expect("send stream end failed");
.die("send stream end failed");
}
Box::new(future::ok(()))
@ -130,10 +137,7 @@ fn main() {
// Start polling `done`
match rt.block_on(done) {
Ok(_) => (),
Err(e) => {
println!("Fatal: {}", e);
()
}
Err(e) => die!("Fatal: {}", e),
};
}