Skip to main content

nectar_primitives/store/
mod.rs

1//! Chunk storage traits and implementations.
2//!
3//! `ChunkGet`, `ChunkPut`, and `ChunkHas` are async and carry `MaybeSend`/
4//! `MaybeSync` bounds so a store may be `!Send` on wasm.
5
6mod maybe_send;
7mod memory;
8mod retry;
9mod typed;
10
11pub use maybe_send::{MaybeSend, MaybeSync};
12pub use memory::MemoryStore;
13pub use retry::{RetryConfig, RetryingChunkGet, Sleeper};
14pub use typed::{ChunkGet, ChunkHas, ChunkPut};
15
16use crate::bmt::DEFAULT_BODY_SIZE;
17use crate::chunk::{AnyChunk, ChunkAddress};
18
19/// Errors from chunk storage operations.
20#[non_exhaustive]
21#[derive(Debug, thiserror::Error)]
22pub enum ChunkStoreError {
23    /// Chunk not found at the given address.
24    #[error("chunk not found: {0}")]
25    NotFound(ChunkAddress),
26    /// Catch-all for backend-specific errors.
27    #[error("{0}")]
28    Other(#[source] Box<dyn std::error::Error + Send + Sync>),
29}
30
31impl ChunkStoreError {
32    /// Create a `NotFound` error for the given address.
33    pub const fn not_found(address: &ChunkAddress) -> Self {
34        Self::NotFound(*address)
35    }
36}
37
38/// A no-op loader that always returns [`ChunkStoreError::NotFound`].
39///
40/// Used by `Node`'s public convenience methods to satisfy the generic
41/// constraint without requiring callers to specify a store type.
42#[derive(Debug)]
43pub struct NullLoader<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>;
44
45impl<const BODY_SIZE: usize> ChunkGet<BODY_SIZE> for NullLoader<BODY_SIZE> {
46    type Error = ChunkStoreError;
47
48    async fn get(&self, address: &ChunkAddress) -> Result<AnyChunk<BODY_SIZE>, Self::Error> {
49        Err(ChunkStoreError::not_found(address))
50    }
51}