Skip to main content

spacetimedsl/
lib.rs

1pub use itertools;
2pub use spacetimedsl_derive::{SpacetimeDSL, dsl, hook};
3use std::fmt::Display;
4pub use {
5    delete::{DeletionResult, DeletionResultEntry, OnDeleteStrategy},
6    error::{ReferenceIntegrityViolationError, SpacetimeDSLError},
7};
8
9pub mod as_anonymous_view_context;
10pub mod as_reducer_context;
11pub mod as_view_context;
12pub mod get_auth;
13pub mod get_connection_id;
14pub mod get_immutable_database;
15pub mod get_module_identity;
16pub mod get_mutable_database;
17pub mod get_random;
18pub mod get_random_number_generator;
19pub mod get_sender;
20pub mod get_timestamp;
21
22pub mod delete;
23pub mod error;
24
25pub mod prelude {
26    pub use crate::{
27        Context, DSL, DSLContext, ReadContext, ReadOnlyDSL, ReadOnlyDSLContext, SpacetimeDSL,
28        Wrapper, WriteContext,
29        as_anonymous_view_context::AsAnonymousViewContext,
30        as_reducer_context::AsReducerContext,
31        as_view_context::AsViewContext,
32        delete::{DeletionResult, DeletionResultEntry},
33        dsl,
34        error::{ReferenceIntegrityViolationError, SpacetimeDSLError},
35        get_auth::GetAuth,
36        get_connection_id::GetConnectionId,
37        get_immutable_database::GetImmutableDatabase,
38        get_module_identity::GetModuleIdentity,
39        get_mutable_database::GetMutableDatabase,
40        get_random::GetRandom,
41        get_random_number_generator::GetRandomNumberGenerator,
42        get_sender::GetSender,
43        get_timestamp::GetTimestamp,
44        hook,
45        itertools::Itertools,
46        read_only_dsl,
47    };
48}
49
50use prelude::*;
51
52//region Write DSL
53
54pub struct DSL<'a, T: Context<spacetimedb::Local>> {
55    pub(crate) ctx: &'a T,
56    pub(crate) db: &'a spacetimedb::Local,
57}
58
59pub fn dsl<'a, T: Context<spacetimedb::Local>>(ctx: &'a T) -> DSL<'a, T> {
60    DSL {
61        ctx,
62        db: spacetimedb::DbContext::db(ctx),
63    }
64}
65
66pub trait DSLContext<T: Context<spacetimedb::Local>> {
67    fn dsl(&self) -> &DSL<'_, T>;
68
69    fn ctx(&self) -> &T;
70
71    fn db(&self) -> &spacetimedb::Local;
72}
73
74impl<T: Context<spacetimedb::Local>> DSLContext<T> for DSL<'_, T> {
75    fn dsl(&self) -> &DSL<'_, T> {
76        self
77    }
78
79    fn ctx(&self) -> &T {
80        self.ctx
81    }
82
83    fn db(&self) -> &spacetimedb::Local {
84        self.db
85    }
86}
87
88//endregion Write DSL
89
90//region Read-Only DSL
91
92pub struct ReadOnlyDSL<'a, T: Context<spacetimedb::LocalReadOnly>> {
93    pub(crate) ctx: &'a T,
94    pub(crate) db: &'a spacetimedb::LocalReadOnly,
95}
96
97pub fn read_only_dsl<'a, T: Context<spacetimedb::LocalReadOnly>>(ctx: &'a T) -> ReadOnlyDSL<'a, T> {
98    ReadOnlyDSL {
99        ctx,
100        db: spacetimedb::DbContext::db(ctx),
101    }
102}
103
104pub trait ReadOnlyDSLContext<T: Context<spacetimedb::LocalReadOnly>> {
105    fn dsl(&self) -> &ReadOnlyDSL<'_, T>;
106
107    fn ctx(&self) -> &T;
108
109    fn db(&self) -> &spacetimedb::LocalReadOnly;
110}
111
112impl<T: Context<spacetimedb::LocalReadOnly>> ReadOnlyDSLContext<T> for ReadOnlyDSL<'_, T> {
113    fn dsl(&self) -> &ReadOnlyDSL<'_, T> {
114        self
115    }
116
117    fn ctx(&self) -> &T {
118        self.ctx
119    }
120
121    fn db(&self) -> &spacetimedb::LocalReadOnly {
122        self.db
123    }
124}
125
126//endregion Read-Only DSL
127
128pub enum ContextType {
129    AnonymousView,
130    Reducer,
131    Transaction,
132    View,
133}
134
135fn get_err(msg: &str, ctx: ContextType) -> SpacetimeDSLError {
136    crate::SpacetimeDSLError::Error(format!(
137        "{msg}. Tried to access from {} Context.",
138        match ctx {
139            ContextType::AnonymousView => "Anonymous View",
140            ContextType::Reducer => "Reducer",
141            ContextType::Transaction => "Transaction",
142            ContextType::View => "View",
143        }
144    ))
145}
146pub trait Context<T>:
147    GetAuth
148    + GetConnectionId
149    + GetImmutableDatabase
150    + GetModuleIdentity
151    + GetMutableDatabase
152    + GetRandom
153    + GetRandomNumberGenerator
154    + GetSender
155    + GetTimestamp
156    + AsAnonymousViewContext
157    + AsReducerContext
158    + AsViewContext
159    + spacetimedb::DbContext<DbView = T>
160{
161}
162pub trait WriteContext: Context<spacetimedb::Local> {}
163
164pub trait ReadContext: Context<spacetimedb::LocalReadOnly> {}
165
166pub struct DSLMethodHooks {}
167
168pub trait Wrapper<WrappedType: Clone + Default, WrapperType>:
169    Default + Clone + PartialEq + PartialOrd + spacetimedb::SpacetimeType + Display
170{
171    fn new(value: WrappedType) -> Self;
172    fn value(&self) -> WrappedType;
173}
174
175#[doc(hidden)]
176pub mod internal {
177    pub struct DSLInternals;
178}