1pub mod blocks;
18pub mod pins;
19pub mod routing;
20pub mod errors;
21
22use std::collections::HashMap;
23use std::future::Future;
24use std::pin::Pin;
25use std::sync::Arc;
26
27use async_trait::async_trait;
28use bytes::Bytes;
29use cid::Cid;
30use futures::Stream;
31use libp2p::Swarm;
32use serde::{Deserialize, Serialize};
33use tokio::sync::Mutex;
34use trust_dns_resolver::TokioAsyncResolver;
35
36pub use blocks::*;
37pub use pins::*;
38pub use routing::*;
39pub use errors::*;
40
41pub type AwaitIterable<T> = Pin<Box<dyn Stream<Item = T> + Send>>;
43
44pub type Await<T> = Pin<Box<dyn Future<Output = T> + Send>>;
46
47#[derive(Debug, Default)]
49pub struct AbortOptions {
50 }
53
54impl Clone for AbortOptions {
55 fn clone(&self) -> Self {
56 Self::default()
58 }
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct ProgressEvent<T> {
64 pub event_type: String,
65 pub detail: T,
66}
67
68pub struct ProgressOptions<T> {
70 pub on_progress: Option<Box<dyn Fn(ProgressEvent<T>) + Send + Sync>>,
72}
73
74impl<T> std::fmt::Debug for ProgressOptions<T> {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 f.debug_struct("ProgressOptions")
77 .field("on_progress", &self.on_progress.as_ref().map(|_| "Some(closure)"))
78 .finish()
79 }
80}
81
82impl<T> Default for ProgressOptions<T> {
83 fn default() -> Self {
84 Self {
85 on_progress: None,
86 }
87 }
88}
89
90impl<T> Clone for ProgressOptions<T> {
91 fn clone(&self) -> Self {
92 Self::default()
94 }
95}
96
97#[async_trait]
99pub trait CodecLoader: Send + Sync {
100 async fn load_codec(&self, code: u64) -> Result<Box<dyn Codec>, HeliaError>;
102}
103
104#[async_trait]
106pub trait HasherLoader: Send + Sync {
107 async fn load_hasher(&self, code: u64) -> Result<Box<dyn Hasher>, HeliaError>;
109}
110
111#[async_trait]
113pub trait Codec: Send + Sync {
114 async fn encode(&self, data: &[u8]) -> Result<Bytes, HeliaError>;
116
117 async fn decode(&self, data: &[u8]) -> Result<Bytes, HeliaError>;
119
120 fn code(&self) -> u64;
122}
123
124#[async_trait]
126pub trait Hasher: Send + Sync {
127 async fn hash(&self, data: &[u8]) -> Result<multihash::Multihash<64>, HeliaError>;
129
130 fn code(&self) -> u64;
132}
133
134#[derive(Debug, Clone)]
136pub enum HeliaEvent {
137 Start,
139 Stop,
141}
142
143#[derive(Debug)]
145pub struct GcOptions {
146 pub abort: AbortOptions,
148 pub progress: ProgressOptions<GcEvent>,
150}
151
152impl Default for GcOptions {
153 fn default() -> Self {
154 Self {
155 abort: AbortOptions::default(),
156 progress: ProgressOptions::default(),
157 }
158 }
159}
160
161impl Clone for GcOptions {
162 fn clone(&self) -> Self {
163 Self {
164 abort: self.abort.clone(),
165 progress: self.progress.clone(),
166 }
167 }
168}
169
170#[derive(Debug, Clone, Serialize, Deserialize)]
172pub enum GcEvent {
173 Deleted(Cid),
175 Error(String),
177}
178
179pub trait ComponentLogger: Send + Sync {
181 fn debug(&self, message: &str);
183 fn info(&self, message: &str);
185 fn warn(&self, message: &str);
187 fn error(&self, message: &str);
189}
190
191#[async_trait]
193pub trait Metrics: Send + Sync {
194 async fn record_counter(&self, name: &str, value: u64, labels: HashMap<String, String>);
196
197 async fn record_gauge(&self, name: &str, value: f64, labels: HashMap<String, String>);
199
200 async fn record_histogram(&self, name: &str, value: f64, labels: HashMap<String, String>);
202}
203
204#[async_trait]
206pub trait Helia: Send + Sync {
207 fn blockstore(&self) -> &dyn Blocks;
209
210 fn datastore(&self) -> &dyn Datastore;
212
213 fn pins(&self) -> &dyn Pins;
215
216 fn logger(&self) -> &dyn ComponentLogger;
218
219 fn routing(&self) -> &dyn Routing;
221
222 fn dns(&self) -> &TokioAsyncResolver;
224
225 fn metrics(&self) -> Option<&dyn Metrics>;
227
228 async fn start(&self) -> Result<(), HeliaError>;
230
231 async fn stop(&self) -> Result<(), HeliaError>;
233
234 async fn gc(&self, options: Option<GcOptions>) -> Result<(), HeliaError>;
236
237 async fn get_codec(&self, code: u64) -> Result<Box<dyn Codec>, HeliaError>;
239
240 async fn get_hasher(&self, code: u64) -> Result<Box<dyn Hasher>, HeliaError>;
242}
243
244#[async_trait]
246pub trait HeliaWithLibp2p<T>: Helia
247where
248 T: libp2p::swarm::NetworkBehaviour + Send + 'static
249{
250 fn libp2p(&self) -> Arc<Mutex<Swarm<T>>>;
252}
253
254#[async_trait]
256pub trait Datastore: Send + Sync {
257 async fn get(&self, key: &[u8]) -> Result<Option<Bytes>, HeliaError>;
259
260 async fn put(&self, key: &[u8], value: Bytes) -> Result<(), HeliaError>;
262
263 async fn delete(&self, key: &[u8]) -> Result<(), HeliaError>;
265
266 async fn has(&self, key: &[u8]) -> Result<bool, HeliaError>;
268
269 async fn query(&self, prefix: Option<&[u8]>) -> Result<AwaitIterable<Bytes>, HeliaError>;
271}