crdb_core/
config.rs

1use crate::{
2    CanDoCallbacks, ClientSideDb, Db, EventId, Importance, ObjectId, ReadPermsChanges,
3    ServerSideDb, TypeId, Update, Updatedness, User,
4};
5use std::{collections::HashSet, sync::Arc};
6
7#[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize)]
8pub struct UploadId(pub i64);
9
10// Each update is both the list of updates itself, and the new latest snapshot
11// for query matching, available if the latest snapshot actually changed. Also,
12// the list of users allowed to read this object.
13// TODO(api-highest): this is only used server-side internally, move it there?
14#[derive(Debug)]
15pub struct UpdatesWithSnap {
16    // The list of actual updates
17    pub updates: Vec<Arc<Update>>,
18
19    // The new last snapshot, if the update did change it (ie. no vacuum) and if the users affected
20    // actually do have access to it. This is used for query matching.
21    // It is always on the latest snapshot version
22    pub new_last_snapshot: Option<Arc<serde_json::Value>>,
23}
24
25// This module needs to actually be public, because the `db` macro needs to be
26// able to implement it. However, making it doc(hidden) makes it look as though
27// it is actually sealed in the documentation, which is good because we don't want
28// users to rely on any stability guarantees there.
29#[doc(hidden)]
30pub mod private {
31    pub trait Sealed {}
32}
33
34pub trait Config: 'static + Send + Sync + private::Sealed {
35    /// Auto-generated by `crdb::db!`.
36    ///
37    /// Panics if there are two types with the same ULID configured
38    fn check_ulids();
39
40    fn reencode_old_versions<D: Db>(call_on: &D) -> impl '_ + waaaa::Future<Output = usize>;
41
42    // TODO(api-high): is this unused?
43    fn create<D: Db>(
44        db: &D,
45        type_id: TypeId,
46        object_id: ObjectId,
47        created_at: EventId,
48        snapshot_version: i32,
49        object: &serde_json::Value,
50    ) -> impl waaaa::Future<Output = crate::Result<()>>;
51
52    /// The returned serde_json::Value is guaranteed to be a serialization of the latest snapshot of the object at the current snapshot version
53    #[allow(clippy::too_many_arguments)] // Used only for relaying to a more specific function
54    fn recreate<D: ClientSideDb>(
55        db: &D,
56        type_id: TypeId,
57        object_id: ObjectId,
58        created_at: EventId,
59        snapshot_version: i32,
60        object: &serde_json::Value,
61        updatedness: Option<Updatedness>,
62        additional_importance: Importance,
63    ) -> impl waaaa::Future<Output = crate::Result<Option<serde_json::Value>>>;
64
65    /// The returned serde_json::Value is guaranteed to be a serialization of the latest snapshot of the object at the current snapshot version
66    fn submit<D: Db>(
67        db: &D,
68        type_id: TypeId,
69        object_id: ObjectId,
70        event_id: EventId,
71        event: &serde_json::Value,
72        updatedness: Option<Updatedness>,
73        additional_importance: Importance,
74    ) -> impl waaaa::Future<Output = crate::Result<Option<serde_json::Value>>>;
75
76    fn remove_event<D: ClientSideDb>(
77        db: &D,
78        type_id: TypeId,
79        object_id: ObjectId,
80        event_id: EventId,
81    ) -> impl waaaa::Future<Output = crate::Result<()>>;
82
83    #[allow(clippy::type_complexity)]
84    fn get_users_who_can_read<'a, D: ServerSideDb, C: CanDoCallbacks>(
85        call_on: &'a D,
86        object_id: ObjectId,
87        type_id: TypeId,
88        snapshot_version: i32,
89        snapshot: serde_json::Value,
90        cb: &'a C,
91    ) -> impl 'a + waaaa::Future<Output = crate::Result<crate::UsersWhoCanRead<D::Lock<'a>>>>;
92
93    // TODO(api-high): rename into recreate_at?
94    #[allow(clippy::type_complexity)] // This is only used to proxy to a real function
95    fn recreate_no_lock<'a, D: ServerSideDb, C: CanDoCallbacks>(
96        call_on: &'a D,
97        type_id: TypeId,
98        object_id: ObjectId,
99        event_id: EventId,
100        updatedness: Updatedness,
101        cb: &'a C,
102    ) -> impl 'a
103           + waaaa::Future<
104        Output = crate::Result<Option<(EventId, i32, serde_json::Value, HashSet<User>)>>,
105    >;
106
107    #[allow(clippy::too_many_arguments, clippy::type_complexity)] // This is only used to proxy to a real function
108    fn upload_object<D: ServerSideDb>(
109        call_on: &D,
110        user: User,
111        updatedness: Updatedness,
112        type_id: TypeId,
113        object_id: ObjectId,
114        created_at: EventId,
115        snapshot_version: i32,
116        snapshot: Arc<serde_json::Value>,
117    ) -> impl '_
118           + waaaa::Future<
119        Output = crate::Result<
120            Option<(Arc<UpdatesWithSnap>, HashSet<User>, Vec<ReadPermsChanges>)>,
121        >,
122    >;
123
124    /// The [`Vec<User>`] in return type is the list of users who can read the object both before and after the change. Users who gained or
125    /// lost access to `object_id` are returned as part of the `Vec<ReadPermsChanges>`.
126    #[allow(clippy::type_complexity)] // This is only used to proxy to a real function
127    fn upload_event<D: ServerSideDb>(
128        call_on: &D,
129        user: User,
130        updatedness: Updatedness,
131        type_id: TypeId,
132        object_id: ObjectId,
133        event_id: EventId,
134        event: Arc<serde_json::Value>,
135    ) -> impl '_
136           + waaaa::Future<
137        Output = crate::Result<Option<(Arc<UpdatesWithSnap>, Vec<User>, Vec<ReadPermsChanges>)>>,
138    >;
139}