Skip to content

Commit c6b0be5

Browse files
Placate Clippy & small cleanups
1 parent 3aa36b2 commit c6b0be5

4 files changed

Lines changed: 69 additions & 79 deletions

File tree

Cargo.toml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
ashpd = { version = "0.12", default-features = false, features = ["tokio", "tracing"] }
8-
chrono = { version = "0.4.41", default-features = false, features = [
9-
"alloc",
10-
"clock",
7+
# App
8+
chrono = { version = "0.4", default-features = false, features = [
9+
"alloc",
10+
"clock",
1111
] }
12+
clap = { version = "4.5", features = ["derive"] }
1213
dirs = "6"
13-
tokio = { version = "1.47.1", default-features = false, features = ["macros"] }
14-
clap = { version = "4.5.46", features = ["derive"] }
15-
zbus = { version = "5", default-features = false }
14+
tokio = { version = "1", default-features = false, features = ["macros"] }
15+
16+
# Screenshots/D-Bus comm
17+
ashpd = { version = "0.12", default-features = false, features = [
18+
"tokio",
19+
"tracing",
20+
] }
21+
zbus = { version = "5", default-features = false, features = ["tokio"] }
1622

1723
# Logging
1824
tracing = "0.1"
1925
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
2026

2127
# Internationalization
2228
i18n-embed = { version = "0.16", features = [
23-
"fluent-system",
24-
"desktop-requester",
29+
"fluent-system",
30+
"desktop-requester",
2531
] }
2632
i18n-embed-fl = "0.10"
2733
rust-embed = "8.5"

i18n/en/cosmic_screenshot.ftl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
cosmic-screenshot = COSMIC Screenshot
22
33
screenshot-saved-to-clipboard = Screenshot saved to clipboard
4-
screenshot-saved-to = Screenshot saved to:
4+
screenshot-saved-to = Screenshot saved to:
5+
6+
screenshot-cancelled = Screenshot canceled
7+
screenshot-dbus-err = Error from D-Bus
8+
screenshot-failed = Screenshot failed
9+
screenshot-no-dir = Unable to write screenshot to directory: {$path}
10+
screenshot-not-allowed = Screenshot not allowed: {$msg}

src/error.rs

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use std::{
22
error::Error as StdError,
33
fmt::{self, Display},
44
io,
5-
path::PathBuf,
5+
path::{Path, PathBuf},
66
};
77

8-
use ashpd::{desktop::ResponseError, Error as AshpdError, PortalError};
8+
use ashpd::{Error as AshpdError, PortalError, desktop::ResponseError};
99
use zbus::Error as ZbusError;
1010

11+
use crate::fl;
12+
1113
/// Error type for requesting screenshots from the XDG portal.
1214
///
1315
/// The primary purpose of this type is to provide simple user facing messages.
@@ -54,27 +56,20 @@ impl Error {
5456
/// Localized, condensed error message for end users
5557
pub fn to_user_facing(&self) -> String {
5658
match self {
57-
_ if self.unsupported() => "Portal does not support screenshots".into(),
58-
_ if self.cancelled() => "Screenshot cancelled".into(),
59-
_ if self.zbus() => "Problem communicating with D-Bus".into(),
60-
Self::MissingSaveDirectory(p) => p
61-
.as_deref()
62-
.map(|path| {
63-
format!(
64-
"Unable to save screenshot to {} or the Pictures directory",
65-
path.display()
66-
)
67-
})
68-
.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(),
77-
_ => "Failed to take screenshot".into(),
59+
// _ if self.unsupported() => fl!("screenshot-unsupported"),
60+
_ if self.cancelled() => fl!("screenshot-cancelled"),
61+
_ if self.zbus() => fl!("screenshot-dbus-err"),
62+
Self::MissingSaveDirectory(p) => {
63+
fl!(
64+
"screenshot-no-dir",
65+
path = p.as_deref().unwrap_or(Path::new("")).to_string_lossy()
66+
)
67+
}
68+
Self::Ashpd(AshpdError::Portal(PortalError::NotAllowed(msg))) => {
69+
fl!("screenshot-not-allowed", msg = msg)
70+
}
71+
// Self::SaveScreenshot { .. } => "Screenshot captured but couldn't be saved".into(),
72+
_ => fl!("screenshot-failed"),
7873
}
7974
}
8075

@@ -86,13 +81,7 @@ impl Error {
8681

8782
match e {
8883
AshpdError::Response(e) => *e == ResponseError::Cancelled,
89-
AshpdError::Portal(e) => {
90-
if let PortalError::Cancelled(_) = e {
91-
true
92-
} else {
93-
false
94-
}
95-
}
84+
AshpdError::Portal(PortalError::Cancelled(_)) => true,
9685
_ => false,
9786
}
9887
}
@@ -122,22 +111,11 @@ impl Error {
122111
/// [zbus::Error] encapsulates many different problems, many of which are programmer errors
123112
/// which shouldn't occur during normal operation.
124113
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-
}
114+
matches!(
115+
self,
116+
Self::Ashpd(AshpdError::Zbus(_))
117+
| Self::Ashpd(AshpdError::Portal(PortalError::ZBus(_)))
118+
)
141119
}
142120
}
143121

src/main.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ mod localize;
33

44
use std::{collections::HashMap, fs, os::unix::fs::MetadataExt, path::PathBuf};
55

6-
use ashpd::desktop::screenshot::{Screenshot, ScreenshotRequest};
6+
use ashpd::desktop::screenshot::Screenshot;
77
use clap::{ArgAction, Parser, command};
8-
use tracing::{error, info};
8+
use tracing::{debug, error, info};
99
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
10-
use zbus::{
11-
Connection, dbus_proxy,
12-
export::futures_util::{TryFutureExt, future::FutureExt},
13-
zvariant::Value,
14-
};
10+
use zbus::{Connection, proxy, zvariant::Value};
1511

1612
use error::Error;
1713

@@ -65,6 +61,7 @@ trait Notifications {
6561
}
6662

6763
// Send a notification for the screenshot app.
64+
#[tracing::instrument(name = "Posting notification")]
6865
async fn send_notify(summary: &str, body: &str) -> Result<(), Error> {
6966
let connection = Connection::session().await.map_err(Error::Notify)?;
7067

@@ -87,6 +84,7 @@ async fn send_notify(summary: &str, body: &str) -> Result<(), Error> {
8784
.map(|_| ())
8885
}
8986

87+
#[tracing::instrument(name = "Requesting screenshot from D-Bus")]
9088
async fn request_screenshot(args: Args) -> Result<String, Error> {
9189
let picture_dir = (!args.interactive)
9290
.then(|| {
@@ -106,6 +104,7 @@ async fn request_screenshot(args: Args) -> Result<String, Error> {
106104
.response()?;
107105

108106
let uri = response.uri();
107+
debug!("Screenshot request URI: {uri}");
109108
match uri.scheme() {
110109
"file" => {
111110
let response_path = uri
@@ -157,7 +156,7 @@ async fn request_screenshot(args: Args) -> Result<String, Error> {
157156
}
158157

159158
#[tokio::main(flavor = "current_thread")]
160-
async fn main() -> Result<(), Error> {
159+
async fn main() {
161160
// Init tracing but don't panic if it fails
162161
let _ = tracing_subscriber::registry()
163162
.with(fmt::layer())
@@ -168,26 +167,27 @@ async fn main() -> Result<(), Error> {
168167
let args = Args::parse();
169168
let notify = args.notify;
170169

171-
let path = match request_screenshot(args).await {
170+
let (summary, body) = match request_screenshot(args).await {
172171
Ok(path) => {
173-
info!("Saving screenshot to {path}");
174-
path
172+
info!("Screenshot saved to {path}");
173+
if path.is_empty() {
174+
(fl!("screenshot-saved-to-clipboard"), "".into())
175+
} else {
176+
(fl!("screenshot-saved-to"), path)
177+
}
175178
}
176179
Err(e) => {
177-
error!("Screenshot failed with {e}");
178-
e.to_user_facing()
180+
if !e.cancelled() {
181+
error!("Screenshot failed with {e}");
182+
(fl!("screenshot-failed"), e.to_user_facing())
183+
} else {
184+
info!("Screenshot cancelled");
185+
(fl!("screenshot-cancelled"), "".into())
186+
}
179187
}
180188
};
181189

182-
if notify {
183-
let message = if path.is_empty() {
184-
fl!("screenshot-saved-to-clipboard")
185-
} else {
186-
fl!("screenshot-saved-to")
187-
};
188-
189-
send_notify(&path, &message).await?;
190+
if notify && let Err(e) = send_notify(&summary, &body).await {
191+
error!("Failed to post notification on completion: {e}");
190192
}
191-
192-
Ok(())
193193
}

0 commit comments

Comments
 (0)