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