read.rs

Overview

This file defines abstractions and error handling for reading data from durable storage in a generic and type-safe manner. It provides an enumeration for load errors (LoadErr) and a trait (DurableStorageRead) that specifies an interface for reading messages identified by keys from a persistent store. The trait supports operations to load an individual message, retrieve the next message key in sequence, and obtain a batch of messages starting from a given key.

This design facilitates implementing different storage backends while maintaining a consistent API for reading stored messages. The generic parameters allow flexibility in the types used for message keys and messages themselves.


Enumerations

LoadErr

#[derive(Clone, Debug)]
pub enum LoadErr {
    NoState,
    DeserializationError,
}

Description

LoadErr defines common error cases encountered when attempting to load data from durable storage:

Usage

This enum is used as a standard error representation for loading operations, allowing implementations to map their specific errors to these generic categories.


Traits

DurableStorageRead<MessageKey, Message>

pub trait DurableStorageRead<MessageKey, Message> {
    type LoadError: Into<LoadErr> + Clone;

    fn load_message(&self, key: &MessageKey) -> Result<Message, Self::LoadError>;
    fn next(&self, key: &MessageKey) -> Result<Option<MessageKey>, Self::LoadError>;
    fn remaining_messages(
        &self,
        starting_key: &MessageKey,
        limit: usize,
    ) -> Result<Vec<Message>, Self::LoadError>;
}

Description

This trait defines a generic interface for reading messages from durable storage:

Methods

load_message
fn load_message(&self, key: &MessageKey) -> Result<Message, Self::LoadError>;
let message = storage.load_message(&some_key)?;
next
fn next(&self, key: &MessageKey) -> Result<Option<MessageKey>, Self::LoadError>;
if let Some(next_key) = storage.next(&current_key)? {
    // process next_key
}
remaining_messages
fn remaining_messages(
    &self,
    starting_key: &MessageKey,
    limit: usize,
) -> Result<Vec<Message>, Self::LoadError>;
let batch = storage.remaining_messages(&start_key, 10)?;

Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram

classDiagram
class LoadErr {
<<enum>>
+NoState
+DeserializationError
}
class DurableStorageRead~MessageKey, Message~ {
<<trait>>
+LoadError: Into<LoadErr> + Clone
+load_message(key: &MessageKey) Result<Message, LoadError>
+next(key: &MessageKey) Result<Option<MessageKey>, LoadError>
+remaining_messages(starting_key: &MessageKey, limit: usize) Result<Vec<Message>, LoadError>
}