stry_common/models/
mod.rs

1//! Database entity models, and utility wrappers and newtypes.
2
3// TODO: maybe create an area for users to make notes (or a wiki), like a staging area for stories and chapters
4
5pub mod blog;
6pub mod core;
7pub mod story;
8pub mod wiki;
9
10use {
11    crate::utils::nanoid,
12    arrayvec::ArrayString,
13    chrono::{DateTime, Utc},
14    std::ops::Deref,
15};
16
17crate::newtype! {
18    /// The database entry id newtype, is a [`ArrayString`] by default
19    #[derive(serde::Deserialize, serde::Serialize)]
20    Id: ArrayString<[u8; nanoid::SIZE]>
21}
22
23/// A wrapper type to indicate that a type has no backend id.
24#[rustfmt::skip]
25#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
26#[derive(serde::Deserialize, serde::Serialize)]
27pub struct New<T> {
28    inner: T,
29}
30
31impl<T> Deref for New<T> {
32    type Target = T;
33
34    fn deref(&self) -> &Self::Target {
35        &self.inner
36    }
37}
38
39/// A wrapper type to indicate that a type has a backend id.
40#[rustfmt::skip]
41#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
42#[derive(serde::Deserialize, serde::Serialize)]
43pub struct Existing<T> {
44    /// The entity's `Id`.
45    ///
46    /// # Note
47    ///
48    /// Each entity is tracked with this `Id` and is unique so no two entity
49    /// should ever have the same `Id`.
50    ///
51    /// Due to this is also shouldn't be possible to allow the changing of
52    /// the `Id` in anyway.
53    pub id: Id,
54
55    inner: T,
56
57    /// The time this entity was made.
58    ///
59    /// # Note
60    ///
61    /// Once created this should never change.
62    pub created: DateTime<Utc>,
63
64    /// The last time this entity was updated.
65    pub updated: DateTime<Utc>,
66}
67
68impl<T> Deref for Existing<T> {
69    type Target = T;
70
71    fn deref(&self) -> &Self::Target {
72        &self.inner
73    }
74}