Skip to content

Commit b2a2ef2

Browse files
committedMar 10, 2024··
lints and fmt
1 parent 1c23aac commit b2a2ef2

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed
 

‎src/cli.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,45 @@ pub enum App {
2222
}
2323

2424
impl App {
25-
pub async fn run(&self) -> AResult<()> {
25+
pub async fn run(self) -> AResult<()> {
2626
debug!("{self:?}");
2727
match self {
28-
App::Send {
29-
magic_string,
28+
Self::Send {
29+
magic_string: magic_string_opt,
3030
file_path,
3131
} => {
32-
let magic_string = magic_string
33-
.to_owned()
34-
.unwrap_or_else(generate_magic_string);
32+
let magic_string = match magic_string_opt {
33+
Some(s) => s,
34+
None => generate_magic_string()?,
35+
};
3536

3637
println!("MAGIC: {magic_string}");
3738

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

4041
let (tx, rx) = oneshot::channel();
4142

42-
let file_size = metadata(file_path)?.len();
43+
let file_size = metadata(&file_path)?.len();
4344

44-
let port = send_file(file_path, file_size, tx).await?;
45+
let port = send_file(&file_path, file_size, tx).await?;
4546

4647
send_msg(
4748
&magic_string,
4849
port,
4950
[
50-
("name".into(), file_path.into()),
51+
("name".into(), file_path),
5152
("size".into(), file_size.to_string()),
5253
]
5354
.into(),
5455
)?;
5556

5657
rx.await?;
5758
}
58-
App::Recv {
59+
Self::Recv {
5960
magic_string,
6061
save_dir,
6162
} => {
62-
let (addrs, port, data) = recv_msg(magic_string)?;
63+
let (addrs, port, data) = recv_msg(&magic_string)?;
6364
let name = Path::new(
6465
data.get_property_val_str("name")
6566
.context("`name` key must be present")?,

‎src/lib.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use std::collections::{HashMap, HashSet};
55
use std::net::IpAddr;
66
use std::path::PathBuf;
77

8-
use anyhow::anyhow;
9-
pub use anyhow::Result as AResult;
8+
use anyhow::{anyhow, Context, Result as AResult};
109
use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo, TxtProperties};
1110
use names::Generator;
1211
use tokio::fs::File;
@@ -18,9 +17,9 @@ use crate::utils::get_progressbar;
1817

1918
const SERVICE_TYPE: &str = "_rope._tcp.local.";
2019

21-
fn generate_magic_string() -> String {
20+
fn generate_magic_string() -> AResult<String> {
2221
let mut generator = Generator::default();
23-
generator.next().unwrap()
22+
generator.next().context("Failed to generate magic string")
2423
}
2524

2625
fn send_msg(magic_string: &str, port: u16, data: HashMap<String, String>) -> AResult<()> {
@@ -83,7 +82,7 @@ async fn send_file(file_path: &str, size: u64, tx: oneshot::Sender<()>) -> AResu
8382

8483
debug!("Peer is connected. Sending file: {file_path_owned}");
8584

86-
let pb = get_progressbar(size);
85+
let pb = get_progressbar(size)?;
8786

8887
let f = File::open(file_path_owned).await?;
8988

@@ -92,7 +91,7 @@ async fn send_file(file_path: &str, size: u64, tx: oneshot::Sender<()>) -> AResu
9291
debug!("Done. Sending signal via channel");
9392

9493
tx.send(())
95-
.map_err(|_| anyhow!("Couldn't sent signal via channel"))
94+
.map_err(|()| anyhow!("Couldn't sent signal via channel"))
9695
});
9796

9897
Ok(addr.port())
@@ -104,7 +103,7 @@ async fn recv_file(ip: &IpAddr, port: u16, path: &PathBuf, size: u64) -> AResult
104103

105104
debug!("Peer is connected. Receiving file: {path:?}");
106105

107-
let pb = get_progressbar(size);
106+
let pb = get_progressbar(size)?;
108107

109108
let f = File::create(path).await?;
110109

‎src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use anyhow::Result as AResult;
12
use clap::Parser;
23
use rope::cli::App;
3-
use rope::AResult;
44

55
#[tokio::main]
66
async fn main() -> AResult<()> {

‎src/utils.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
use std::fmt::Write;
1+
use core::fmt::Write;
22

3-
use if_addrs::IfAddr;
3+
use anyhow::Context;
4+
use if_addrs::{IfAddr, Interface};
45
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
56

7+
use crate::AResult;
8+
69
pub fn my_ext_interfaces() -> Vec<IfAddr> {
710
if_addrs::get_if_addrs()
811
.unwrap_or_default()
912
.into_iter()
10-
.filter(|i| i.is_loopback())
13+
.filter(Interface::is_loopback)
1114
.map(|i| i.addr)
1215
.collect()
1316
}
1417

15-
pub fn get_progressbar(size: u64) -> ProgressBar {
18+
pub fn get_progressbar(size: u64) -> AResult<ProgressBar> {
1619
let pb = ProgressBar::new(size);
1720
pb.set_style(ProgressStyle::with_template("{spinner:.yellow} [{elapsed_precise}] [{wide_bar:.green/red}] {bytes}/{total_bytes} ({eta})")
18-
.unwrap()
21+
.context("Failed to set progress bar style")?
1922
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
2023
.progress_chars("█▓▒░─"));
21-
22-
pb
24+
Ok(pb)
2325
}

0 commit comments

Comments
 (0)
Please sign in to comment.