Skip to content

Commit d760064

Browse files
authored
Merge pull request #24 from HigherOrderLogic/main
Replace std fs operation with async one
2 parents 4a28db4 + e590641 commit d760064

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description = "Language Server Protocol implementation for Cargo.toml manifests.
77
license = "MIT"
88

99
[dependencies]
10-
tokio = { version = "1.44.2", features = ["rt", "macros", "io-std"] }
10+
tokio = { version = "1.44.2", features = ["rt", "macros", "io-std", "fs"] }
1111
tower-lsp = "0.20.0"
1212
async-trait = "0.1"
1313

src/crates/cache.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{collections::HashMap, path::PathBuf, sync::Arc};
33
use semver::Version;
44
use serde::{Deserialize, Serialize};
55
use time::OffsetDateTime;
6-
use tokio::sync::RwLock;
6+
use tokio::{fs, sync::RwLock};
77

88
#[derive(Debug, Clone, Serialize, Deserialize)]
99
struct Fetch {
@@ -47,12 +47,13 @@ impl CrateCache {
4747
cache
4848
}
4949
pub async fn change_directory(&self, directory: PathBuf) {
50-
std::fs::create_dir_all(&directory).unwrap_or_else(|_| {
50+
fs::create_dir_all(&directory).await.unwrap_or_else(|_| {
5151
panic!("Failed to create crates-lsp cache directory: {directory:?}")
5252
});
5353

5454
// This directory might be within a git repository, so make sure it is ignored.
55-
std::fs::write(directory.join(".gitignore"), "*")
55+
fs::write(directory.join(".gitignore"), "*")
56+
.await
5657
.expect("failed to create crates-lsp .gitignore file.");
5758

5859
*self.directory.write().await = directory
@@ -68,7 +69,8 @@ impl CrateCache {
6869
};
6970

7071
// Attempt to load crate informtion from file cache.
71-
if let Ok(content) = std::fs::read_to_string(self.directory.read().await.join(crate_name)) {
72+
if let Ok(content) = fs::read_to_string(self.directory.read().await.join(crate_name)).await
73+
{
7274
if let Ok(fetch) = serde_json::from_str::<Fetch>(&content) {
7375
if OffsetDateTime::now_utc() < fetch.expires_at {
7476
self.put(crate_name, fetch.version.clone(), fetch.expires_at)
@@ -93,10 +95,11 @@ impl CrateCache {
9395
expires_at,
9496
};
9597

96-
std::fs::write(
98+
fs::write(
9799
self.directory.read().await.join(crate_name),
98100
serde_json::to_string(&fetch).as_deref().unwrap_or("{}"),
99101
)
102+
.await
100103
.unwrap();
101104

102105
self.crates

0 commit comments

Comments
 (0)