use std::str::FromStr;
use crate::{MaybeSend, MaybeSync};
use strum::IntoDiscriminant;
pub trait NetabaseDiscriminant:
AsRef<str>
+ Clone
+ Copy
+ std::fmt::Debug
+ std::fmt::Display
+ PartialEq
+ Eq
+ std::hash::Hash
+ strum::IntoEnumIterator
+ MaybeSend
+ MaybeSync
+ 'static
+ FromStr
+ bincode::Encode
+ bincode::Decode<()>
{
}
impl<T> NetabaseDiscriminant for T where
T: AsRef<str>
+ Clone
+ Copy
+ std::fmt::Debug
+ std::fmt::Display
+ PartialEq
+ Eq
+ std::hash::Hash
+ strum::IntoEnumIterator
+ MaybeSend
+ MaybeSync
+ 'static
+ FromStr
+ bincode::Encode
+ bincode::Decode<()>
{
}
pub trait NetabaseKeyDiscriminant:
Clone
+ Copy
+ std::fmt::Debug
+ PartialEq
+ Eq
+ std::hash::Hash
+ MaybeSend
+ MaybeSync
+ 'static
+ bincode::Encode
+ bincode::Decode<()>
{
}
impl<T> NetabaseKeyDiscriminant for T where
T: Clone
+ Copy
+ std::fmt::Debug
+ PartialEq
+ Eq
+ std::hash::Hash
+ MaybeSend
+ MaybeSync
+ 'static
+ bincode::Encode
+ bincode::Decode<()>
{
}
pub trait NetabaseDefinitionTrait:
bincode::Encode
+ bincode::Decode<()>
+ Clone
+ std::fmt::Debug
+ MaybeSend
+ MaybeSync
+ 'static
+ IntoDiscriminant<Discriminant: NetabaseDiscriminant>
{
type Keys: NetabaseDefinitionTraitKey<Discriminant: NetabaseKeyDiscriminant>;
type Tables: Clone + Copy;
type SubscriptionManager: Clone;
fn tables() -> Self::Tables;
fn subscription_manager() -> Self::SubscriptionManager;
fn discriminant_name(&self) -> String {
self.discriminant().to_string()
}
#[cfg(all(feature = "libp2p", not(target_arch = "wasm32")))]
fn to_record(&self) -> Result<libp2p::kad::Record, bincode::error::EncodeError> {
let key_bytes = bincode::encode_to_vec(self, bincode::config::standard())?;
let record_key = libp2p::kad::RecordKey::new(&key_bytes);
let value_bytes = bincode::encode_to_vec(self, bincode::config::standard())?;
Ok(libp2p::kad::Record {
key: record_key,
value: value_bytes,
publisher: None,
expires: None,
})
}
}
#[cfg(all(feature = "libp2p", not(target_arch = "wasm32")))]
pub trait RecordStoreExt: NetabaseDefinitionTrait
where
<Self as IntoDiscriminant>::Discriminant: NetabaseDiscriminant,
<<Self as NetabaseDefinitionTrait>::Keys as IntoDiscriminant>::Discriminant:
NetabaseKeyDiscriminant,
{
#[cfg(feature = "sled")]
fn handle_sled_put(
&self,
store: &crate::databases::sled_store::SledStore<Self>,
) -> libp2p::kad::store::Result<()>;
#[cfg(feature = "sled")]
fn handle_sled_get(
store: &crate::databases::sled_store::SledStore<Self>,
key: &libp2p::kad::RecordKey,
) -> Option<(Self, libp2p::kad::Record)>;
#[cfg(feature = "sled")]
fn handle_sled_remove(
store: &crate::databases::sled_store::SledStore<Self>,
key: &libp2p::kad::RecordKey,
);
#[cfg(feature = "sled")]
fn handle_sled_records<'a>(
store: &'a crate::databases::sled_store::SledStore<Self>,
) -> Box<dyn Iterator<Item = std::borrow::Cow<'a, libp2p::kad::Record>> + 'a>;
#[cfg(feature = "redb")]
fn handle_redb_put(
&self,
store: &crate::databases::redb_store::RedbStore<Self>,
) -> libp2p::kad::store::Result<()>;
#[cfg(feature = "redb")]
fn handle_redb_get(
store: &crate::databases::redb_store::RedbStore<Self>,
key: &libp2p::kad::RecordKey,
) -> Option<(Self, libp2p::kad::Record)>;
#[cfg(feature = "redb")]
fn handle_redb_remove(
store: &crate::databases::redb_store::RedbStore<Self>,
key: &libp2p::kad::RecordKey,
);
#[cfg(feature = "redb")]
fn handle_redb_records<'a>(
store: &'a crate::databases::redb_store::RedbStore<Self>,
) -> Box<dyn Iterator<Item = std::borrow::Cow<'a, libp2p::kad::Record>> + 'a>;
#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
fn handle_indexeddb_put(
&self,
store: &crate::databases::indexeddb_store::IndexedDBStore<Self>,
) -> libp2p::kad::store::Result<()>;
#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
fn handle_indexeddb_get(
store: &crate::databases::indexeddb_store::IndexedDBStore<Self>,
key: &libp2p::kad::RecordKey,
) -> Option<(Self, libp2p::kad::Record)>;
#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
fn handle_indexeddb_remove(
store: &crate::databases::indexeddb_store::IndexedDBStore<Self>,
key: &libp2p::kad::RecordKey,
);
}
pub trait NetabaseDefinitionTraitKey:
bincode::Encode
+ bincode::Decode<()>
+ Clone
+ std::fmt::Debug
+ PartialEq
+ Eq
+ std::hash::Hash
+ PartialOrd
+ Ord
+ MaybeSend
+ MaybeSync
+ 'static
+ strum::IntoDiscriminant<Discriminant: NetabaseKeyDiscriminant>
{
#[cfg(all(feature = "libp2p", not(target_arch = "wasm32")))]
fn to_record_key(&self) -> Result<libp2p::kad::RecordKey, bincode::error::EncodeError> {
let key_bytes = bincode::encode_to_vec(self, bincode::config::standard())?;
Ok(libp2p::kad::RecordKey::new(&key_bytes))
}
}