bitcoind_cache/store/
mod.rs

1use std::{future::Future, pin::Pin};
2
3#[cfg(feature = "fs")]
4use std::io;
5
6#[cfg(feature = "fs")]
7pub mod filesystem;
8#[cfg(feature = "http")]
9pub mod http;
10#[cfg(feature = "r2")]
11pub mod r2;
12
13#[cfg(feature = "fs")]
14pub use filesystem::FileStore;
15#[cfg(feature = "http")]
16pub use http::HttpStore;
17#[cfg(feature = "r2")]
18pub use r2::R2Store;
19
20#[derive(Clone)]
21pub enum AnyStore {
22    #[cfg(feature = "fs")]
23    File(FileStore),
24    #[cfg(feature = "http")]
25    HTTP(HttpStore),
26    #[cfg(feature = "r2")]
27    R2(R2Store),
28}
29
30impl Store for AnyStore {
31    fn put_object<'a>(&'a self, filename: String, content: &'a [u8]) -> AsyncStoreResult<()> {
32        match self {
33            #[cfg(feature = "fs")]
34            AnyStore::File(store) => store.put_object(filename, content),
35            #[cfg(feature = "http")]
36            AnyStore::HTTP(store) => store.put_object(filename, content),
37            #[cfg(feature = "r2")]
38            AnyStore::R2(store) => store.put_object(filename, content),
39        }
40    }
41
42    fn get_object(&self, filename: String) -> AsyncStoreResult<Option<Vec<u8>>> {
43        match self {
44            #[cfg(feature = "fs")]
45            AnyStore::File(store) => store.get_object(filename),
46            #[cfg(feature = "http")]
47            AnyStore::HTTP(store) => store.get_object(filename),
48            #[cfg(feature = "r2")]
49            AnyStore::R2(store) => store.get_object(filename),
50        }
51    }
52}
53
54#[derive(Debug)]
55pub enum StoreError {
56    #[cfg(feature = "fs")]
57    Io(io::Error),
58    #[cfg(feature = "http")]
59    Reqwest(reqwest::Error),
60    #[cfg(feature = "r2")]
61    R2(s3::error::S3Error),
62}
63
64pub type AsyncStoreResult<'a, T> = Pin<Box<dyn Future<Output = Result<T, StoreError>> + 'a + Send>>;
65
66pub trait Store {
67    fn put_object<'a>(&'a self, filename: String, content: &'a [u8]) -> AsyncStoreResult<'a, ()>;
68    fn get_object(&self, filename: String) -> AsyncStoreResult<Option<Vec<u8>>>;
69}