active_storage/store.rs
1//! # Store Module
2//!
3//! The `store` module defines a `Store` struct, representing a storage unit
4//! with associated methods for file operations.
5//!
6//! ## Example Usage
7//!
8//! ```rust
9//! # #[cfg(feature = "derive")] {
10#![doc = include_str!("../examples/disk.rs")]
11//! # }
12//! ```
13use std::path::Path;
14
15use crate::{
16 contents::Contents,
17 drivers::Driver,
18 errors::{DriverError, DriverResult},
19};
20pub struct Store {
21 driver: Box<dyn Driver>,
22}
23
24impl Clone for Store {
25 fn clone(&self) -> Self {
26 Self {
27 driver: dyn_clone::clone_box(&*self.driver),
28 }
29 }
30}
31
32impl Store {
33 #[must_use]
34 pub fn new(driver: Box<dyn Driver>) -> Self {
35 Self { driver }
36 }
37 /// Checks if a file exists at the specified path within the storage.
38 ///
39 /// # Parameters
40 ///
41 /// - `path`: The path to the file to be checked.
42 ///
43 /// # Examples
44 ///
45 /// ```rust
46 /// use std::path::PathBuf;
47 /// use active_storage::StoreConfig;
48 ///
49 /// #[tokio::main]
50 /// async fn main() {
51 /// let inmem_driver = StoreConfig::InMem().build().await.unwrap();
52 /// let file_path = PathBuf::from("test.txt");
53 /// inmem_driver.write(file_path.as_path(), "my content").await;
54 /// assert!(inmem_driver.file_exists(file_path.as_path()).await.unwrap());
55 /// }
56 /// ```
57 ///
58 /// # Errors
59 ///
60 /// Returns an error if the underlying `Driver` encounters an issue while
61 /// checking file existence.
62 pub async fn file_exists(&self, path: &Path) -> DriverResult<bool> {
63 self.driver.file_exists(path).await
64 }
65
66 /// Writes the provided contents to a file at the specified path within the
67 /// storage.
68 ///
69 /// # Parameters
70 ///
71 /// - `path`: The path to the file to be written.
72 /// - `contents`: The contents to be written to the file.
73 ///
74 /// # Examples
75 ///
76 /// ```rust
77 /// use std::path::PathBuf;
78 /// use active_storage::StoreConfig;
79 ///
80 /// #[tokio::main]
81 /// async fn main() {
82 /// let inmem_driver = StoreConfig::InMem().build().await.unwrap();
83 /// let file_path = PathBuf::from("test.txt");
84 /// assert!(inmem_driver.write(file_path.as_path(), "my content").await.is_ok());
85 /// }
86 /// ```
87 ///
88 /// # Errors
89 ///
90 /// Returns an error if the underlying `Driver` encounters an issue while
91 /// writing to the file.
92 pub async fn write<C: AsRef<[u8]> + Send>(&self, path: &Path, content: C) -> DriverResult<()> {
93 self.driver.write(path, content.as_ref().to_vec()).await
94 }
95
96 /// Reads the contents of a file at the specified path within the storage.
97 ///
98 /// # Parameters
99 ///
100 /// - `path`: The path to the file to be read.
101 ///
102 /// # Examples
103 ///
104 /// ```rust
105 /// use std::path::PathBuf;
106 /// use active_storage::StoreConfig;
107 ///
108 /// #[tokio::main]
109 /// async fn main() {
110 /// let inmem_driver = StoreConfig::InMem().build().await.unwrap();
111 /// let file_path = PathBuf::from("test.txt");
112 /// inmem_driver.write(file_path.as_path(), "my content").await;
113 /// assert_eq!(
114 /// inmem_driver.read::<String>(file_path.as_path()).await.unwrap(),
115 /// "my content".to_string(),
116 /// );
117 /// }
118 /// ```
119 ///
120 /// # Errors
121 ///
122 /// Returns an error if the underlying `Driver` encounters an issue while
123 /// reading from the file.
124 pub async fn read<T: TryFrom<Contents>>(&self, path: &Path) -> DriverResult<T> {
125 Contents::from(self.driver.read(path).await?)
126 .try_into()
127 .map_or_else(|_| Err(DriverError::DecodeError), |content| Ok(content))
128 }
129
130 /// Deletes a file at the specified path within the storage.
131 ///
132 /// # Parameters
133 ///
134 /// - `path`: The path to the file to be deleted.
135 ///
136 /// # Examples
137 ///
138 /// ```rust
139 /// use std::path::PathBuf;
140 /// use active_storage::StoreConfig;
141 ///
142 /// #[tokio::main]
143 /// async fn main() {
144 /// let inmem_driver = StoreConfig::InMem().build().await.unwrap();
145 /// let file_path = PathBuf::from("test.txt");
146 /// inmem_driver.write(file_path.as_path(), "my content").await;
147 /// assert!(inmem_driver.delete(file_path.as_path()).await.is_ok());
148 /// }
149 /// ```
150 ///
151 /// # Errors
152 ///
153 /// Returns an error if the underlying `Driver` encounters an issue while
154 /// deleting the file.
155 pub async fn delete(&self, path: &Path) -> DriverResult<()> {
156 self.driver.delete(path).await
157 }
158
159 /// Deletes a directory at the specified path within the storage.
160 ///
161 /// # Parameters
162 ///
163 /// - `path`: The path to the directory to be deleted.
164 ///
165 /// # Examples
166 ///
167 /// ```rust
168 /// use std::path::PathBuf;
169 /// use active_storage::StoreConfig;
170 ///
171 /// #[tokio::main]
172 /// async fn main() {
173 /// let inmem_driver = StoreConfig::InMem().build().await.unwrap();
174 /// let folder = PathBuf::from("foo");
175 /// let file_path = folder.join("bar.txt");
176 /// inmem_driver.write(file_path.as_path(), "my content").await;
177 /// assert!(inmem_driver.delete_directory(folder.as_path()).await.is_ok());
178 /// }
179 /// ```
180 ///
181 /// # Errors
182 ///
183 /// Returns an error if the underlying `Driver` encounters an issue while
184 /// deleting the directory.
185 pub async fn delete_directory(&self, path: &Path) -> DriverResult<()> {
186 self.driver.delete_directory(path).await
187 }
188
189 /// Retrieves the last modified timestamp of a file at the specified path
190 /// within the storage.
191 ///
192 /// # Parameters
193 ///
194 /// - `path`: The path to the file for which the last modified timestamp is
195 /// retrieved.
196 ///
197 /// # Examples
198 ///
199 /// ```rust
200 /// use std::path::PathBuf;
201 /// use active_storage::StoreConfig;
202 ///
203 /// #[tokio::main]
204 /// async fn main() {
205 /// let inmem_driver = StoreConfig::InMem().build().await.unwrap();
206 /// let file_path = PathBuf::from("test.txt");
207 /// println!("{:#?}", inmem_driver.last_modified(file_path.as_path()).await);
208 /// }
209 /// ```
210 ///
211 /// # Errors
212 ///
213 /// Returns an error if the underlying `Driver` encounters an issue while
214 /// retrieving the timestamp.
215 pub async fn last_modified(&self, path: &Path) -> DriverResult<std::time::SystemTime> {
216 self.driver.last_modified(path).await
217 }
218}