1pub mod blocks;
18pub mod errors;
19pub mod pins;
20pub mod routing;
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 errors::*;
38pub use pins::*;
39pub use routing::*;
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(
78 "on_progress",
79 &self.on_progress.as_ref().map(|_| "Some(closure)"),
80 )
81 .finish()
82 }
83}
84
85impl<T> Default for ProgressOptions<T> {
86 fn default() -> Self {
87 Self { on_progress: None }
88 }
89}
90
91impl<T> Clone for ProgressOptions<T> {
92 fn clone(&self) -> Self {
93 Self::default()
95 }
96}
97
98#[async_trait]
100pub trait CodecLoader: Send + Sync {
101 async fn load_codec(&self, code: u64) -> Result<Box<dyn Codec>, HeliaError>;
103}
104
105#[async_trait]
107pub trait HasherLoader: Send + Sync {
108 async fn load_hasher(&self, code: u64) -> Result<Box<dyn Hasher>, HeliaError>;
110}
111
112#[async_trait]
114pub trait Codec: Send + Sync {
115 async fn encode(&self, data: &[u8]) -> Result<Bytes, HeliaError>;
117
118 async fn decode(&self, data: &[u8]) -> Result<Bytes, HeliaError>;
120
121 fn code(&self) -> u64;
123}
124
125#[async_trait]
127pub trait Hasher: Send + Sync {
128 async fn hash(&self, data: &[u8]) -> Result<multihash::Multihash<64>, HeliaError>;
130
131 fn code(&self) -> u64;
133}
134
135#[derive(Debug, Clone)]
137pub enum HeliaEvent {
138 Start,
140 Stop,
142}
143
144#[derive(Debug)]
146pub struct GcOptions {
147 pub abort: AbortOptions,
149 pub progress: ProgressOptions<GcEvent>,
151}
152
153impl Default for GcOptions {
154 fn default() -> Self {
155 Self {
156 abort: AbortOptions::default(),
157 progress: ProgressOptions::default(),
158 }
159 }
160}
161
162impl Clone for GcOptions {
163 fn clone(&self) -> Self {
164 Self {
165 abort: self.abort.clone(),
166 progress: self.progress.clone(),
167 }
168 }
169}
170
171#[derive(Debug, Clone, Serialize, Deserialize)]
173pub enum GcEvent {
174 Deleted(Cid),
176 Error(String),
178}
179
180pub trait ComponentLogger: Send + Sync {
182 fn debug(&self, message: &str);
184 fn info(&self, message: &str);
186 fn warn(&self, message: &str);
188 fn error(&self, message: &str);
190}
191
192#[async_trait]
194pub trait Metrics: Send + Sync {
195 async fn record_counter(&self, name: &str, value: u64, labels: HashMap<String, String>);
197
198 async fn record_gauge(&self, name: &str, value: f64, labels: HashMap<String, String>);
200
201 async fn record_histogram(&self, name: &str, value: f64, labels: HashMap<String, String>);
203}
204
205#[async_trait]
207pub trait Helia: Send + Sync {
208 fn blockstore(&self) -> &dyn Blocks;
210
211 fn datastore(&self) -> &dyn Datastore;
213
214 fn pins(&self) -> &dyn Pins;
216
217 fn logger(&self) -> &dyn ComponentLogger;
219
220 fn routing(&self) -> &dyn Routing;
222
223 fn dns(&self) -> &TokioAsyncResolver;
225
226 fn metrics(&self) -> Option<&dyn Metrics>;
228
229 async fn start(&self) -> Result<(), HeliaError>;
231
232 async fn stop(&self) -> Result<(), HeliaError>;
234
235 async fn gc(&self, options: Option<GcOptions>) -> Result<(), HeliaError>;
237
238 async fn get_codec(&self, code: u64) -> Result<Box<dyn Codec>, HeliaError>;
240
241 async fn get_hasher(&self, code: u64) -> Result<Box<dyn Hasher>, HeliaError>;
243}
244
245#[async_trait]
247pub trait HeliaWithLibp2p<T>: Helia
248where
249 T: libp2p::swarm::NetworkBehaviour + Send + 'static,
250{
251 fn libp2p(&self) -> Arc<Mutex<Swarm<T>>>;
253}
254
255#[async_trait]
257pub trait Datastore: Send + Sync {
258 async fn get(&self, key: &[u8]) -> Result<Option<Bytes>, HeliaError>;
260
261 async fn put(&self, key: &[u8], value: Bytes) -> Result<(), HeliaError>;
263
264 async fn delete(&self, key: &[u8]) -> Result<(), HeliaError>;
266
267 async fn has(&self, key: &[u8]) -> Result<bool, HeliaError>;
269
270 async fn query(&self, prefix: Option<&[u8]>) -> Result<AwaitIterable<Bytes>, HeliaError>;
272}