kmip_protocol/net/client/
builder.rs1use std::{mem::MaybeUninit, sync::Arc};
2
3#[cfg(feature = "tokio")]
4use tokio::sync::Mutex;
5
6#[cfg(not(feature = "tokio"))]
7use std::sync::Mutex;
8
9use crate::{
10 net::client::Client,
11 types::{
12 request::{Authentication, CredentialValue, Password, Username},
13 traits::ReadWrite,
14 },
15};
16
17#[derive(Debug)]
19pub struct ClientBuilder<T: ReadWrite> {
20 stream: T,
21 auth: Option<Authentication>,
22 max_messagesize: i32,
23}
24
25impl<T: ReadWrite> ClientBuilder<T> {
26 pub fn new(stream: T) -> Self {
37 Self {
38 stream,
39 auth: None,
40 max_messagesize: i32::MAX,
41 }
42 }
43
44 pub fn with_credentials(mut self, username: String, password: Option<String>) -> Self {
48 self.auth = Some(Authentication::build(CredentialValue::UsernameAndPassword(
49 Username(username),
50 password.map(Password),
51 )));
52 self
53 }
54
55 pub fn with_max_message_size(mut self, max: i32) -> Self {
57 self.max_messagesize = max;
58 self
59 }
60
61 pub fn build(self) -> Client<T> {
63 let auth = self.auth;
64 let stream = Arc::new(Mutex::new(self.stream));
65 let max_message_size = self.max_messagesize;
66 let read_buf = vec![0u8; 8192];
67 let write_buf = vec![MaybeUninit::uninit(); 8192];
68 let connection_error_count = Default::default();
69
70 Client {
71 auth,
72 max_message_size,
73 stream,
74 read_buf,
75 write_buf,
76 connection_error_count,
77 cached_query_response_payload: None,
78 }
79 }
80}