spacetimedsl 0.21.1

The SpacetimeDB Rust Server Module meta-framework
Documentation
pub use itertools;
pub use spacetimedsl_derive::{SpacetimeDSL, dsl, hook};
use std::fmt::Display;
pub use {
    delete::{DeletionResult, DeletionResultEntry, OnDeleteStrategy},
    error::{ReferenceIntegrityViolationError, SpacetimeDSLError},
};

pub mod as_anonymous_view_context;
pub mod as_reducer_context;
pub mod as_view_context;
pub mod get_auth;
pub mod get_connection_id;
pub mod get_immutable_database;
pub mod get_module_identity;
pub mod get_mutable_database;
pub mod get_random;
pub mod get_random_number_generator;
pub mod get_sender;
pub mod get_timestamp;

pub mod delete;
pub mod error;

use as_anonymous_view_context::AsAnonymousViewContext;
use as_reducer_context::AsReducerContext;
use as_view_context::AsViewContext;
use get_auth::GetAuth;
use get_connection_id::GetConnectionId;
use get_immutable_database::GetImmutableDatabase;
use get_module_identity::GetModuleIdentity;
use get_mutable_database::GetMutableDatabase;
use get_random::GetRandom;
use get_random_number_generator::GetRandomNumberGenerator;
use get_sender::GetSender;
use get_timestamp::GetTimestamp;

pub enum ContextType {
    AnonymousView,
    Reducer,
    Transaction,
    View,
}

pub(crate) fn get_err(msg: &str, ctx: ContextType) -> SpacetimeDSLError {
    crate::SpacetimeDSLError::Error(format!(
        "{msg}. Tried to access from {} Context.",
        match ctx {
            ContextType::AnonymousView => "Anonymous View",
            ContextType::Reducer => "Reducer",
            ContextType::Transaction => "Transaction",
            ContextType::View => "View",
        }
    ))
}

pub trait Context<T>:
    GetAuth
    + GetConnectionId
    + GetImmutableDatabase
    + GetModuleIdentity
    + GetMutableDatabase
    + GetRandom
    + GetRandomNumberGenerator
    + GetSender
    + GetTimestamp
    + AsAnonymousViewContext
    + AsReducerContext
    + AsViewContext
    + spacetimedb::DbContext<DbView = T>
{
}

pub trait WriteContext: Context<spacetimedb::Local> {}

pub trait ReadContext: Context<spacetimedb::LocalReadOnly> {}

pub trait Wrapper<WrappedType: Clone + Default, WrapperType>:
    Default + Clone + PartialEq + PartialOrd + spacetimedb::SpacetimeType + Display
{
    fn new(value: WrappedType) -> Self;
    fn value(&self) -> WrappedType;
}

