From b8c4575db0b223d13b16ab4a37dca65bb7089ed9 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Arnaud Date: Mon, 12 Aug 2024 21:51:37 +0200 Subject: [PATCH] refactor: simplify tokio usage --- src/services/storage/fs.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/services/storage/fs.rs b/src/services/storage/fs.rs index c9093e2..62ee01c 100644 --- a/src/services/storage/fs.rs +++ b/src/services/storage/fs.rs @@ -6,8 +6,6 @@ use std::path::{Path, PathBuf}; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; - use super::Storage; use crate::model::cargo::CrateMetadata; use crate::utils::apierror::{error_not_found, ApiError}; @@ -43,9 +41,7 @@ impl<'config> FsStorage<'config> { async fn write_to_file(&self, path: &str, content: &[u8]) -> Result<(), ApiError> { let full_path = PathBuf::from(format!("{}/{path}", self.data_dir)); tokio::fs::create_dir_all(full_path.parent().unwrap()).await?; - let file = tokio::fs::File::create(&full_path).await?; - let mut writer = tokio::io::BufWriter::new(file); - writer.write_all(content).await?; + tokio::fs::write(full_path, content).await?; Ok(()) } @@ -61,11 +57,8 @@ impl<'config> FsStorage<'config> { return Err(error_not_found()); } } - let file = tokio::fs::File::open(&full_path).await?; - let mut reader = tokio::io::BufReader::new(file); - let mut buffer = Vec::new(); - reader.read_to_end(&mut buffer).await?; - Ok(buffer) + + Ok(tokio::fs::read(full_path).await?) } }