Skip to main content

cli_shared/
client_config.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Client configuration.
3
4use wire::AuthToken;
5
6/// Client configuration.
7#[derive(Debug, Clone)]
8pub struct ClientConfig {
9    /// Client identifier.
10    pub client_id: String,
11    /// Authentication token.
12    pub token: Option<AuthToken>,
13    /// Optional private key used to prove possession of a bound token.
14    pub auth_proof_key_pem: Option<String>,
15    /// Server key used to look up the credential in the credential store
16    /// (`~/.heddle/credentials.toml`).  Matches the key used by `heddle auth login`.
17    pub server_key: Option<String>,
18    /// Enable TLS.
19    pub tls_enabled: bool,
20    /// Override the expected TLS server name.
21    pub tls_domain_name: Option<String>,
22    /// Optional PEM CA certificate bundle for server verification.
23    pub tls_ca_certificate_pem: Option<String>,
24    /// Skip TLS certificate verification (insecure).
25    pub tls_skip_verify: bool,
26    /// Connection timeout in seconds.
27    pub timeout_secs: u64,
28    /// Enable compression.
29    pub compression: bool,
30    /// Preferred chunk size.
31    pub chunk_size: usize,
32    /// Enable chunked transfer negotiation.
33    pub chunked_transfer: bool,
34    /// Enable resumable transfer negotiation.
35    pub resumable_transfer: bool,
36    /// Enable pack transfer negotiation.
37    pub pack_transfer: bool,
38    /// Enable partial fetch negotiation.
39    pub partial_fetch: bool,
40}
41
42impl ClientConfig {
43    /// Create a new client configuration.
44    pub fn new(client_id: impl Into<String>) -> Self {
45        Self {
46            client_id: client_id.into(),
47            token: None,
48            auth_proof_key_pem: None,
49            server_key: None,
50            tls_enabled: false,
51            tls_domain_name: None,
52            tls_ca_certificate_pem: None,
53            tls_skip_verify: false,
54            timeout_secs: 30,
55            compression: true,
56            chunk_size: 64 * 1024,
57            chunked_transfer: true,
58            resumable_transfer: true,
59            pack_transfer: true,
60            partial_fetch: true,
61        }
62    }
63
64    /// Set authentication token.
65    pub fn with_token(mut self, token: AuthToken) -> Self {
66        self.token = Some(token);
67        self
68    }
69
70    /// Set a private key used for proof-of-possession metadata.
71    pub fn with_auth_proof_key_pem(mut self, pem: impl Into<String>) -> Self {
72        self.auth_proof_key_pem = Some(pem.into());
73        self
74    }
75
76    /// Set the server key used to look up credentials in the credential store.
77    pub fn with_server_key(mut self, key: impl Into<String>) -> Self {
78        self.server_key = Some(key.into());
79        self
80    }
81
82    /// Enable TLS.
83    pub fn with_tls(mut self, skip_verify: bool) -> Self {
84        self.tls_enabled = true;
85        self.tls_skip_verify = skip_verify;
86        self
87    }
88
89    pub fn with_tls_domain_name(mut self, domain_name: impl Into<String>) -> Self {
90        self.tls_domain_name = Some(domain_name.into());
91        self
92    }
93
94    pub fn with_tls_ca_certificate_pem(mut self, pem: impl Into<String>) -> Self {
95        self.tls_enabled = true;
96        self.tls_ca_certificate_pem = Some(pem.into());
97        self
98    }
99
100    /// Set timeout.
101    pub fn with_timeout(mut self, secs: u64) -> Self {
102        self.timeout_secs = secs;
103        self
104    }
105
106    /// Disable compression.
107    pub fn without_compression(mut self) -> Self {
108        self.compression = false;
109        self
110    }
111
112    /// Set chunk size.
113    pub fn with_chunk_size(mut self, size: usize) -> Self {
114        self.chunk_size = size;
115        self
116    }
117
118    /// Enable or disable chunked transfer negotiation.
119    pub fn with_chunked_transfer(mut self, enabled: bool) -> Self {
120        self.chunked_transfer = enabled;
121        self
122    }
123
124    /// Enable or disable resumable transfer negotiation.
125    pub fn with_resumable_transfer(mut self, enabled: bool) -> Self {
126        self.resumable_transfer = enabled;
127        self
128    }
129
130    /// Enable or disable pack transfer negotiation.
131    pub fn with_pack_transfer(mut self, enabled: bool) -> Self {
132        self.pack_transfer = enabled;
133        self
134    }
135
136    /// Enable or disable partial fetch negotiation.
137    pub fn with_partial_fetch(mut self, enabled: bool) -> Self {
138        self.partial_fetch = enabled;
139        self
140    }
141}
142
143impl Default for ClientConfig {
144    fn default() -> Self {
145        Self::new("heddle-client")
146    }
147}