Skip to content

Commit 0fa560f

Browse files
committed
Reduce amount of blocking in blocks
1 parent 005233a commit 0fa560f

File tree

7 files changed

+40
-13
lines changed

7 files changed

+40
-13
lines changed

src/blocks/amd_gpu.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
6363
};
6464

6565
let device = match &config.device {
66-
Some(name) => Device::new(name)?,
66+
Some(name) => Device::new(name).await?,
6767
None => Device::default_card()
6868
.await
6969
.error("failed to get default GPU")?
@@ -121,10 +121,10 @@ struct GpuInfo {
121121
}
122122

123123
impl Device {
124-
fn new(name: &str) -> Result<Self, Error> {
124+
async fn new(name: &str) -> Result<Self, Error> {
125125
let path = PathBuf::from(format!("/sys/class/drm/{name}/device"));
126126

127-
if !path.exists() {
127+
if !file_exists(&path).await {
128128
Err(Error::new(format!("Device {name} not found")))
129129
} else {
130130
Ok(Self { path })
@@ -185,9 +185,9 @@ impl Device {
185185
mod tests {
186186
use super::*;
187187

188-
#[test]
189-
fn test_non_existing_gpu_device() {
190-
let device = Device::new("/nope");
188+
#[tokio::test]
189+
async fn test_non_existing_gpu_device() {
190+
let device = Device::new("/nope").await;
191191
assert!(device.is_err());
192192
}
193193
}

src/blocks/calendar.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
295295
let warning_threshold = Duration::try_seconds(config.warning_threshold.into())
296296
.error("Invalid warning threshold configuration")?;
297297

298-
let mut source = Source::new(source_config.clone()).await?;
298+
let mut source = Source::new(source_config.to_owned()).await?;
299299

300300
let mut timer = config.fetch_interval.timer();
301301

@@ -424,7 +424,8 @@ impl Source {
424424
credentials_path,
425425
}) => {
426426
let credentials = if let Some(path) = credentials_path {
427-
util::deserialize_toml_file(path.expand()?.to_string())
427+
util::async_deserialize_toml_file(path.expand()?.to_string())
428+
.await
428429
.error("Failed to read basic credentials file")?
429430
} else {
430431
credentials.clone()
@@ -440,7 +441,8 @@ impl Source {
440441
}
441442
AuthConfig::OAuth2(oauth2) => {
442443
let credentials = if let Some(path) = &oauth2.credentials_path {
443-
util::deserialize_toml_file(path.expand()?.to_string())
444+
util::async_deserialize_toml_file(path.expand()?.to_string())
445+
.await
444446
.error("Failed to read oauth2 credentials file")?
445447
} else {
446448
oauth2.credentials.clone()

src/blocks/packages/apt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Apt {
5050
async fn setup(&mut self) -> Result<()> {
5151
let mut cache_dir = env::temp_dir();
5252
cache_dir.push("i3rs-apt");
53-
if !cache_dir.exists() {
53+
if !file_exists(&cache_dir).await {
5454
create_dir_all(&cache_dir)
5555
.await
5656
.error("Failed to create temp dir")?;

src/blocks/packages/pacman.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Backend for Pacman {
7272

7373
// Create symlink to local cache in `checkup-db` if required
7474
let local_cache = PACMAN_UPDATES_DB.join("local");
75-
if !local_cache.exists() {
75+
if !file_exists(&local_cache).await {
7676
symlink(PACMAN_DB.join("local"), local_cache)
7777
.await
7878
.error("Failed to created required symlink")?;

src/blocks/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub use super::{BlockAction, CommonApi};
33
pub use crate::click::MouseButton;
44
pub use crate::errors::*;
55
pub use crate::formatting::{config::Config as FormatConfig, value::Value, Values};
6-
pub use crate::util::{default, new_dbus_connection, new_system_dbus_connection};
6+
pub use crate::util::{default, file_exists, new_dbus_connection, new_system_dbus_connection};
77
pub use crate::widget::{State, Widget};
88
pub use crate::wrappers::{Seconds, ShellString};
99
pub(crate) use crate::REQWEST_CLIENT;

src/netlink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl NetDevice {
7777
let path = Path::new("/sys/class/net").join(&iface.name);
7878
let tun = iface.name.starts_with("tun")
7979
|| iface.name.starts_with("tap")
80-
|| path.join("tun_flags").exists();
80+
|| util::file_exists(path.join("tun_flags")).await;
8181
let (wg, ppp) = util::read_file(path.join("uevent"))
8282
.await
8383
.map_or((false, false), |c| {

src/util.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ use tokio::process::Command;
77

88
use crate::errors::*;
99

10+
pub async fn file_exists(path: impl AsRef<Path>) -> bool {
11+
tokio::fs::metadata(path).await.is_ok()
12+
}
13+
1014
/// Tries to find a file in standard locations:
1115
/// - Fist try to find a file by full path (only if path is absolute)
1216
/// - Then try XDG_CONFIG_HOME (e.g. `~/.config`)
@@ -95,6 +99,27 @@ where
9599
let contents = std::fs::read_to_string(path)
96100
.or_error(|| format!("Failed to read file: {}", path.display()))?;
97101

102+
deserialize_toml_file_string(contents, path)
103+
}
104+
105+
pub async fn async_deserialize_toml_file<T, P>(path: P) -> Result<T>
106+
where
107+
T: DeserializeOwned,
108+
P: AsRef<Path>,
109+
{
110+
let path = path.as_ref();
111+
112+
let contents = read_file(path)
113+
.await
114+
.or_error(|| format!("Failed to read file: {}", path.display()))?;
115+
116+
deserialize_toml_file_string(contents, path)
117+
}
118+
119+
fn deserialize_toml_file_string<T>(contents: String, path: &Path) -> Result<T>
120+
where
121+
T: DeserializeOwned,
122+
{
98123
toml::from_str(&contents).map_err(|err| {
99124
let location_msg = err
100125
.span()

0 commit comments

Comments
 (0)