Struct initiative_core::storage::repository::Repository

source ·
pub struct Repository {
    data_store: Box<dyn DataStore>,
    data_store_enabled: bool,
    recent: VecDeque<Thing>,
    redo_change: Option<Change>,
    undo_history: VecDeque<Change>,
}

Fields§

§data_store: Box<dyn DataStore>§data_store_enabled: bool§recent: VecDeque<Thing>§redo_change: Option<Change>§undo_history: VecDeque<Change>

Implementations§

source§

impl Repository

source

pub fn new(data_store: impl DataStore + 'static) -> Self

source

pub async fn init(&mut self)

The data store will not necessarily be available at construct, so we need to check if it’s healthy or discard it and fall back on a memory data store instead.

source

pub async fn get_by_change(&self, change: &Change) -> Result<Record, Error>

Get the record associated with a given change, if available.

source

pub async fn load_relations( &self, thing: &Thing, ) -> Result<ThingRelations, Error>

Load child and grandchild relations associated with a Thing (eg. location).

source

pub async fn get_by_name_start( &self, name: &str, limit: Option<usize>, ) -> Result<Vec<Record>, Error>

Get all saved and recent Things beginning with a given (case-insensitive) string, up to an optional limit.

source

pub fn recent(&self) -> impl Iterator<Item = &Thing>

Get an iterator over all recent Things.

source

pub async fn journal(&self) -> Result<Vec<Thing>, Error>

Get all Things contained in the journal. This could get heavy, so should not be used lightly.

source

pub async fn get_by_name(&self, name: &str) -> Result<Record, Error>

Get the Thing from saved or recent with a given name. (There should be only one.)

source

pub async fn get_by_uuid(&self, uuid: &Uuid) -> Result<Record, Error>

Get the Thing from saved or recent with a given UUID. (There should be only one.)

source

pub async fn modify( &mut self, change: Change, ) -> Result<Option<Record>, (Change, Error)>

Apply a given Change, returning the affected Thing (with modifications applied) on success, or a tuple of the Change and Error message on failure.

source

pub async fn undo(&mut self) -> Option<Result<Option<Record>, Error>>

Undo the most recent Change. Returns None if the undo history is empty; otherwise returns the Result of the modify() operation.

source

pub fn undo_history(&self) -> impl Iterator<Item = &Change>

Get an iterator over the Changes currently queued up in the undo history, from newest to oldest.

source

pub async fn redo(&mut self) -> Option<Result<Option<Record>, Error>>

Redo the most recently undid Change. Returns None if no such change exists; otherwise returns the result of the modify() operation. This differs from undo() in that only one Change is stored in history at a time.

source

pub fn get_redo(&self) -> Option<&Change>

Get the Change currently queued up for redo(), if any.

source

pub async fn modify_without_undo( &mut self, change: Change, ) -> Result<Change, (Change, Error)>

Apply a Change to the Repository without adding the Change to the undo history. Returns the reverse operation on success (what would be otherwise inserted into the undo history), or a tuple of the failed Change and error message on failure.

source

pub async fn get_key_value(&self, key: &KeyValue) -> Result<KeyValue, Error>

Get a value from the key-value store.

source

pub fn data_store_enabled(&self) -> bool

Is the data store currently enabled? Returns false if init() has not yet been called.

source

async fn set_key_value( &mut self, key_value: &KeyValue, ) -> Result<KeyValue, Error>

Set a value in the key-value store.

Publicly this is done using modify() with Change::SetKeyValue.

source

fn push_recent(&mut self, thing: Thing)

Add a Thing to the recent list.

source

fn take_recent<F>(&mut self, f: F) -> Option<Thing>
where F: Fn(&Thing) -> bool,

Remove the latest Thing in the recent list, returning it if one is present.

source

async fn create_thing( &mut self, thing_data: ThingData, uuid: Option<Uuid>, ) -> Result<Uuid, (ThingData, Error)>

Create a Thing, pushing it onto the recent list.

Publicly this is invoked using modify() with Change::Create.

source

async fn create_and_save_thing( &mut self, thing_data: ThingData, uuid: Option<Uuid>, ) -> Result<Thing, (ThingData, Error)>

Create a Thing and save it directly to the journal.

Publicly this is invoked using modify() with Change::CreateAndSave.

source

async fn delete_thing_by_uuid( &mut self, uuid: &Uuid, ) -> Result<Record, (Option<Record>, Error)>

Delete a Thing from recent or journal by its UUID.

Publicly this is invoked using modify() with Change::Delete.

source

async fn save_thing_by_name(&mut self, name: &String) -> Result<Thing, Error>

Transfer a Thing from recent to the journal, referenced by its name. Returns the Thing transferred, or an error on failure.

Publicly this is invoked using modify() with Change::Save { uuid: None, .. }

source

async fn save_thing_by_uuid(&mut self, uuid: &Uuid) -> Result<Thing, Error>

Transfer a Thing from recent to the journal, referenced by its UUID. Returns the Thing transferred, or an error on failure.

Publicly this is invoked using modify() with Change::Save { uuid: Some(_), .. }

source

async fn save_thing(&mut self, thing: &Thing) -> Result<(), Error>

Write a Thing to the data store.

source

async fn unsave_thing_by_uuid( &mut self, uuid: &Uuid, ) -> Result<String, (Option<String>, Error)>

Remove a Thing from the data store and add it to the recent list instead. Returns the name of the Thing transferred on success, or a tuple of the optional name and an error on failure. This is asymmetric with save_thing_by_* because writing to the recent list takes ownership while writing to the data store accepts a reference, so returning a Thing here would require an unnecessary clone() call when all we really want is the name of the Thing we just unsaved.

Publicly this is invoked using modify() with Change::Unsave.

source

async fn edit_thing_by_name( &mut self, name: &String, diff: ThingData, ) -> Result<(Record, String), (Option<Record>, ThingData, Error)>

Apply a diff to a Thing matched by name. See edit_thing() for details.

Publicly this is invoked using modify() with Change::Edit { uuid: None, .. }.

source

async fn edit_thing_by_uuid( &mut self, uuid: &Uuid, diff: ThingData, ) -> Result<(Record, String), (Option<Record>, ThingData, Error)>

Apply a diff to a Thing matched by UUID. See edit_thing() for details.

Publicly this is invoked using modify() with Change::Edit { uuid: Some(_), .. }.

source

async fn edit_thing( &mut self, record: Record, diff: ThingData, ) -> Result<(Record, String), (Record, ThingData, Error)>

Apply a diff to a given Record. Returns a tuple consisting of a Record containing the modified fields and the matched Thing’s actual name on success, or a tuple consisting of an optional Record of the matched Thing, the attempted diff, and an error message on failure. Note that the successful response includes only the old values of any modified fields, so re-applying the diff will revert the Thing back to its original state.

Supports the edit_thing_by_* functions.

source

async fn thing_data_into_thing( &self, thing_data: ThingData, uuid: Option<Uuid>, ) -> Result<Thing, (ThingData, Error)>

Creates a new Thing from its data, generating a UUID if necessary and checking for name/UUID conflicts.

Trait Implementations§

source§

impl Debug for Repository

source§

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

Formats the value using the given formatter. Read more

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> 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, 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V