1use std::fmt::Debug;
20
21use crate::{dht::DhtNode, net::ChannelPtr, Result};
22
23type K = blake3::Hash;
24
25#[derive(Clone, Debug)]
26pub enum DhtEvent<N: DhtNode, V: Clone + Debug> {
27 BootstrapStarted,
28 BootstrapCompleted,
29 PingReceived { from: ChannelPtr, result: Result<K> },
30 PingSent { to: ChannelPtr, result: Result<()> },
31 ValueFound { key: K, value: V },
32 NodesFound { key: K, nodes: Vec<N> },
33 ValueLookupStarted { key: K },
34 NodesLookupStarted { key: K },
35 ValueLookupCompleted { key: K, nodes: Vec<N>, values: Vec<V> },
36 NodesLookupCompleted { key: K, nodes: Vec<N> },
37}
38
39impl<N: DhtNode, V: Clone + Debug> DhtEvent<N, V> {
40 pub fn key(&self) -> Option<&blake3::Hash> {
41 match self {
42 DhtEvent::BootstrapStarted => None,
43 DhtEvent::BootstrapCompleted => None,
44 DhtEvent::PingReceived { .. } => None,
45 DhtEvent::PingSent { .. } => None,
46 DhtEvent::ValueFound { key, .. } => Some(key),
47 DhtEvent::NodesFound { key, .. } => Some(key),
48 DhtEvent::ValueLookupStarted { key } => Some(key),
49 DhtEvent::NodesLookupStarted { key } => Some(key),
50 DhtEvent::ValueLookupCompleted { key, .. } => Some(key),
51 DhtEvent::NodesLookupCompleted { key, .. } => Some(key),
52 }
53 }
54
55 pub fn into_value(self) -> Option<V> {
56 match self {
57 DhtEvent::ValueFound { value, .. } => Some(value),
58 _ => None,
59 }
60 }
61}