From 5112be68a93eb113f50271d145513fc3da6713ce Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Sat, 12 Aug 2017 23:59:43 -0400 Subject: [PATCH] Tweaks and cleanup --- Cargo.lock | 2 +- Cargo.toml | 7 +++++-- src/main.rs | 43 ++++++++++++++++++++++--------------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b6c126..ff4409d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,5 @@ [root] -name = "sendxmpp-rs" +name = "sendxmpp" version = "0.1.0" dependencies = [ "gumdrop 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 290e277..fc3e524 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,10 @@ [package] -name = "sendxmpp-rs" +name = "sendxmpp" version = "0.1.0" authors = ["moparisthebest "] +description = "A fun game where you guess what number the computer has chosen." +license = "GPL-3.0+" +readme = "README.md" [dependencies] xmpp = { git = "https://gitlab.com/lumi/xmpp-rs.git" } @@ -10,4 +13,4 @@ toml = "0.4.4" serde_derive = "1.0.11" serde = "1.0.11" gumdrop = "0.3.0" -gumdrop_derive = "0.3.0" \ No newline at end of file +gumdrop_derive = "0.3.0" diff --git a/src/main.rs b/src/main.rs index 62f8b07..4875dd8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use std::env; use std::iter::Iterator; use std::io::{Read, stdin}; use std::fs::File; +use std::path::Path; use std::env::args; use gumdrop::Options; @@ -26,15 +27,20 @@ struct Config { password: String, } -fn parse_cfg(path: &str) -> Option { - File::open(path).and_then(|mut f| { - let mut input = String::new(); - f.read_to_string(&mut input)?; - match toml::from_str(&input) { - Ok(toml) => Ok(toml), - Err(error) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, error)) +fn parse_cfg>(path: P) -> Option { + match File::open(path) { + Ok(mut f) => { + let mut input = String::new(); + match f.read_to_string(&mut input) { + Ok(_) => match toml::from_str(&input) { + Ok(toml) => Some(toml), + Err(_) => None + }, + Err(_) => None + } } - }).ok() + Err(_) => None + } } #[derive(Default, Options)] @@ -77,20 +83,15 @@ fn main() { return; } - let recipients: Vec = opts.recipients.iter().map(|s| s.parse::().unwrap()).collect(); + let recipients: Vec = opts.recipients.iter().map(|s| s.parse::().expect("invalid recipient jid")).collect(); let cfg = match opts.config { - Some(config) => parse_cfg(&config).unwrap(), - None => { - let mut home_cfg = String::new(); - home_cfg += env::home_dir().unwrap().to_str().unwrap(); - home_cfg += "/.config/sendxmpp.toml"; - - parse_cfg(&home_cfg).unwrap_or_else(|| parse_cfg("/etc/sendxmpp/sendxmpp.toml").unwrap()) - } + Some(config) => parse_cfg(&config).expect("provided config cannot be found/parsed"), + None => parse_cfg(env::home_dir().expect("cannot find home directory").join(".config/sendxmpp.toml")) + .or_else(|| parse_cfg("/etc/sendxmpp/sendxmpp.toml")).expect("valid config file not found") }; - let jid: Jid = cfg.jid.parse().unwrap(); + let jid: Jid = cfg.jid.parse().expect("invalid jid in config file"); let mut data = String::new(); stdin().read_to_string(&mut data).expect("error reading from stdin"); @@ -98,17 +99,17 @@ fn main() { let mut client = ClientBuilder::new(jid) .password(cfg.password) .connect() - .unwrap(); + .expect("client cannot connect"); client.register_plugin(MessagingPlugin::new()); for recipient in recipients { - client.plugin::().send_message(&recipient, &data).unwrap(); + client.plugin::().send_message(&recipient, &data).expect("error sending message"); } thread::spawn(|| { thread::sleep(Duration::from_millis(4000)); std::process::exit(0); }); - client.main().unwrap() + client.main().expect("error during client main") }