braid_core/core/
traits.rs1use crate::core::error::Result;
2use crate::core::{BraidRequest, BraidResponse, Update};
3use async_trait::async_trait;
4use std::future::Future;
5use std::pin::Pin;
6use std::time::Duration;
7
8pub trait BraidRuntime: Send + Sync + 'static {
10 fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>);
11 fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
12 fn now_ms(&self) -> u64;
13}
14
15#[async_trait]
17pub trait BraidNetwork: Send + Sync + 'static {
18 async fn fetch(&self, url: &str, req: BraidRequest) -> Result<BraidResponse>;
19 async fn subscribe(
20 &self,
21 url: &str,
22 req: BraidRequest,
23 ) -> Result<async_channel::Receiver<Result<Update>>>;
24}
25
26#[async_trait]
28pub trait BraidStorage: Send + Sync + 'static {
29 async fn put(&self, key: &str, data: bytes::Bytes, meta: String) -> Result<()>;
30 async fn get(&self, key: &str) -> Result<Option<(bytes::Bytes, String)>>;
31 async fn delete(&self, key: &str) -> Result<()>;
32 async fn list_keys(&self) -> Result<Vec<String>>;
33}
34
35#[cfg(feature = "native")]
36pub struct NativeRuntime;
37
38#[cfg(feature = "native")]
39impl BraidRuntime for NativeRuntime {
40 fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>) {
41 tokio::spawn(future);
42 }
43
44 fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
45 Box::pin(tokio::time::sleep(duration))
46 }
47
48 fn now_ms(&self) -> u64 {
49 std::time::SystemTime::now()
50 .duration_since(std::time::UNIX_EPOCH)
51 .unwrap()
52 .as_millis() as u64
53 }
54}