Skip to content

Commit d0fac8d

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

File tree

4 files changed

+68
-78
lines changed

4 files changed

+68
-78
lines changed

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
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-failed = Screenshot failed
8+
screenshot-no-dir = Unable to write screenshot to directory: {$path}
9+
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 & 22 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

@@ -87,6 +83,7 @@ async fn send_notify(summary: &str, body: &str) -> Result<(), Error> {
8783
.map(|_| ())
8884
}
8985

86+
#[tracing::instrument]
9087
async fn request_screenshot(args: Args) -> Result<String, Error> {
9188
let picture_dir = (!args.interactive)
9289
.then(|| {
@@ -106,6 +103,7 @@ async fn request_screenshot(args: Args) -> Result<String, Error> {
106103
.response()?;
107104

108105
let uri = response.uri();
106+
debug!("Screenshot request URI: {uri}");
109107
match uri.scheme() {
110108
"file" => {
111109
let response_path = uri
@@ -157,7 +155,7 @@ async fn request_screenshot(args: Args) -> Result<String, Error> {
157155
}
158156

159157
#[tokio::main(flavor = "current_thread")]
160-
async fn main() -> Result<(), Error> {
158+
async fn main() {
161159
// Init tracing but don't panic if it fails
162160
let _ = tracing_subscriber::registry()
163161
.with(fmt::layer())
@@ -168,26 +166,29 @@ async fn main() -> Result<(), Error> {
168166
let args = Args::parse();
169167
let notify = args.notify;
170168

171-
let path = match request_screenshot(args).await {
169+
let (summary, body) = match request_screenshot(args).await {
172170
Ok(path) => {
173-
info!("Saving screenshot to {path}");
174-
path
171+
info!("Screenshot saved to {path}");
172+
if path.is_empty() {
173+
(fl!("screenshot-saved-to-clipboard"), "".into())
174+
} else {
175+
(fl!("screenshot-saved-to"), path)
176+
}
175177
}
176178
Err(e) => {
177-
error!("Screenshot failed with {e}");
178-
e.to_user_facing()
179+
if !e.cancelled() {
180+
error!("Screenshot failed with {e}");
181+
(fl!("screenshot-failed"), e.to_user_facing())
182+
} else {
183+
info!("Screenshot cancelled");
184+
(fl!("screenshot-cancelled"), "".into())
185+
}
179186
}
180187
};
181188

182189
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 let Err(e) = send_notify(&summary, &body).await {
191+
error!("Failed to post notification on completion: {e}");
192+
}
190193
}
191-
192-
Ok(())
193194
}

0 commit comments

Comments
 (0)