1use crate::node::ShadowNode;
4use crypto::Keypair;
5use shadow_core::error::Result;
6use dht::NodeConfig as DHTConfig;
7
8pub struct ShadowNodeBuilder {
10 keypair: Option<Keypair>,
11 dht_config: DHTConfig,
12}
13
14impl ShadowNodeBuilder {
15 pub fn new() -> Self {
17 Self {
18 keypair: None,
19 dht_config: DHTConfig::default(),
20 }
21 }
22
23 pub fn with_keypair(mut self, keypair: Keypair) -> Self {
25 self.keypair = Some(keypair);
26 self
27 }
28
29 pub fn generate_keypair(mut self) -> Self {
31 self.keypair = Some(Keypair::generate());
32 self
33 }
34
35 pub fn with_dht_config(mut self, config: DHTConfig) -> Self {
37 self.dht_config = config;
38 self
39 }
40
41 pub fn with_bucket_size(mut self, k: usize) -> Self {
43 self.dht_config.k = k;
44 self
45 }
46
47 pub fn with_max_storage(mut self, bytes: usize) -> Self {
49 self.dht_config.max_storage = bytes;
50 self
51 }
52
53 pub fn build(self) -> Result<ShadowNode> {
55 let keypair = self.keypair.unwrap_or_else(Keypair::generate);
56 Ok(ShadowNode::new(keypair))
57 }
58}
59
60impl Default for ShadowNodeBuilder {
61 fn default() -> Self {
62 Self::new()
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_builder() {
72 let node = ShadowNodeBuilder::new()
73 .generate_keypair()
74 .with_bucket_size(20)
75 .build()
76 .unwrap();
77
78 assert_eq!(node.get_peers().len(), 0);
79 }
80
81 #[test]
82 fn test_builder_with_keypair() {
83 let keypair = Keypair::generate();
84 let node = ShadowNodeBuilder::new()
85 .with_keypair(keypair)
86 .build()
87 .unwrap();
88
89 assert_eq!(node.get_peers().len(), 0);
90 }
91}