Switch unwrap/expect to use die crate for proper messages and error codes
This commit is contained in:
parent
12ea8c5531
commit
37b5efd4b6
440
Cargo.lock
generated
440
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "sendxmpp"
|
name = "sendxmpp"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
authors = ["moparisthebest <admin@moparisthebest.com>"]
|
authors = ["moparisthebest <admin@moparisthebest.com>"]
|
||||||
|
|
||||||
description = "Send XMPP messages from the command line."
|
description = "Send XMPP messages from the command line."
|
||||||
@ -25,3 +25,4 @@ tokio-xmpp = "1.0.0"
|
|||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
tokio = "0.1"
|
tokio = "0.1"
|
||||||
xmpp-parsers = "0.12.2"
|
xmpp-parsers = "0.12.2"
|
||||||
|
die = "0.2.0"
|
||||||
|
54
src/main.rs
54
src/main.rs
@ -4,6 +4,7 @@ use std::io::{stdin, Read};
|
|||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
use die::{die, Die};
|
||||||
use gumdrop::Options;
|
use gumdrop::Options;
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
@ -62,50 +63,56 @@ fn main() {
|
|||||||
// Remember to skip the first argument. That's the program name.
|
// Remember to skip the first argument. That's the program name.
|
||||||
let opts = match MyOptions::parse_args_default(&args[1..]) {
|
let opts = match MyOptions::parse_args_default(&args[1..]) {
|
||||||
Ok(opts) => opts,
|
Ok(opts) => opts,
|
||||||
Err(e) => {
|
Err(e) => die!(
|
||||||
println!("{}: {}", args[0], e);
|
"{}: {}\nUsage: {} [OPTIONS] [ARGUMENTS]\n\n{}",
|
||||||
println!("Usage: {} [OPTIONS] [ARGUMENTS]", args[0]);
|
args[0],
|
||||||
println!();
|
e,
|
||||||
println!("{}", MyOptions::usage());
|
args[0],
|
||||||
return;
|
MyOptions::usage()
|
||||||
}
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
if opts.help {
|
if opts.help {
|
||||||
println!("Usage: {} [OPTIONS] [ARGUMENTS]", args[0]);
|
die!(
|
||||||
println!();
|
"Usage: {} [OPTIONS] [ARGUMENTS]\n\n{}",
|
||||||
println!("{}", MyOptions::usage());
|
args[0],
|
||||||
return;
|
MyOptions::usage()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let recipients: Vec<Jid> = opts
|
let recipients: Vec<Jid> = opts
|
||||||
.recipients
|
.recipients
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| s.parse::<Jid>().expect("invalid recipient jid"))
|
.map(|s| s.parse::<Jid>().die("invalid recipient jid"))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
if recipients.is_empty() {
|
||||||
|
die!("no recipients specified!");
|
||||||
|
}
|
||||||
|
|
||||||
let recipients = &recipients;
|
let recipients = &recipients;
|
||||||
|
|
||||||
let cfg = match opts.config {
|
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(
|
None => parse_cfg(
|
||||||
dirs::config_dir()
|
dirs::config_dir()
|
||||||
.expect("cannot find home directory")
|
.die("cannot find home directory")
|
||||||
.join("sendxmpp.toml"),
|
.join("sendxmpp.toml"),
|
||||||
)
|
)
|
||||||
.or_else(|| parse_cfg("/etc/sendxmpp/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();
|
let mut data = String::new();
|
||||||
stdin()
|
stdin()
|
||||||
.read_to_string(&mut data)
|
.read_to_string(&mut data)
|
||||||
.expect("error reading from stdin");
|
.die("error reading from stdin");
|
||||||
let data = data.trim();
|
let data = data.trim();
|
||||||
|
|
||||||
// tokio_core context
|
// tokio_core context
|
||||||
let mut rt = Runtime::new().unwrap();
|
let mut rt = Runtime::new().die("unknown error");
|
||||||
// Client instance
|
// 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
|
// Make the two interfaces for sending and receiving independent
|
||||||
// of each other so we can move one into a closure.
|
// of each other so we can move one into a closure.
|
||||||
@ -115,13 +122,13 @@ fn main() {
|
|||||||
// Main loop, processes events
|
// Main loop, processes events
|
||||||
let done = stream.for_each(move |event| {
|
let done = stream.for_each(move |event| {
|
||||||
if event.is_online() {
|
if event.is_online() {
|
||||||
let mut sink = sink_state.take().unwrap();
|
let mut sink = sink_state.take().die("unknown error");
|
||||||
for recipient in recipients {
|
for recipient in recipients {
|
||||||
let reply = make_reply(recipient.clone(), &data);
|
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)
|
sink.start_send(Packet::StreamEnd)
|
||||||
.expect("send stream end failed");
|
.die("send stream end failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
Box::new(future::ok(()))
|
Box::new(future::ok(()))
|
||||||
@ -130,10 +137,7 @@ fn main() {
|
|||||||
// Start polling `done`
|
// Start polling `done`
|
||||||
match rt.block_on(done) {
|
match rt.block_on(done) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(e) => {
|
Err(e) => die!("Fatal: {}", e),
|
||||||
println!("Fatal: {}", e);
|
|
||||||
()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user