Skip to content

Commit

Permalink
Update APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Jan 2, 2025
1 parent 23c6426 commit 0426118
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
14 changes: 5 additions & 9 deletions src/storage/drivers/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ use crate::storage::{drivers::opendal_adapter::OpendalAdapter, StorageResult};
/// let file_system_driver = local::new();
/// ```
///
/// # Panics
/// # Errors
///
/// Panics if the filesystem service built failed.
#[must_use]
pub fn new() -> Box<dyn StoreDriver> {
/// Errors if the filesystem service built failed.
pub fn new() -> StorageResult<Box<dyn StoreDriver>> {
let fs = Fs::default().root("/");
Box::new(OpendalAdapter::new(
Operator::new(fs)
.expect("fs service should build with success")
.finish(),
))
let store = Box::new(OpendalAdapter::new(Operator::new(fs)?.finish()));
Ok(store)
}

/// Create new filesystem storage with `prefix` applied to all paths
Expand Down
17 changes: 8 additions & 9 deletions src/storage/drivers/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use opendal::{services::Memory, Operator};

use super::StoreDriver;
use crate::storage::drivers::opendal_adapter::OpendalAdapter;
use crate::storage::StorageResult;

/// Create new in-memory storage.
///
Expand All @@ -11,14 +12,12 @@ use crate::storage::drivers::opendal_adapter::OpendalAdapter;
/// let mem_storage = mem::new();
/// ```
///
/// # Panics
/// # Errors
///
/// Panics if the memory service built failed.
#[must_use]
pub fn new() -> Box<dyn StoreDriver> {
Box::new(OpendalAdapter::new(
Operator::new(Memory::default())
.expect("memory service must build with success")
.finish(),
))
/// Errors if the memory service built failed.
pub fn new() -> StorageResult<Box<dyn StoreDriver>> {
let store = Box::new(OpendalAdapter::new(
Operator::new(Memory::default())?.finish(),
));
Ok(store)
}
4 changes: 2 additions & 2 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Storage {
///```
/// use loco_rs::storage;
///
/// let storage = storage::Storage::single(storage::drivers::mem::new());
/// let storage = storage::Storage::single(storage::drivers::mem::new().unwrap());
/// ```
#[must_use]
pub fn single(store: Box<dyn StoreDriver>) -> Self {
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Storage {
/// use std::path::Path;
/// use bytes::Bytes;
/// pub async fn upload() {
/// let storage = storage::Storage::single(storage::drivers::mem::new());
/// let storage = storage::Storage::single(storage::drivers::mem::new().unwrap());
/// let path = Path::new("example.txt");
/// let content = "Loco!";
/// let result = storage.upload(path, &Bytes::from(content)).await;
Expand Down
7 changes: 6 additions & 1 deletion src/tests_cfg/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use crate::{
tests_cfg::config::test_config,
};

/// Create a new `AppContext` for testing purposes.
///
/// # Panics
///
/// Panics if the storage drivers failed to initiate.
pub async fn get_app_context() -> AppContext {
AppContext {
environment: Environment::Test,
Expand All @@ -14,7 +19,7 @@ pub async fn get_app_context() -> AppContext {
queue_provider: None,
config: test_config(),
mailer: None,
storage: Storage::single(storage::drivers::mem::new()).into(),
storage: Storage::single(storage::drivers::mem::new().unwrap()).into(),
#[cfg(feature = "cache_inmem")]
cache: cache::Cache::new(cache::drivers::inmem::new()).into(),
#[cfg(not(feature = "cache_inmem"))]
Expand Down

0 comments on commit 0426118

Please sign in to comment.