Skip to content

Commit

Permalink
lints and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Mar 10, 2024
1 parent 1c23aac commit b2a2ef2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
23 changes: 12 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,45 @@ 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}");

info!("MAGIC: {magic_string}");

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(),
)?;

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")?,
Expand Down
13 changes: 6 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> {
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<String, String>) -> AResult<()> {
Expand Down Expand Up @@ -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?;

Expand All @@ -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())
Expand All @@ -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?;

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand Down
16 changes: 9 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<IfAddr> {
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<ProgressBar> {
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)
}

0 comments on commit b2a2ef2

Please sign in to comment.