Skip to content

Commit 6c0623f

Browse files
committed
main: eliminate all unwraps
1 parent 083d324 commit 6c0623f

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ serde_json = "1.0"
2424
futures = "0.3"
2525
serde = { version = "1.0", features = ["derive"] }
2626
oo7 = { version = "0.3", default-features = false, features = [ "tokio", "openssl_crypto" ] }
27-
xdg = "^2.1"
27+
xdg = "^2"
2828

2929
[profile.release]
3030
lto = true

src/main.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ impl AppState<'_> {
127127
let status = res.status();
128128
let json: POOCAPIResponse = res.json().await?;
129129
match status {
130-
reqwest::StatusCode::OK => Ok(json.secret.unwrap()),
130+
reqwest::StatusCode::OK => {
131+
json.secret.ok_or(anyhow!("Secret not found in JSON"))
132+
}
131133
_ => Err(anyhow!("Unhandled status code from Open Client API")),
132134
}
133135
}
134136
reqwest::StatusCode::OK => {
135137
assert!(json.status == 1);
136-
Ok(json.secret.unwrap())
138+
json.secret.ok_or(anyhow!("Secret not found in JSON"))
137139
}
138140
_ => {
139141
assert!(json.status == 0);
@@ -172,7 +174,7 @@ impl AppState<'_> {
172174
match status {
173175
reqwest::StatusCode::OK => {
174176
assert!(json.status == 1);
175-
Ok(json.id.unwrap())
177+
json.id.ok_or(anyhow!("ID not found in JSON"))
176178
}
177179
_ => {
178180
assert!(json.status == 0);
@@ -345,7 +347,7 @@ const WS_URL: &str = "wss://client.pushover.net/push";
345347
async fn display_message(state: &AppState<'_>, message: &POMessage) -> Result<()> {
346348
if message.priority < 0 {
347349
println!("{}: {}", message.title, message.message);
348-
return Ok(())
350+
return Ok(());
349351
}
350352

351353
Notification::new()
@@ -369,32 +371,30 @@ async fn inner_loop(state: &mut AppState<'_>) -> Result<()> {
369371
let (_write, mut read) = ws_stream.split();
370372

371373
loop {
372-
let message = timeout(std::time::Duration::from_secs(95), read.next())
373-
.await?
374-
.unwrap()?;
375-
let text = message.to_text()?;
376-
match text {
377-
"!" => {
378-
while let Some(m) = state.download_messages().await? {
379-
380-
for message in &m {
381-
display_message(state, message).await?;
374+
if let Some(Ok(message)) = timeout(std::time::Duration::from_secs(95), read.next()).await? {
375+
let text = message.to_text()?;
376+
match text {
377+
"!" => {
378+
while let Some(m) = state.download_messages().await? {
379+
for message in &m {
380+
display_message(state, message).await?;
381+
}
382+
state.delete_messages(&m).await?;
382383
}
383-
state.delete_messages(&m).await?;
384384
}
385+
"E" => {
386+
log::error!("Received an error from upstream, should reconnect");
387+
return Err(TsunaLoopError::Error().into());
388+
}
389+
"A" => {
390+
log::error!("Abort");
391+
return Err(TsunaLoopError::Abort().into());
392+
}
393+
"#" => {
394+
log::debug!("[{:?}], Keepalive", std::time::SystemTime::now());
395+
}
396+
_ => {}
385397
}
386-
"E" => {
387-
log::error!("Received an error from upstream, should reconnect");
388-
return Err(TsunaLoopError::Error().into());
389-
}
390-
"A" => {
391-
log::error!("Abort");
392-
return Err(TsunaLoopError::Abort().into());
393-
}
394-
"#" => {
395-
log::debug!("[{:?}], Keepalive", std::time::SystemTime::now());
396-
}
397-
_ => {}
398398
}
399399
}
400400
}
@@ -411,10 +411,12 @@ async fn run_loop(state: &mut AppState<'_>) -> Result<()> {
411411
state.increment_backoff();
412412
continue;
413413
}
414-
Err(e) if e.is::<TsunaLoopError>() => match e.downcast_ref::<TsunaLoopError>().unwrap()
415-
{
416-
TsunaLoopError::Abort() => return Err(e),
417-
TsunaLoopError::Error() => continue,
414+
Err(e) if e.is::<TsunaLoopError>() => match e.downcast_ref::<TsunaLoopError>() {
415+
Some(inner) => match inner {
416+
TsunaLoopError::Abort() => return Err(e),
417+
TsunaLoopError::Error() => continue,
418+
},
419+
None => return Err(e),
418420
},
419421
Ok(o) => return Ok(o),
420422
Err(e) => {
@@ -440,7 +442,7 @@ async fn main() -> Result<()> {
440442
.build()?,
441443
secrets: None,
442444
backoff_time: Duration::from_secs(10),
443-
xdg_dirs: xdg::BaseDirectories::with_prefix("tsuna").unwrap(),
445+
xdg_dirs: xdg::BaseDirectories::with_prefix("tsuna")?,
444446
};
445447

446448
if args.command == Commands::Register {

0 commit comments

Comments
 (0)