helia_utils/
lib.rs

1//! # Helia Utils
2//!
3//! Shared utilities and implementations for the Helia IPFS implementation.
4//!
5//! This crate provides concrete implementations of the traits defined in `helia-interface`,
6//! including the main `Helia` struct, blockstore implementations, and utility functions.
7
8pub mod blockstore;
9pub mod blockstore_with_bitswap;
10pub mod datastore;
11pub mod helia;
12pub mod libp2p_behaviour;
13pub mod logger;
14pub mod metrics;
15
16#[cfg(test)]
17mod blockstore_tests;
18
19#[cfg(test)]
20mod pins_tests;
21
22use std::sync::Arc;
23
24pub use blockstore::SledBlockstore;
25pub use blockstore_with_bitswap::BlockstoreWithBitswap;
26pub use datastore::SledDatastore;
27pub use helia::{DummyRouting, HeliaImpl, SimplePins};
28pub use libp2p_behaviour::{create_swarm, create_swarm_with_keypair, HeliaBehaviour};
29pub use logger::TracingLogger;
30pub use metrics::SimpleMetrics;
31
32use libp2p::Swarm;
33use tokio::sync::Mutex;
34
35// Re-export interface types for convenience
36pub use helia_interface::*;
37
38/// Configuration for creating a new Helia node
39pub struct HeliaConfig {
40    /// The libp2p swarm instance (wrapped in Arc<Mutex<>> for thread safety)
41    pub libp2p: Option<Arc<Mutex<Swarm<HeliaBehaviour>>>>,
42    /// Datastore configuration
43    pub datastore: DatastoreConfig,
44    /// Blockstore configuration
45    pub blockstore: BlockstoreConfig,
46    /// DNS resolver configuration
47    pub dns: Option<trust_dns_resolver::TokioAsyncResolver>,
48    /// Logger configuration
49    pub logger: LoggerConfig,
50    /// Metrics configuration
51    pub metrics: Option<Arc<dyn Metrics>>,
52}
53
54impl std::fmt::Debug for HeliaConfig {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        f.debug_struct("HeliaConfig")
57            .field("libp2p", &self.libp2p.as_ref().map(|_| "Some(Swarm)"))
58            .field("datastore", &self.datastore)
59            .field("blockstore", &self.blockstore)
60            .field("dns", &self.dns.as_ref().map(|_| "Some(resolver)"))
61            .field("logger", &self.logger)
62            .field("metrics", &self.metrics.as_ref().map(|_| "Some(metrics)"))
63            .finish()
64    }
65}
66
67impl Default for HeliaConfig {
68    fn default() -> Self {
69        Self {
70            libp2p: None,
71            datastore: DatastoreConfig::default(),
72            blockstore: BlockstoreConfig::default(),
73            dns: None,
74            logger: LoggerConfig::default(),
75            metrics: None,
76        }
77    }
78}
79
80/// Configuration for the datastore
81#[derive(Debug, Clone)]
82pub struct DatastoreConfig {
83    /// Path to the datastore directory
84    pub path: Option<std::path::PathBuf>,
85    /// Whether to create the datastore if it doesn't exist
86    pub create_if_missing: bool,
87}
88
89impl Default for DatastoreConfig {
90    fn default() -> Self {
91        Self {
92            path: None,
93            create_if_missing: true,
94        }
95    }
96}
97
98/// Configuration for the blockstore
99#[derive(Debug, Clone)]
100pub struct BlockstoreConfig {
101    /// Path to the blockstore directory
102    pub path: Option<std::path::PathBuf>,
103    /// Whether to create the blockstore if it doesn't exist
104    pub create_if_missing: bool,
105}
106
107impl Default for BlockstoreConfig {
108    fn default() -> Self {
109        Self {
110            path: None,
111            create_if_missing: true,
112        }
113    }
114}
115
116/// Configuration for the logger
117#[derive(Debug, Clone)]
118pub struct LoggerConfig {
119    /// Log level
120    pub level: tracing::Level,
121    /// Whether to include timestamps
122    pub include_timestamps: bool,
123}
124
125impl Default for LoggerConfig {
126    fn default() -> Self {
127        Self {
128            level: tracing::Level::INFO,
129            include_timestamps: true,
130        }
131    }
132}