Struct ModuleDef

Source
#[non_exhaustive]
pub struct ModuleDef { /* private fields */ }
Expand description

A validated, canonicalized, immutable module definition.

Cannot be created directly. Instead, create/deserialize a spacetimedb_lib::RawModuleDef and call ModuleDef::try_from.

use spacetimedb_lib::RawModuleDef;
use spacetimedb_schema::def::{ModuleDef, TableDef, IndexDef, TypeDef, ModuleDefLookup, ScopedTypeName};
use spacetimedb_schema::identifier::Identifier;

fn read_raw_module_def_from_file() -> RawModuleDef {
    // ...
}

let raw_module_def = read_raw_module_def_from_file();
let module_def = ModuleDef::try_from(raw_module_def).expect("valid module def");

let table_name = Identifier::new("my_table".into()).expect("valid table name");
let index_name = "my_table_my_column_idx_btree";
let scoped_type_name = ScopedTypeName::try_new([], "MyType").expect("valid scoped type name");

let table: Option<&TableDef> = module_def.lookup(&table_name);
let index: Option<&IndexDef> = module_def.lookup(index_name);
let type_def: Option<&TypeDef> = module_def.lookup(&scoped_type_name);
// etc.

Author’s apology: If you find yourself asking: “Why are we using strings to look up everything here, rather than integer indexes?” The answer is “I tried to get rid of the strings, but people thought it would be too confusing to have multiple kinds of integer index.” Because the system tables and stuff would be using a different sort of integer index. shrug emoji.

Implementations§

Source§

impl ModuleDef

Source

pub fn tables(&self) -> impl Iterator<Item = &TableDef>

The tables of the module definition.

Source

pub fn indexes(&self) -> impl Iterator<Item = &IndexDef>

The indexes of the module definition.

Source

pub fn constraints(&self) -> impl Iterator<Item = &ConstraintDef>

The constraints of the module definition.

Source

pub fn sequences(&self) -> impl Iterator<Item = &SequenceDef>

The sequences of the module definition.

Source

pub fn schedules(&self) -> impl Iterator<Item = &ScheduleDef>

The schedules of the module definition.

Source

pub fn reducers(&self) -> impl Iterator<Item = &ReducerDef>

The reducers of the module definition.

Source

pub fn types(&self) -> impl Iterator<Item = &TypeDef>

The type definitions of the module definition.

Source

pub fn row_level_security( &self, ) -> impl Iterator<Item = &RawRowLevelSecurityDefV9>

The row-level security policies of the module definition.

Source

pub fn typespace(&self) -> &Typespace

The Typespace used by the module.

AlgebraicTypeRefs in the table, reducer, and type alias declarations refer to this typespace.

The typespace must satisfy Typespace::is_valid_for_client_code_generation. That is, all types stored in the typespace must either:

  1. satisfy AlgebraicType::is_valid_for_client_type_definition
  2. and/or AlgebraicType::is_valid_for_client_type_use.

Types satisfying condition 1 correspond to generated classes in client code. (Types satisfying condition 2 are an artifact of the module bindings, and do not affect the semantics of the module definition.)

Types satisfying condition 1 are required to have corresponding RawTypeDefV9 declarations in the module.

Source

pub fn typespace_for_generate(&self) -> &TypespaceForGenerate

The typespace of the module from a different perspective, one useful for client code generation.

Source

pub fn stored_in_table_def(&self, name: &str) -> Option<&TableDef>

The TableDef an entity in the global namespace is stored in, if any.

Generally, you will want to use the lookup method on the entity type instead.

Source

pub fn lookup<T: ModuleDefLookup>(&self, key: T::Key<'_>) -> Option<&T>

Lookup a definition by its key in self.

Source

pub fn lookup_expect<T: ModuleDefLookup>(&self, key: T::Key<'_>) -> &T

Lookup a definition by its key in self, panicking if not found. Only use this method if you are sure the key exists in the module definition.

Source

pub fn table<K: ?Sized + Hash + Equivalent<Identifier>>( &self, name: &K, ) -> Option<&TableDef>

Convenience method to look up a table, possibly by a string.

Source

pub fn reducer<K: ?Sized + Hash + Equivalent<Identifier>>( &self, name: &K, ) -> Option<&ReducerDef>

Convenience method to look up a reducer, possibly by a string.

Source

pub fn reducer_full<K: ?Sized + Hash + Equivalent<Identifier>>( &self, name: &K, ) -> Option<(ReducerId, &ReducerDef)>

Convenience method to look up a reducer, possibly by a string, returning its id as well.

Source

pub fn reducer_by_id(&self, id: ReducerId) -> &ReducerDef

Look up a reducer by its id.

Source

pub fn get_reducer_by_id(&self, id: ReducerId) -> Option<&ReducerDef>

Look up a reducer by its id.

Source

pub fn lifecycle_reducer( &self, lifecycle: Lifecycle, ) -> Option<(ReducerId, &ReducerDef)>

Looks up a lifecycle reducer defined in the module.

Source

pub fn reducer_arg_deserialize_seed<K: ?Sized + Hash + Equivalent<Identifier>>( &self, name: &K, ) -> Option<(ReducerId, ReducerArgsDeserializeSeed<'_>)>

Get a DeserializeSeed that can pull data from a Deserializer and format it into a ProductType at the parameter type of the reducer named name.

Source

pub fn type_def_from_ref( &self, r: AlgebraicTypeRef, ) -> Option<(&ScopedTypeName, &TypeDef)>

Look up the name corresponding to an AlgebraicTypeRef.

Source

pub fn table_schema<K: ?Sized + Hash + Equivalent<Identifier>>( &self, name: &K, table_id: TableId, ) -> Option<TableSchema>

Convenience method to look up a table and convert it to a TableSchema. All indexes, constraints, etc inside the table will have ID 0!

Source

pub fn expect_lookup<T: ModuleDefLookup>(&self, key: T::Key<'_>) -> &T

Lookup a definition by its key in self, panicking if it is not found.

Source

pub fn expect_contains<Def: ModuleDefLookup>(&self, def: &Def)

Expect that this module definition contains a definition.

Trait Implementations§

Source§

impl Clone for ModuleDef

Source§

fn clone(&self) -> ModuleDef

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ModuleDef

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<ModuleDef> for RawModuleDefV9

Source§

fn from(val: ModuleDef) -> Self

Converts to this type from the input type.
Source§

impl TryFrom<RawModuleDef> for ModuleDef

Source§

type Error = ErrorStream<ValidationError>

The type returned in the event of a conversion error.
Source§

fn try_from(raw: RawModuleDef) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<RawModuleDefV8> for ModuleDef

Source§

type Error = ErrorStream<ValidationError>

The type returned in the event of a conversion error.
Source§

fn try_from(v8_mod: RawModuleDefV8) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<RawModuleDefV9> for ModuleDef

Source§

type Error = ErrorStream<ValidationError>

The type returned in the event of a conversion error.
Source§

fn try_from(v9_mod: RawModuleDefV9) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.