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