refactor: replace EncryptionGuard with with_encryption for better async context handling

This commit is contained in:
Tunglies
2025-11-05 16:56:04 +08:00
Unverified
parent 8c3e9c9ea9
commit ea41e71f72
2 changed files with 16 additions and 30 deletions

View File

@@ -1,4 +1,4 @@
use crate::{config::EncryptionGuard, enhance::seq::SeqMap, logging, utils::logging::Type};
use crate::{config::with_encryption, enhance::seq::SeqMap, logging, utils::logging::Type};
use anyhow::{Context, Result, anyhow, bail};
use nanoid::nanoid;
use serde::{Serialize, de::DeserializeOwned};
@@ -11,10 +11,9 @@ pub async fn read_yaml<T: DeserializeOwned>(path: &PathBuf) -> Result<T> {
bail!("file not found \"{}\"", path.display());
}
let _guard = EncryptionGuard::new();
let yaml_str = tokio::fs::read_to_string(path).await?;
Ok(serde_yaml_ng::from_str::<T>(&yaml_str)?)
Ok(with_encryption(|| async { serde_yaml_ng::from_str::<T>(&yaml_str) }).await?)
}
/// read mapping from yaml
@@ -66,8 +65,7 @@ pub async fn save_yaml<T: Serialize + Sync>(
data: &T,
prefix: Option<&str>,
) -> Result<()> {
let _guard = EncryptionGuard::new();
let data_str = serde_yaml_ng::to_string(data)?;
let data_str = with_encryption(|| async { serde_yaml_ng::to_string(data) }).await?;
let yaml_str = match prefix {
Some(prefix) => format!("{prefix}\n\n{data_str}"),