use crate::types::P2PResult;
use async_trait::async_trait;
use std::fmt::{Debug, Display};
#[async_trait]
pub trait P2PSystem: Send + Sync + 'static {
type NodeId: P2PNodeId;
type Connection: P2PConnection<NodeId = Self::NodeId>;
async fn connect(&self, node_id: Self::NodeId) -> P2PResult<Self::Connection>;
async fn discover(&self, name: &str) -> P2PResult<Self::NodeId>;
#[must_use]
fn local_node_id(&self) -> &Self::NodeId;
}
pub trait P2PNodeId: Clone + Debug + Display + Send + Sync + 'static {
fn from_bytes(bytes: &[u8; 32]) -> P2PResult<Self>;
#[must_use]
fn as_bytes(&self) -> &[u8; 32];
#[must_use]
fn fmt_short(&self) -> String;
}
#[async_trait]
pub trait P2PConnection: Send + Sync + 'static {
type NodeId: P2PNodeId;
async fn send(&mut self, data: &[u8]) -> P2PResult<()>;
async fn recv(&mut self) -> P2PResult<Vec<u8>>;
#[must_use]
fn remote_node_id(&self) -> &Self::NodeId;
#[must_use]
fn is_connected(&self) -> bool;
fn close(&mut self) -> P2PResult<()>;
}