Skip to main content

ferripfs_blockstore/
lib.rs

1// Ported from: kubo/boxo/blockstore/blockstore.go
2// Kubo version: v0.39.0
3// Original: https://github.com/ipfs/kubo/blob/v0.39.0/boxo/blockstore/blockstore.go
4//
5// Original work: Copyright (c) Protocol Labs, Inc.
6// Port: Copyright (c) 2026 ferripfs contributors
7// SPDX-License-Identifier: MIT OR Apache-2.0
8
9//! Blockstore implementation for ferripfs, ported from Kubo's boxo/blockstore.
10//!
11//! This module provides:
12//! - Block type representing data with a CID
13//! - CID utilities for creating and parsing content identifiers
14//! - Blockstore trait for block storage operations
15//! - FlatFS-backed blockstore implementation
16//! - Caching blockstore wrapper
17
18mod block;
19mod caching;
20mod cid_utils;
21mod flatfs;
22mod gc;
23mod traits;
24
25pub use block::*;
26pub use caching::*;
27pub use cid_utils::*;
28pub use flatfs::*;
29pub use gc::*;
30pub use traits::*;
31
32use thiserror::Error;
33
34/// Blockstore error type
35#[derive(Debug, Error)]
36pub enum BlockstoreError {
37    #[error("Block not found: {0}")]
38    NotFound(String),
39
40    #[error("Invalid CID: {0}")]
41    InvalidCid(String),
42
43    #[error("Invalid multihash type: {0}")]
44    InvalidMultihash(String),
45
46    #[error("Hash mismatch: expected {expected}, got {actual}")]
47    HashMismatch { expected: String, actual: String },
48
49    #[error("IO error: {0}")]
50    Io(#[from] std::io::Error),
51
52    #[error("Repository error: {0}")]
53    Repo(#[from] ferripfs_repo::RepoError),
54
55    #[error("CID error: {0}")]
56    Cid(String),
57
58    #[error("GC blocked: {0}")]
59    GcBlocked(String),
60}
61
62/// Result type for blockstore operations
63pub type BlockstoreResult<T> = Result<T, BlockstoreError>;
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_error_display() {
71        let err = BlockstoreError::NotFound("QmTest".to_string());
72        assert!(err.to_string().contains("not found"));
73    }
74}