cido 0.2.0

Core traits and implementations for indexing with cido
Documentation
pub use context::*;

#[cido_macros::_impls]
mod context {
  use crate::prelude::*;

  /// Allows synchronizing code within the preprocessing context so that event ordering can be maintained
  pub struct PreprocessingQueueRegister<'a, C: Cidomap> {
    inner: std::marker::PhantomData<&'a C>,
  }

  impl<'a, C: Cidomap> PreprocessingQueueRegister<'a, C> {
    /// Wait for the order in line that this was created in
    pub async fn wait(&mut self) -> usize {
      unimplemented!()
    }
    /// Mark all synchronization needs finished and let the next one execute
    pub fn finish(self) {}
  }

  impl<'a, C: Cidomap> Drop for PreprocessingQueueRegister<'a, C> {
    fn drop(&mut self) {}
  }

  /// Allows making network calls before entering the generator or handler functions
  ///
  /// The generator and handler functions are all executed sequentially so any waiting done for network
  /// calls is expensive. This struct is used in the preprocessor functions which are all executed
  /// simulatenously so that all waiting can happen concurrently
  ///
  /// This struct is passed to the preprocessor handler
  #[allow(clippy::module_name_repetitions)]
  pub struct PreprocessingContext<C: Cidomap> {
    inner: std::marker::PhantomData<std::sync::Arc<C::Network>>,
  }

  impl<C: Cidomap> PreprocessingContext<C> {
    /// Get access to the network for making calls
    pub fn network(&self) -> &<C as Cidomap>::Network {
      unimplemented!()
    }

    /// Registers with a queue that guarantees single order processing. This is useful
    /// when some order needs to be enforced to avoid race conditions that can otherwise
    /// arise from running all futures concurrently.
    ///
    /// Must be called in synchronous code before the async future is created. This is because the
    /// order that this function is called matters and creating the futures happens separately from
    /// polling them.
    ///
    /// ```rust,ignore
    /// async fn preprocess_event(cx: &Preprocessor<Cidomap>) -> impl Future<Output = Result<(), Cidomap::Error> {
    ///    let wait = cs.register_serial_queue();
    ///     async move {
    ///         let _place_in_line = wait.wait().await;
    ///         // do some processing
    ///         wait.finish();
    ///         // finish processing
    ///     }
    /// }
    /// ```
    ///
    /// All futures that register can await the returned value to get their place in line
    /// when the register has `finish` called then the next one in line will be signaled and allowed
    /// to execute.
    pub fn register_serial_queue(&self) -> PreprocessingQueueRegister<'_, C> {
      unimplemented!()
    }
  }

  impl<C: Cidomap> Clone for PreprocessingContext<C> {
    fn clone(&self) -> Self {
      unimplemented!()
    }
  }

  /// Allows making new filter sources
  ///
  /// This struct is passed to the generator handler
  #[allow(clippy::module_name_repetitions)]
  pub struct GeneratorContext<'a, C: Cidomap> {
    inner: std::marker::PhantomData<&'a mut std::cell::RefCell<C>>,
  }

  impl<'a, C: Cidomap> GeneratorContext<'a, C> {
    /// Gives a reference to the network if any calls need to be made
    ///
    /// If possible this should only be used in preprocessing and the results can be
    /// cached and forwarded to the event handler so that no time is spent waiting
    pub fn network(&self) -> &<C as Cidomap>::Network {
      unimplemented!()
    }

    /// Create a new source
    pub async fn create_source<F: ProcessingOrderFilter<Network = <C as Cidomap>::Network>>(
      &mut self,
      p: F::Param,
    ) -> Result<(), CidomapError> {
      let _ = p;
      Ok(())
    }
  }

  /// Allows interacting with the database to load and save entities and events
  ///
  /// This struct is passed to the final handler
  pub struct Context<'a, C: Cidomap> {
    inner: std::marker::PhantomData<&'a mut std::cell::RefCell<C>>,
  }

  impl<'a, C: Cidomap> Context<'a, C> {
    /// Load a mutable entity or event and return None if it doesn't exist.
    ///
    /// If you will be creating an entity or event if it doesn't exist, use `load_mut_or_else` as it
    /// is more ergonomic and requires only a single cache lookup
    ///
    /// This function can be unwrapped because any errors returned are not recoverable by handler code
    pub async fn load_mut<E>(
      &self,
      id: impl CacheKey<E>,
    ) -> Result<Option<RefMut<'_, E>>, CidomapError>
    where
      E: Transformer<Cidomap = C>,
    {
      let _ = id;
      unimplemented!()
    }

    /// Load an immutable entity or event and return  None if it doesn't exist.
    ///
    /// This function can be unwrapped because any errors returned are not recoverable by handler code
    pub async fn load<E>(&'a self, id: impl CacheKey<E>) -> Result<Option<Ref<'a, E>>, CidomapError>
    where
      E: Transformer<Cidomap = C>,
    {
      let _ = id;
      unimplemented!()
    }

    /// Load a mutable entity or event or else execute an `AsyncFnOnce(id: Id)` if it doesn't exist
    ///
    /// This function can be unwrapped because any errors returned are not recoverable by handler code
    pub async fn load_mut_or_else<E>(
      &'a self,
      id: impl CacheKey<E>,
      f: impl AsyncFnOnce(<E as Identifiable>::Id) -> Result<E, C::Error>,
    ) -> Result<RefMut<'a, E>, CidomapError>
    where
      E: Transformer<Cidomap = C>,
    {
      let _ = id;
      let _ = f;
      unimplemented!()
    }

    /// Saves a new entity.
    ///
    /// # Errors
    /// This function will return an error if the entity being saved is already cached.
    pub async fn save<E>(&self, entity: E) -> Result<(), CidomapError>
    where
      E: Transformer<Cidomap = C>,
    {
      let _ = entity;
      Ok(())
    }

    /// Query all instances of a type
    ///
    /// This is generally used in the `create` function on the Cidomap trait
    ///
    /// This function can be unwrapped because any errors returned are not recoverable by handler code
    pub async fn dynamic_query<D>(&self, d: &mut D) -> Result<(), CidomapError>
    where
      D: Query<Cidomap = C>,
    {
      let _ = d;
      Ok(())
    }

    /// Returns a references to the network so that calls can be made if necessary
    ///
    /// If possible this should only be used in preprocessing and the results can be
    /// cached and forwarded to the event handler so that no time is spent waiting
    pub fn network(&self) -> &<C as Cidomap>::Network {
      unimplemented!()
    }
  }
}