kitsune2_api/builder.rs
1//! Builder-related types.
2
3use crate::*;
4use std::sync::Arc;
5
6/// The general Kitsune2 builder.
7/// This contains both configuration and factory instances,
8/// allowing construction of runtime module instances.
9#[derive(Debug)]
10pub struct Builder {
11 /// The module configuration to be used when building modules.
12 /// This can be loaded from disk or modified before freezing the builder.
13 pub config: Config,
14
15 /// The [Verifier] to use for this Kitsune2 instance.
16 pub verifier: DynVerifier,
17
18 /// Any auth_material required to connect to
19 /// sbd/signal and bootstrap services.
20 pub auth_material: Option<Vec<u8>>,
21
22 /// The [KitsuneFactory] to be used for creating
23 /// [Kitsune] module instances.
24 pub kitsune: DynKitsuneFactory,
25
26 /// The [SpaceFactory] to be used for creating
27 /// [Space] instances.
28 pub space: DynSpaceFactory,
29
30 /// The [PeerStoreFactory] to be used for creating
31 /// [PeerStore] instances.
32 pub peer_store: DynPeerStoreFactory,
33
34 /// The [BootstrapFactory] to be used for creating
35 /// [Bootstrap] instances for initial WAN discovery.
36 pub bootstrap: DynBootstrapFactory,
37
38 /// The [FetchFactory] to be used for creating
39 /// [Fetch] instances.
40 pub fetch: DynFetchFactory,
41
42 /// The [TransportFactory] to be used for creating
43 /// [Transport] instances.
44 pub transport: DynTransportFactory,
45
46 /// The [OpStoreFactory] to be used for creating
47 /// [OpStore] instances.
48 pub op_store: DynOpStoreFactory,
49
50 /// The [PeerMetaStoreFactory] to be used for creating
51 /// [PeerMetaStore] instances.
52 pub peer_meta_store: DynPeerMetaStoreFactory,
53
54 /// The [GossipFactory] to be used for creating
55 /// [Gossip] instances.
56 pub gossip: DynGossipFactory,
57
58 /// The [LocalAgentStoreFactory] to be used for creating
59 /// [LocalAgentStore] instances.
60 pub local_agent_store: Arc<dyn LocalAgentStoreFactory>,
61
62 /// The [PublishFactory] to be used for creating [Publish]
63 /// instances
64 pub publish: DynPublishFactory,
65}
66
67impl Builder {
68 /// Construct a default config given the configured module factories.
69 ///
70 /// Note, this should be called before [Self::build] or otherwise
71 /// freezing the Builder instance in an Arc<>.
72 pub fn with_default_config(mut self) -> K2Result<Self> {
73 {
74 let Self {
75 config,
76 verifier: _,
77 auth_material: _,
78 kitsune,
79 space,
80 peer_store,
81 bootstrap,
82 fetch,
83 transport,
84 op_store,
85 peer_meta_store,
86 gossip,
87 local_agent_store,
88 publish,
89 } = &mut self;
90
91 kitsune.default_config(config)?;
92 space.default_config(config)?;
93 peer_store.default_config(config)?;
94 bootstrap.default_config(config)?;
95 fetch.default_config(config)?;
96 transport.default_config(config)?;
97 op_store.default_config(config)?;
98 peer_meta_store.default_config(config)?;
99 gossip.default_config(config)?;
100 local_agent_store.default_config(config)?;
101 publish.default_config(config)?;
102
103 config.mark_defaults_set();
104 }
105
106 Ok(self)
107 }
108
109 /// Validate the current configuration.
110 pub fn validate_config(&self) -> K2Result<()> {
111 self.kitsune.validate_config(&self.config)?;
112 self.space.validate_config(&self.config)?;
113 self.peer_store.validate_config(&self.config)?;
114 self.bootstrap.validate_config(&self.config)?;
115 self.fetch.validate_config(&self.config)?;
116 self.transport.validate_config(&self.config)?;
117 self.op_store.validate_config(&self.config)?;
118 self.peer_meta_store.validate_config(&self.config)?;
119 self.gossip.validate_config(&self.config)?;
120 self.local_agent_store.validate_config(&self.config)?;
121 self.publish.validate_config(&self.config)?;
122
123 self.config.mark_validated();
124
125 Ok(())
126 }
127
128 /// Generate the actual kitsune instance, validating configuration
129 /// if that has not already explicitly been done.
130 pub async fn build(self) -> K2Result<DynKitsune> {
131 if !self.config.mark_validated() {
132 self.validate_config()?;
133 }
134
135 self.config.mark_runtime();
136 let builder = Arc::new(self);
137
138 builder.kitsune.create(builder.clone()).await
139 }
140}