1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::path::Path;

use crate::{
    contents::Contents,
    drivers::Driver,
    errors::{DriverError, DriverResult},
};
pub struct Store {
    driver: Box<dyn Driver>,
}

impl Clone for Store {
    fn clone(&self) -> Self {
        Self {
            driver: dyn_clone::clone_box(&*self.driver),
        }
    }
}

impl Store {
    #[must_use]
    pub fn new(driver: Box<dyn Driver>) -> Self {
        Self { driver }
    }
    /// Checks if a file exists at the specified path within the storage.
    ///
    /// # Parameters
    ///
    /// - `path`: The path to the file to be checked.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::path::PathBuf;
    /// use active_storage::StoreConfig;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let inmem_driver = StoreConfig::InMem().build().await.unwrap();
    ///     let file_path = PathBuf::from("test.txt");
    ///     inmem_driver.write(file_path.as_path(), "my content").await;
    ///     assert!(inmem_driver.file_exists(file_path.as_path()).await.unwrap());
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying `Driver` encounters an issue while
    /// checking file existence.
    pub async fn file_exists(&self, path: &Path) -> DriverResult<bool> {
        self.driver.file_exists(path).await
    }

    /// Writes the provided contents to a file at the specified path within the
    /// storage.
    ///
    /// # Parameters
    ///
    /// - `path`: The path to the file to be written.
    /// - `contents`: The contents to be written to the file.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::path::PathBuf;
    /// use active_storage::StoreConfig;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let inmem_driver = StoreConfig::InMem().build().await.unwrap();
    ///     let file_path = PathBuf::from("test.txt");
    ///     assert!(inmem_driver.write(file_path.as_path(), "my content").await.is_ok());
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying `Driver` encounters an issue while
    /// writing to the file.
    pub async fn write<C: AsRef<[u8]> + Send>(&self, path: &Path, content: C) -> DriverResult<()> {
        self.driver.write(path, content.as_ref().to_vec()).await
    }

    /// Reads the contents of a file at the specified path within the storage.
    ///
    /// # Parameters
    ///
    /// - `path`: The path to the file to be read.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::path::PathBuf;
    /// use active_storage::StoreConfig;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let inmem_driver = StoreConfig::InMem().build().await.unwrap();
    ///     let file_path = PathBuf::from("test.txt");
    ///     inmem_driver.write(file_path.as_path(), "my content").await;
    ///     assert_eq!(
    ///         inmem_driver.read::<String>(file_path.as_path()).await.unwrap(),
    ///         "my content".to_string(),
    ///     );
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying `Driver` encounters an issue while
    /// reading from the file.
    pub async fn read<T: TryFrom<Contents>>(&self, path: &Path) -> DriverResult<T> {
        Contents::from(self.driver.read(path).await?)
            .try_into()
            .map_or_else(|_| Err(DriverError::DecodeError), |content| Ok(content))
    }

    /// Deletes a file at the specified path within the storage.
    ///
    /// # Parameters
    ///
    /// - `path`: The path to the file to be deleted.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::path::PathBuf;
    /// use active_storage::StoreConfig;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let inmem_driver = StoreConfig::InMem().build().await.unwrap();
    ///     let file_path = PathBuf::from("test.txt");
    ///     inmem_driver.write(file_path.as_path(), "my content").await;
    ///     assert!(inmem_driver.delete(file_path.as_path()).await.is_ok());
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying `Driver` encounters an issue while
    /// deleting the file.
    pub async fn delete(&self, path: &Path) -> DriverResult<()> {
        self.driver.delete(path).await
    }

    /// Deletes a directory at the specified path within the storage.
    ///
    /// # Parameters
    ///
    /// - `path`: The path to the directory to be deleted.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::path::PathBuf;
    /// use active_storage::StoreConfig;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let inmem_driver = StoreConfig::InMem().build().await.unwrap();
    ///     let folder = PathBuf::from("foo");
    ///     let file_path = folder.join("bar.txt");
    ///     inmem_driver.write(file_path.as_path(), "my content").await;
    ///     assert!(inmem_driver.delete_directory(folder.as_path()).await.is_ok());
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying `Driver` encounters an issue while
    /// deleting the directory.
    pub async fn delete_directory(&self, path: &Path) -> DriverResult<()> {
        self.driver.delete_directory(path).await
    }

    /// Retrieves the last modified timestamp of a file at the specified path
    /// within the storage.
    ///
    /// # Parameters
    ///
    /// - `path`: The path to the file for which the last modified timestamp is
    ///   retrieved.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::path::PathBuf;
    /// use active_storage::StoreConfig;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let inmem_driver = StoreConfig::InMem().build().await.unwrap();
    ///     let file_path = PathBuf::from("test.txt");
    ///     println!("{:#?}", inmem_driver.last_modified(file_path.as_path()).await);
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying `Driver` encounters an issue while
    /// retrieving the timestamp.
    pub async fn last_modified(&self, path: &Path) -> DriverResult<std::time::SystemTime> {
        self.driver.last_modified(path).await
    }
}