1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::{marker::PhantomData, sync::Arc};
use bonsaidb_core::{custom_api::CustomApi, networking::CURRENT_PROTOCOL_VERSION};
#[cfg(not(target_arch = "wasm32"))]
use fabruic::Certificate;
use url::Url;
use crate::{client::CustomApiCallback, Client, Error};
#[must_use]
pub struct Builder<A: CustomApi = ()> {
url: Url,
protocol_version: &'static str,
custom_api_callback: Option<Arc<dyn CustomApiCallback<A>>>,
#[cfg(not(target_arch = "wasm32"))]
certificate: Option<fabruic::Certificate>,
_api: PhantomData<A>,
}
impl Builder<()> {
pub(crate) fn new(url: Url) -> Self {
Self {
url,
protocol_version: CURRENT_PROTOCOL_VERSION,
custom_api_callback: None,
#[cfg(not(target_arch = "wasm32"))]
certificate: None,
_api: PhantomData::default(),
}
}
pub fn with_custom_api<A: CustomApi>(self) -> Builder<A> {
Builder {
url: self.url,
protocol_version: self.protocol_version,
custom_api_callback: None,
#[cfg(not(target_arch = "wasm32"))]
certificate: self.certificate,
_api: PhantomData::default(),
}
}
pub fn with_custom_api_callback<A: CustomApi, C: CustomApiCallback<A>>(
self,
callback: C,
) -> Builder<A> {
Builder {
url: self.url,
protocol_version: self.protocol_version,
custom_api_callback: Some(Arc::new(callback)),
#[cfg(not(target_arch = "wasm32"))]
certificate: self.certificate,
_api: PhantomData::default(),
}
}
}
impl<A: CustomApi> Builder<A> {
#[cfg(not(target_arch = "wasm32"))]
pub fn with_certificate(mut self, certificate: Certificate) -> Self {
self.certificate = Some(certificate);
self
}
#[cfg(feature = "test-util")]
pub fn with_protocol_version(mut self, version: &'static str) -> Self {
self.protocol_version = version;
self
}
pub async fn finish(self) -> Result<Client<A>, Error<A::Error>> {
Client::<A>::new_from_parts(
self.url,
self.protocol_version,
self.custom_api_callback,
#[cfg(not(target_arch = "wasm32"))]
self.certificate,
)
.await
}
}