From 3c2fbe17cc3e8b8a6199e2ad62c23fbbb18487ff Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 28 Nov 2017 23:34:07 -0800 Subject: DRY in geniza-net --- src/bin/geniza-net.rs | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/bin/geniza-net.rs b/src/bin/geniza-net.rs index 853b497..38dc507 100644 --- a/src/bin/geniza-net.rs +++ b/src/bin/geniza-net.rs @@ -9,7 +9,24 @@ extern crate geniza; // TODO: more careful import use geniza::*; -use clap::{App, SubCommand}; +use std::path::Path; +use clap::{App, SubCommand, Arg}; + +fn parse_dat_key(raw_key: &str) -> Result> { + + if raw_key.len() != 32 * 2 { + bail!("dat key not correct length"); + } + let mut key_bytes = vec![]; + for i in 0..32 { + let r = u8::from_str_radix(&raw_key[2 * i..2 * i + 2], 16); + match r { + Ok(b) => key_bytes.push(b), + Err(e) => bail!("Problem with hex: {}", e), + }; + } + Ok(key_bytes) +} fn run() -> Result<()> { env_logger::init().unwrap(); @@ -65,14 +82,7 @@ fn run() -> Result<()> { if dat_key.len() != 32 * 2 { bail!("dat key not correct length"); } - let mut key_bytes = vec![]; - for i in 0..32 { - let r = u8::from_str_radix(&dat_key[2 * i..2 * i + 2], 16); - match r { - Ok(b) => key_bytes.push(b), - Err(e) => bail!("Problem with hex: {}", e), - }; - } + let key_bytes = parse_dat_key(&dat_key)?; let mut dc = DatConnection::connect(host_port, &key_bytes, false)?; // XXX: number here totally arbitrary dc.receive_all(false, 10)?; @@ -84,14 +94,7 @@ fn run() -> Result<()> { if dat_key.len() != 32 * 2 { bail!("dat key not correct length"); } - let mut key_bytes = vec![]; - for i in 0..32 { - let r = u8::from_str_radix(&dat_key[2 * i..2 * i + 2], 16); - match r { - Ok(b) => key_bytes.push(b), - Err(e) => bail!("Problem with hex: {}", e), - }; - } + let key_bytes = parse_dat_key(&dat_key)?; let disc_key = make_discovery_key(&key_bytes); for b in disc_key { print!("{:02x}", b); @@ -103,14 +106,7 @@ fn run() -> Result<()> { if dat_key.len() != 32 * 2 { bail!("dat key not correct length"); } - let mut key_bytes = vec![]; - for i in 0..32 { - let r = u8::from_str_radix(&dat_key[2 * i..2 * i + 2], 16); - match r { - Ok(b) => key_bytes.push(b), - Err(e) => bail!("Problem with hex: {}", e), - }; - } + let key_bytes = parse_dat_key(&dat_key)?; let disc_key = make_discovery_key(&key_bytes); for b in 0..20 { print!("{:02x}", disc_key[b]); -- cgit v1.2.3