active_storage/lib.rs
1//! # Active Storage
2//!
3//! Active Storage streamlines the process of uploading files to cloud storage,
4//! offering both local disk-based and in-memory services for development and
5//! testing. Additionally, it supports mirroring files to subordinate services,
6//! enhancing capabilities for backups and migrations.
7//!
8//! It's inspired by Rails [Active Store](https://guides.rubyonrails.org/active_storage_overview.html)
9//!
10//! ## Services
11//!
12//! * [Disk](./examples/disk.rs)
13//! * [In Memory](./examples/in_memory.rs)
14//! * [AWS S3](./examples/aws_s3.rs) - Requires enabling the `aws_s3` feature.
15//!
16//! ## Examples
17//!
18//! ```rust
19//! # #[cfg(feature = "derive")] {
20#![doc = include_str!("../examples/disk.rs")]
21//! # }
22//! ```
23//!
24//! ### Mirroring
25//! ```rust
26//! # #[cfg(feature = "derive")] {
27#![doc = include_str!("../examples/multi.rs")]
28//! # }
29//! ```
30
31mod contents;
32pub mod drivers;
33pub mod errors;
34pub mod multi_store;
35pub mod store;
36
37/// The [`StoreConfig`] enum represents configuration options for building a
38/// storage system. It includes different variants for various storage options,
39/// and the availability of these variants depends on compile-time feature
40/// flags.
41///
42/// ## Enum Variants
43///
44/// - `InMem`: In-memory storage variant. This variant is available when the
45/// `inmem` feature is enabled.
46///
47/// - `AwsS3`: AWS S3 storage variant. This variant is available when the
48/// `aws_s3` feature is enabled. It includes a configuration parameter.
49///
50/// - `Disk`: Disk storage variant. This variant is available when the `disk`
51/// feature is enabled. It includes a configuration parameter.
52pub enum StoreConfig {
53 #[cfg(feature = "inmem")]
54 InMem(),
55 #[cfg(feature = "aws_s3")]
56 AwsS3(drivers::aws_s3::Config),
57 #[cfg(feature = "disk")]
58 Disk(drivers::disk::Config),
59}
60
61/// `StoreConfig` represents the configuration for creating a [`store::Store`]
62/// instance.
63impl StoreConfig {
64 /// Builds a [`store::Store`] instance based on the configured storage type.
65 ///
66 ///
67 /// # Examples
68 ///
69 /// ```
70 /// use std::path::PathBuf;
71 /// use active_storage::StoreConfig;
72 ///
73 /// async fn example() {
74 /// let inmem_driver = StoreConfig::InMem().build().await.unwrap();
75 /// let file_path = PathBuf::from("test.txt");
76 /// inmem_driver
77 /// .write(file_path.as_path(), "my content")
78 /// .await
79 /// .unwrap();
80 /// }
81 /// ```
82 /// # Errors
83 ///
84 /// Returns a [`errors::DriverResult`] when could not initialize the driver
85 /// store
86 #[allow(clippy::unused_async)]
87 pub async fn build(self) -> errors::DriverResult<store::Store> {
88 let driver = match self {
89 #[cfg(feature = "inmem")]
90 Self::InMem() => {
91 Box::<drivers::inmem::InMemoryDriver>::default() as Box<dyn drivers::Driver>
92 }
93 #[cfg(feature = "aws_s3")]
94 Self::AwsS3(config) => {
95 Box::new(drivers::aws_s3::AwsS3::new(config)) as Box<dyn drivers::Driver>
96 }
97 #[cfg(feature = "disk")]
98 Self::Disk(config) => {
99 Box::new(drivers::disk::DiskDriver::new(config).await?) as Box<dyn drivers::Driver>
100 }
101 };
102
103 Ok(store::Store::new(driver))
104 }
105
106 /// Creates a [`store::Store`] instance with the provided storage driver.
107 #[must_use]
108 pub fn with_driver(driver: Box<dyn drivers::Driver>) -> store::Store {
109 store::Store::new(driver)
110 }
111}