diff --git a/src/cli.rs b/src/cli.rs index a905053..4284d0c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -22,16 +22,17 @@ pub enum App { } impl App { - pub async fn run(&self) -> AResult<()> { + pub async fn run(self) -> AResult<()> { debug!("{self:?}"); match self { - App::Send { - magic_string, + Self::Send { + magic_string: magic_string_opt, file_path, } => { - let magic_string = magic_string - .to_owned() - .unwrap_or_else(generate_magic_string); + let magic_string = match magic_string_opt { + Some(s) => s, + None => generate_magic_string()?, + }; println!("MAGIC: {magic_string}"); @@ -39,15 +40,15 @@ impl App { let (tx, rx) = oneshot::channel(); - let file_size = metadata(file_path)?.len(); + let file_size = metadata(&file_path)?.len(); - let port = send_file(file_path, file_size, tx).await?; + let port = send_file(&file_path, file_size, tx).await?; send_msg( &magic_string, port, [ - ("name".into(), file_path.into()), + ("name".into(), file_path), ("size".into(), file_size.to_string()), ] .into(), @@ -55,11 +56,11 @@ impl App { rx.await?; } - App::Recv { + Self::Recv { magic_string, save_dir, } => { - let (addrs, port, data) = recv_msg(magic_string)?; + let (addrs, port, data) = recv_msg(&magic_string)?; let name = Path::new( data.get_property_val_str("name") .context("`name` key must be present")?, diff --git a/src/lib.rs b/src/lib.rs index 894e8aa..07e4b01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,7 @@ use std::collections::{HashMap, HashSet}; use std::net::IpAddr; use std::path::PathBuf; -use anyhow::anyhow; -pub use anyhow::Result as AResult; +use anyhow::{anyhow, Context, Result as AResult}; use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo, TxtProperties}; use names::Generator; use tokio::fs::File; @@ -18,9 +17,9 @@ use crate::utils::get_progressbar; const SERVICE_TYPE: &str = "_rope._tcp.local."; -fn generate_magic_string() -> String { +fn generate_magic_string() -> AResult { let mut generator = Generator::default(); - generator.next().unwrap() + generator.next().context("Failed to generate magic string") } fn send_msg(magic_string: &str, port: u16, data: HashMap) -> AResult<()> { @@ -83,7 +82,7 @@ async fn send_file(file_path: &str, size: u64, tx: oneshot::Sender<()>) -> AResu debug!("Peer is connected. Sending file: {file_path_owned}"); - let pb = get_progressbar(size); + let pb = get_progressbar(size)?; let f = File::open(file_path_owned).await?; @@ -92,7 +91,7 @@ async fn send_file(file_path: &str, size: u64, tx: oneshot::Sender<()>) -> AResu debug!("Done. Sending signal via channel"); tx.send(()) - .map_err(|_| anyhow!("Couldn't sent signal via channel")) + .map_err(|()| anyhow!("Couldn't sent signal via channel")) }); Ok(addr.port()) @@ -104,7 +103,7 @@ async fn recv_file(ip: &IpAddr, port: u16, path: &PathBuf, size: u64) -> AResult debug!("Peer is connected. Receiving file: {path:?}"); - let pb = get_progressbar(size); + let pb = get_progressbar(size)?; let f = File::create(path).await?; diff --git a/src/main.rs b/src/main.rs index 518a3a5..5014054 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ +use anyhow::Result as AResult; use clap::Parser; use rope::cli::App; -use rope::AResult; #[tokio::main] async fn main() -> AResult<()> { diff --git a/src/utils.rs b/src/utils.rs index b95e1a8..704e3c7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,23 +1,25 @@ -use std::fmt::Write; +use core::fmt::Write; -use if_addrs::IfAddr; +use anyhow::Context; +use if_addrs::{IfAddr, Interface}; use indicatif::{ProgressBar, ProgressState, ProgressStyle}; +use crate::AResult; + pub fn my_ext_interfaces() -> Vec { if_addrs::get_if_addrs() .unwrap_or_default() .into_iter() - .filter(|i| i.is_loopback()) + .filter(Interface::is_loopback) .map(|i| i.addr) .collect() } -pub fn get_progressbar(size: u64) -> ProgressBar { +pub fn get_progressbar(size: u64) -> AResult { let pb = ProgressBar::new(size); pb.set_style(ProgressStyle::with_template("{spinner:.yellow} [{elapsed_precise}] [{wide_bar:.green/red}] {bytes}/{total_bytes} ({eta})") - .unwrap() + .context("Failed to set progress bar style")? .with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap()) .progress_chars("█▓▒░─")); - - pb + Ok(pb) }