/// Invoke this macro once at the root of your crate (`lib.rs`) to create the
/// `crate::spacetimedsl` module that holds the DSL structs, constructors, and
/// all types generated by `#[dsl]` attributes.
///
/// ```rust,ignore
/// // lib.rs
/// ::spacetimedsl::spacetimedsl!();
/// ```
///
/// Then import everything you need with:
///
/// ```rust,ignore
/// use crate::spacetimedsl::prelude::*;
/// ```
#[macro_export]
macro_rules! spacetimedsl {
    () => {
        /// Generated by [`::spacetimedsl::spacetimedsl!()`]. Contains all DSL
        /// structs, constructor functions, and the prelude for this crate.
        pub mod spacetimedsl {
            // Use `::spacetimedsl::` (global path) throughout this module.
            // The local module name `spacetimedsl` creates ambiguity with `$crate` so
            // we use the explicit `::` prefix to reference the external crate.
            pub struct DSL<'a, T: ::spacetimedsl::Context<spacetimedb::Local>> {
                ctx: &'a T,
                db: &'a spacetimedb::Local,
            }

            pub fn dsl<'a, T: ::spacetimedsl::Context<spacetimedb::Local>>(
                ctx: &'a T,
            ) -> DSL<'a, T> {
                DSL {
                    ctx,
                    db: spacetimedb::DbContext::db(ctx),
                }
            }

            impl<T: ::spacetimedsl::Context<spacetimedb::Local>> DSL<'_, T> {
                pub fn dsl(&self) -> &DSL<'_, T> {
                    self
                }
                pub fn ctx(&self) -> &T {
                    self.ctx
                }
                pub fn db(&self) -> &spacetimedb::Local {
                    self.db
                }
            }

            pub struct ReadOnlyDSL<'a, T: ::spacetimedsl::Context<spacetimedb::LocalReadOnly>> {
                ctx: &'a T,
                db: &'a spacetimedb::LocalReadOnly,
            }

            pub fn read_only_dsl<'a, T: ::spacetimedsl::Context<spacetimedb::LocalReadOnly>>(
                ctx: &'a T,
            ) -> ReadOnlyDSL<'a, T> {
                ReadOnlyDSL {
                    ctx,
                    db: spacetimedb::DbContext::db(ctx),
                }
            }

            impl<T: ::spacetimedsl::Context<spacetimedb::LocalReadOnly>> ReadOnlyDSL<'_, T> {
                pub fn dsl(&self) -> &ReadOnlyDSL<'_, T> {
                    self
                }
                pub fn ctx(&self) -> &T {
                    self.ctx
                }
                pub fn db(&self) -> &spacetimedb::LocalReadOnly {
                    self.db
                }
            }

            pub struct DSLMethodHooks {}

            #[doc(hidden)]
            pub mod internal {
                pub struct DSLInternals;
            }

            // Re-export key extern-crate items directly so that proc-macro–generated code
            // can reach them via `crate::spacetimedsl::X` without needing `::spacetimedsl::X`.
            pub use ::spacetimedsl::Context;
            pub use ::spacetimedsl::ReadContext;
            pub use ::spacetimedsl::Wrapper;
            pub use ::spacetimedsl::WriteContext;
            pub use ::spacetimedsl::delete;
            pub use ::spacetimedsl::error;
            pub use ::spacetimedsl::itertools;
            // Flat re-exports so that `spacetimedsl::X` paths (which in user crates resolve to
            // `crate::spacetimedsl::X`) work without needing the sub-module prefix.
            pub use ::spacetimedsl::delete::{
                DeletionResult, DeletionResultEntry, OnDeleteStrategy,
            };
            pub use ::spacetimedsl::error::{ReferenceIntegrityViolationError, SpacetimeDSLError};

            pub mod prelude {
                pub use super::{DSL, DSLMethodHooks, ReadOnlyDSL, dsl, read_only_dsl};
                pub use ::spacetimedsl::Context;
                pub use ::spacetimedsl::ReadContext;
                pub use ::spacetimedsl::WriteContext;

                pub use ::spacetimedsl::delete::{
                    DeletionResult, DeletionResultEntry, OnDeleteStrategy,
                };

                pub use ::spacetimedsl::error::{
                    ReferenceIntegrityViolationError, SpacetimeDSLError,
                };

                pub use ::spacetimedsl::Wrapper;

                pub use ::spacetimedsl::get_auth::GetAuth;
                pub use ::spacetimedsl::get_connection_id::GetConnectionId;
                pub use ::spacetimedsl::get_immutable_database::GetImmutableDatabase;
                pub use ::spacetimedsl::get_module_identity::GetModuleIdentity;
                pub use ::spacetimedsl::get_mutable_database::GetMutableDatabase;
                pub use ::spacetimedsl::get_random::GetRandom;
                pub use ::spacetimedsl::get_random_number_generator::GetRandomNumberGenerator;
                pub use ::spacetimedsl::get_sender::GetSender;
                pub use ::spacetimedsl::get_timestamp::GetTimestamp;

                pub use ::spacetimedsl::as_anonymous_view_context::AsAnonymousViewContext;
                pub use ::spacetimedsl::as_reducer_context::AsReducerContext;
                pub use ::spacetimedsl::as_view_context::AsViewContext;

                pub use ::spacetimedsl::itertools::Itertools;

                pub use spacetimedb::{
                    AnonymousViewContext, Identity, ProcedureContext, ReducerContext, ScheduleAt,
                    SpacetimeType, Table, TimeDuration, Timestamp, ViewContext, rand::Rng,
                };
            }
        }
    };
}