examples: Remove all unwrap()s.

This commit is contained in:
Emmanuel Gil Peyrot 2019-12-01 17:00:50 +01:00
parent 0d58b56920
commit 0d01918713

View file

@ -12,7 +12,7 @@ use xmpp_parsers::{
disco::DiscoInfoResult,
ecaps2::{compute_disco as compute_disco_ecaps2, hash_ecaps2, ECaps2},
hashes::Algo,
Element,
Element, Error,
};
fn get_caps(disco: &DiscoInfoResult, node: String) -> Result<Caps, String> {
@ -21,18 +21,18 @@ fn get_caps(disco: &DiscoInfoResult, node: String) -> Result<Caps, String> {
Ok(Caps::new(node, caps_hash))
}
fn get_ecaps2(disco: &DiscoInfoResult) -> Result<ECaps2, String> {
let ecaps2_data = compute_disco_ecaps2(&disco).unwrap();
fn get_ecaps2(disco: &DiscoInfoResult) -> Result<ECaps2, Error> {
let ecaps2_data = compute_disco_ecaps2(&disco)?;
let ecaps2_sha256 = hash_ecaps2(&ecaps2_data, Algo::Sha_256)?;
let ecaps2_sha3_256 = hash_ecaps2(&ecaps2_data, Algo::Sha3_256)?;
Ok(ECaps2::new(vec![ecaps2_sha256, ecaps2_sha3_256]))
}
fn main() -> Result<(), ()> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<_> = env::args().collect();
if args.len() != 2 {
println!("Usage: {} <node>", args[0]);
return Err(());
std::process::exit(1);
}
let node = args[1].clone();
@ -42,15 +42,15 @@ fn main() -> Result<(), ()> {
let stdin = io::stdin();
let mut data = String::new();
let mut handle = stdin.lock();
handle.read_to_string(&mut data).unwrap();
handle.read_to_string(&mut data)?;
// Parse the payload into a DiscoInfoResult.
let elem: Element = data.parse().unwrap();
let disco = DiscoInfoResult::try_from(elem).unwrap();
let elem: Element = data.parse()?;
let disco = DiscoInfoResult::try_from(elem)?;
// Compute both kinds of caps.
let caps = get_caps(&disco, node).unwrap();
let ecaps2 = get_ecaps2(&disco).unwrap();
let caps = get_caps(&disco, node)?;
let ecaps2 = get_ecaps2(&disco)?;
// Print them.
let caps_elem = Element::from(caps);