1use std::future::Future;
6
7use bt_common::{
8 core::{Address, AddressType},
9 PeerId,
10};
11
12use crate::types::*;
13
14pub trait GetPeerAddr {
17 fn get_peer_address(
19 &self,
20 peer_id: PeerId,
21 ) -> impl Future<Output = Result<(Address, AddressType)>>;
22}
23
24pub struct StaticPeerAddr {
27 peer_id: Option<PeerId>,
28 address: Address,
29 address_type: AddressType,
30}
31
32impl StaticPeerAddr {
33 pub fn new(address: Address, address_type: AddressType) -> Self {
36 Self { peer_id: None, address, address_type }
37 }
38
39 pub fn new_for_peer(peer_id: PeerId, address: Address, address_type: AddressType) -> Self {
42 Self { peer_id: Some(peer_id), address, address_type }
43 }
44}
45
46impl GetPeerAddr for StaticPeerAddr {
47 async fn get_peer_address(&self, peer_id: PeerId) -> Result<(Address, AddressType)> {
48 if let Some(validated_peer_id) = self.peer_id {
49 if peer_id != validated_peer_id {
50 return Err(Error::PeerNotRecognized(peer_id));
51 }
52 }
53 return Ok((self.address, self.address_type));
54 }
55}