stry_common/
backend.rs

1//! Types for the implementation of database backends.
2
3use {
4    crate::{
5        models::{
6            blog::Post,
7            core::{Comment, Part, User},
8            story::{Chapter, Character, Origin, Pairing, Series, Story, Tag, Warning},
9            wiki::{Category, Page},
10            Existing, Id, New,
11        },
12        uri::Uri,
13    },
14    std::error::Error,
15};
16
17/// A backend 'factory' that acts as a constructor for backends.
18#[cfg(feature = "with-backend")]
19#[async_trait::async_trait]
20pub trait BackendFactory {
21    type Error: Error;
22    type Backend: Backend<Self::Error>;
23
24    /// Essentially a `new` function.
25    async fn create(&self, config: Uri) -> Result<Self::Backend, Self::Error>;
26}
27
28/// A supported database backend that depends on a series of entries.
29///
30/// Requires that the backend also implements [`BackendEntry`] for these types
31/// (sharing the same error type):
32///
33///   - Core Types
34///     - [`Comment`]
35///     - [`Part`]
36///     - [`User`]
37///   - Blog Types
38///     - [`Post`]
39///   - Story Types
40///     - [`Chapter`]
41///     - [`Character`]
42///     - [`Origin`]
43///     - [`Pairing`]
44///     - [`Series`]
45///     - [`Story`]
46///     - [`Tag`]
47///     - [`Warning`]
48///   - Wiki Types
49///     - [`Category`]
50///     - [`Page`]
51#[cfg(feature = "with-backend")]
52#[rustfmt::skip]
53#[async_trait::async_trait]
54pub trait Backend<Err>:
55    // Core
56    BackendEntry<User, Err>
57    + BackendEntry<Comment, Err>
58    + BackendEntry<Part, Err>
59    // Blog
60    + BackendEntry<Post, Err>
61    // Story
62    + BackendEntry<Chapter, Err>
63    + BackendEntry<Origin, Err>
64    + BackendEntry<Warning, Err>
65    + BackendEntry<Pairing, Err>
66    + BackendEntry<Character, Err>
67    + BackendEntry<Tag, Err>
68    + BackendEntry<Story, Err>
69    + BackendEntry<Series, Err>
70    // Wiki
71    + BackendEntry<Category, Err>
72    + BackendEntry<Page, Err>
73where
74    Err: Error,
75{
76    /// Run any missing migration on the database backend.
77    async fn migrate(&self) -> Result<(), Err>;
78}
79
80/// A database entry, or something that can be stored and retrieved from a database.
81#[cfg(feature = "with-backend")]
82#[async_trait::async_trait]
83pub trait BackendEntry<Entry, Error> {
84    /// Get an entity of type with a specific id.
85    async fn get(&self, id: Id) -> Result<Existing<Entry>, Error>;
86    /// Get all entities of type using offset cursor
87    async fn all(&self, cursor: Id, limit: usize) -> Result<Vec<Existing<Entry>>, Error>;
88
89    /// Create a new entity of type.
90    async fn create(&self, data: New<Entry>) -> Result<Id, Error>;
91    /// Update an entity of type's data.
92    async fn update(&self, data: Existing<Entry>) -> Result<(), Error>;
93    /// Remove a entity of type.
94    async fn remove(&self, id: Id) -> Result<(), Error>;
95}