rust_helia/lib.rs
1//! # Helia
2//!
3//! An implementation of IPFS in Rust
4//!
5//! This crate provides a `create_helia` function that returns an object implementing
6//! the [`Helia`] trait. Pass it to other modules like `helia-unixfs` to make files
7//! available on the distributed web.
8//!
9//! ## Example
10//!
11//! ```rust
12//! use rust_helia::create_helia;
13//! use cid::Cid;
14//!
15//! #[tokio::main]
16//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
17//! let helia = create_helia(None).await?;
18//!
19//! // Use helia with other modules
20//! // let fs = helia_unixfs::create_unixfs(helia);
21//! // fs.cat(&cid).await?;
22//!
23//! Ok(())
24//! }
25//! ```
26
27use helia_utils::{HeliaConfig, HeliaImpl};
28
29pub use helia_interface::*;
30pub use helia_utils::{
31 create_swarm, create_swarm_with_keypair, BlockstoreConfig, DatastoreConfig, LoggerConfig,
32};
33
34/// Create a new Helia node with the given configuration
35///
36/// If no configuration is provided, sensible defaults will be used.
37pub async fn create_helia(config: Option<HeliaConfig>) -> Result<HeliaImpl, HeliaError> {
38 let config = config.unwrap_or_default();
39 let helia = HeliaImpl::new(config).await?;
40 Ok(helia)
41}
42
43/// Create a new Helia node with default configuration
44pub async fn create_helia_default() -> Result<HeliaImpl, HeliaError> {
45 create_helia(None).await
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[tokio::test]
53 async fn test_create_helia_default() {
54 let helia = create_helia_default().await;
55 assert!(helia.is_ok());
56 }
57
58 #[tokio::test]
59 async fn test_create_helia_with_config() {
60 let config = HeliaConfig::default();
61 let helia = create_helia(Some(config)).await;
62 assert!(helia.is_ok());
63 }
64}