about summary refs log tree commit diff
path: root/src/database/file
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-08-01 19:48:37 +0300
committerVika <vika@fireburn.ru>2024-08-01 20:40:00 +0300
commit7e8e688e2e58f9c944b941e768ab7b034a348a1f (patch)
tree1068469c6b9b97bac407038276fd8971b2101e48 /src/database/file
parent57a9c3c7e520714928904fc7e2ff3d62ac2b2467 (diff)
downloadkittybox-7e8e688e2e58f9c944b941e768ab7b034a348a1f.tar.zst
treewide: create a common method for state initialization
Now the database objects can be uniformly created from a URI. They can
also optionally do sanity checks and one-time initialization.
Diffstat (limited to 'src/database/file')
-rw-r--r--src/database/file/mod.rs14
1 files changed, 5 insertions, 9 deletions
diff --git a/src/database/file/mod.rs b/src/database/file/mod.rs
index f6715e1..ba8201f 100644
--- a/src/database/file/mod.rs
+++ b/src/database/file/mod.rs
@@ -197,15 +197,7 @@ fn modify_post(post: &serde_json::Value, update: MicropubUpdate) -> Result<serde
 /// A backend using a folder with JSON files as a backing store.
 /// Uses symbolic links to represent a many-to-one mapping of URLs to a post.
 pub struct FileStorage {
-    root_dir: PathBuf,
-}
-
-impl FileStorage {
-    /// Create a new storage wrapping a folder specified by root_dir.
-    pub async fn new(root_dir: PathBuf) -> Result<Self> {
-        // TODO check if the dir is writable
-        Ok(Self { root_dir })
-    }
+    pub(super) root_dir: PathBuf,
 }
 
 async fn hydrate_author<S: Storage>(
@@ -255,6 +247,10 @@ async fn hydrate_author<S: Storage>(
 
 #[async_trait]
 impl Storage for FileStorage {
+    async fn new(url: &'_ url::Url) -> Result<Self> {
+        // TODO: sanity check
+        Ok(Self { root_dir: PathBuf::from(url.path()) })
+    }
     #[tracing::instrument(skip(self))]
     async fn categories(&self, url: &str) -> Result<Vec<String>> {
         // This requires an expensive scan through the entire