about summary refs log tree commit diff
path: root/src/indieauth
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/indieauth
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/indieauth')
-rw-r--r--src/indieauth/backend.rs2
-rw-r--r--src/indieauth/backend/fs.rs12
2 files changed, 8 insertions, 6 deletions
diff --git a/src/indieauth/backend.rs b/src/indieauth/backend.rs
index 534bcfb..5814dc2 100644
--- a/src/indieauth/backend.rs
+++ b/src/indieauth/backend.rs
@@ -11,6 +11,8 @@ pub use fs::FileBackend;
 
 #[async_trait::async_trait]
 pub trait AuthBackend: Clone + Send + Sync + 'static {
+    /// Initialize self from URL, possibly performing initialization.
+    async fn new(url: &'_ url::Url) -> Result<Self>;
     // Authorization code management.
     /// Create a one-time OAuth2 authorization code for the passed
     /// authorization request, and save it for later retrieval.
diff --git a/src/indieauth/backend/fs.rs b/src/indieauth/backend/fs.rs
index 80c3703..5e97ae5 100644
--- a/src/indieauth/backend/fs.rs
+++ b/src/indieauth/backend/fs.rs
@@ -20,12 +20,6 @@ pub struct FileBackend {
 }
 
 impl FileBackend {
-    pub fn new<T: Into<PathBuf>>(path: T) -> Self {
-        Self {
-            path: path.into()
-        }
-    }
-
     /// Sanitize a filename, leaving only alphanumeric characters.
     ///
     /// Doesn't allocate a new string unless non-alphanumeric
@@ -193,6 +187,12 @@ impl FileBackend {
 
 #[async_trait]
 impl AuthBackend for FileBackend {
+    async fn new(path: &'_ url::Url) -> Result<Self> {
+        Ok(Self {
+            path: std::path::PathBuf::from(path.path())
+        })
+    }
+
     // Authorization code management.
     async fn create_code(&self, data: AuthorizationRequest) -> Result<String> {
         self.serialize_to_file("codes", None, CODE_LENGTH, data).await