Skip to content

Commit 544b1da

Browse files
Placate Clippy & small cleanups
1 parent ec7d80d commit 544b1da

3 files changed

Lines changed: 39 additions & 53 deletions

File tree

Cargo.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ name = "cosmic-screenshot"
33
version = "0.1.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
86
[dependencies]
9-
ashpd = { version = "0.6.8", features = ["tracing"] }
7+
# App
108
chrono = "0.4.24"
9+
clap = { version = "4.4.16", features = ["derive"] }
1110
dirs = "5.0.1"
1211
tokio = { version = "1.28.1", features = ["full"] }
13-
clap = { version = "4.4.16", features = ["derive"] }
14-
zbus = { version = "3", default-features = false, features = ["tokio"] }
12+
13+
# Logging
1514
tracing = "0.1"
1615
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
16+
17+
# Screenshots/D-Bus comm
18+
ashpd = { version = "0.6.8", features = ["tracing"] }
19+
zbus = { version = "3", default-features = false, features = ["tokio"] }

src/error.rs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,10 @@ impl Error {
6666
)
6767
})
6868
.unwrap_or_else(|| "Unable to save screenshot to the Pictures directory".into()),
69-
Self::Ashpd(e) => match e {
70-
AshpdError::Portal(e) => match e {
71-
PortalError::NotAllowed(msg) => format!("Screenshot not allowed: {msg}"),
72-
_ => "Failed to take screenshot".into(),
73-
},
74-
_ => "Failed to take screenshot".into(),
75-
},
76-
Self::SaveScreenshot { .. } => "Screenshot succeeded but couldn't be saved".into(),
69+
Self::Ashpd(AshpdError::Portal(PortalError::NotAllowed(msg))) => {
70+
format!("Screenshot not allowed: {msg}")
71+
}
72+
Self::SaveScreenshot { .. } => "Screenshot captured but couldn't be saved".into(),
7773
_ => "Failed to take screenshot".into(),
7874
}
7975
}
@@ -86,13 +82,7 @@ impl Error {
8682

8783
match e {
8884
AshpdError::Response(e) => *e == ResponseError::Cancelled,
89-
AshpdError::Portal(e) => {
90-
if let PortalError::Cancelled(_) = e {
91-
true
92-
} else {
93-
false
94-
}
95-
}
85+
AshpdError::Portal(PortalError::Cancelled(_)) => true,
9686
_ => false,
9787
}
9888
}
@@ -122,22 +112,11 @@ impl Error {
122112
/// [zbus::Error] encapsulates many different problems, many of which are programmer errors
123113
/// which shouldn't occur during normal operation.
124114
pub fn zbus(&self) -> bool {
125-
if let Self::Ashpd(e) = self {
126-
match e {
127-
AshpdError::Zbus(_) => true,
128-
AshpdError::Portal(PortalError::ZBus(_)) => {
129-
// if let PortalError::ZBus(_) = e {
130-
// true
131-
// } else {
132-
// false
133-
// }
134-
true
135-
}
136-
_ => false,
137-
}
138-
} else {
139-
false
140-
}
115+
matches!(
116+
self,
117+
Self::Ashpd(AshpdError::Zbus(_))
118+
| Self::Ashpd(AshpdError::Portal(PortalError::ZBus(_)))
119+
)
141120
}
142121
}
143122

src/main.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
#![allow(clippy::too_many_arguments)]
2+
13
mod error;
24

3-
use ashpd::desktop::screenshot::{Screenshot, ScreenshotRequest};
5+
use ashpd::desktop::screenshot::Screenshot;
46
use clap::{command, ArgAction, Parser};
57
use std::{collections::HashMap, fs, os::unix::fs::MetadataExt, path::PathBuf};
6-
use tracing::{error, info};
8+
use tracing::{debug, error, info};
79
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
8-
use zbus::{
9-
dbus_proxy,
10-
export::futures_util::{future::FutureExt, TryFutureExt},
11-
zvariant::Value,
12-
Connection,
13-
};
10+
use zbus::{dbus_proxy, zvariant::Value, Connection};
1411

1512
use error::Error;
1613

@@ -85,6 +82,7 @@ async fn send_notify(summary: &str, body: &str) -> Result<(), Error> {
8582
.map(|_| ())
8683
}
8784

85+
#[tracing::instrument]
8886
async fn request_screenshot(args: Args) -> Result<String, Error> {
8987
let picture_dir = (!args.interactive)
9088
.then(|| {
@@ -104,6 +102,7 @@ async fn request_screenshot(args: Args) -> Result<String, Error> {
104102
.response()?;
105103

106104
let uri = response.uri();
105+
debug!("Screenshot request URI: {uri}");
107106
match uri.scheme() {
108107
"file" => {
109108
if let Some(picture_dir) = picture_dir {
@@ -153,7 +152,7 @@ async fn request_screenshot(args: Args) -> Result<String, Error> {
153152
}
154153

155154
#[tokio::main(flavor = "current_thread")]
156-
async fn main() -> Result<(), Error> {
155+
async fn main() {
157156
// Init tracing but don't panic if it fails
158157
let _ = tracing_subscriber::registry()
159158
.with(fmt::layer())
@@ -163,20 +162,25 @@ async fn main() -> Result<(), Error> {
163162
let args = Args::parse();
164163
let notify = args.notify;
165164

166-
let result = match request_screenshot(args).await {
165+
let (summary, body) = match request_screenshot(args).await {
167166
Ok(path) => {
168-
info!("Saving screenshot to {path}");
169-
path
167+
info!("Screenshot saved to {path}");
168+
("Screenshot captured", path)
170169
}
171170
Err(e) => {
172-
error!("Screenshot failed with {e}");
173-
e.to_user_facing()
171+
if !e.cancelled() {
172+
error!("Screenshot failed with {e}");
173+
("Screenshot failed", e.to_user_facing())
174+
} else {
175+
info!("Screenshot cancelled");
176+
("Screenshot cancelled", "".into())
177+
}
174178
}
175179
};
176180

177181
if notify {
178-
send_notify(&result, "").await?;
182+
if let Err(e) = send_notify(summary, &body).await {
183+
error!("Failed to post notification on completion: {e}");
184+
}
179185
}
180-
181-
Ok(())
182186
}

0 commit comments

Comments
 (0)