refactor: convert file operations to async using tokio fs (#5267)

* refactor: convert file operations to async using tokio fs

* refactor: integrate AsyncHandler for file operations in backup processes
This commit is contained in:
Tunglies
2025-11-01 16:46:03 +08:00
committed by Tunglies
parent 413f29e22a
commit b3b8eeb577
12 changed files with 210 additions and 183 deletions

View File

@@ -7,7 +7,8 @@ use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use serde_yaml_ng::Mapping;
use smartstring::alias::String;
use std::{fs, time::Duration};
use std::time::Duration;
use tokio::fs;
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct PrfItem {
@@ -546,24 +547,28 @@ impl PrfItem {
}
/// get the file data
pub fn read_file(&self) -> Result<String> {
pub async fn read_file(&self) -> Result<String> {
let file = self
.file
.clone()
.as_ref()
.ok_or_else(|| anyhow::anyhow!("could not find the file"))?;
let path = dirs::app_profiles_dir()?.join(file.as_str());
let content = fs::read_to_string(path).context("failed to read the file")?;
let content = fs::read_to_string(path)
.await
.context("failed to read the file")?;
Ok(content.into())
}
/// save the file data
pub fn save_file(&self, data: String) -> Result<()> {
pub async fn save_file(&self, data: String) -> Result<()> {
let file = self
.file
.clone()
.as_ref()
.ok_or_else(|| anyhow::anyhow!("could not find the file"))?;
let path = dirs::app_profiles_dir()?.join(file.as_str());
fs::write(path, data.as_bytes()).context("failed to save the file")
fs::write(path, data.as_bytes())
.await
.context("failed to save the file")
}
}