timber_rust/config/
entry_crud.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Dante Doménech Martinez dante19031999@gmail.com
3
4use crate::Concurrency;
5use crate::config::entry::Entry;
6#[cfg(feature = "aws")]
7use crate::service::CloudWatchConfig;
8#[cfg(feature = "loki")]
9use crate::service::LokiConfig;
10#[cfg(feature = "aws")]
11use std::time::SystemTime;
12
13impl Entry {
14    /// Returns the concurrency strategy if the variant supports one.
15    ///
16    /// Returns `None` for variants like `Silent` where execution strategy is irrelevant.
17    pub fn get_concurrency(&self) -> Option<Concurrency> {
18        match self {
19            Entry::Silent { .. } => None,
20            Entry::StdOut { concurrency, .. } => Some(*concurrency),
21            Entry::StdErr { concurrency, .. } => Some(*concurrency),
22            Entry::File { concurrency, .. } => Some(*concurrency),
23            Entry::BufferedFile { concurrency, .. } => Some(*concurrency),
24            Entry::String { concurrency, .. } => Some(*concurrency),
25            Entry::Vector { concurrency, .. } => Some(*concurrency),
26            _ => None,
27        }
28    }
29
30    /// Returns the configured capacity if the variant supports pre-allocation.
31    pub fn get_capacity(&self) -> Option<usize> {
32        match self {
33            Entry::String { capacity, .. } => capacity.clone(),
34            Entry::Vector { capacity, .. } => capacity.clone(),
35            _ => None,
36        }
37    }
38
39    /// Returns the number of worker threads configured for this entry.
40    pub fn get_worker_count(&self) -> Option<usize> {
41        match self {
42            Entry::StdOut { worker_count, .. } => worker_count.clone(),
43            Entry::StdErr { worker_count, .. } => worker_count.clone(),
44            Entry::File { worker_count, .. } => worker_count.clone(),
45            Entry::BufferedFile { worker_count, .. } => worker_count.clone(),
46            Entry::String { worker_count, .. } => worker_count.clone(),
47            Entry::Vector { worker_count, .. } => worker_count.clone(),
48            #[cfg(feature = "loki")]
49            Entry::Loki { worker_count, .. } => Some(*worker_count),
50            #[cfg(feature = "awscout")]
51            Entry::CloudWatchCout { worker_count, .. } => worker_count.clone(),
52            _ => None,
53        }
54    }
55
56    /// Returns the maximum retry attempts configured for this entry.
57    pub fn get_max_retries(&self) -> Option<usize> {
58        match self {
59            Entry::StdOut { max_retries, .. } => max_retries.clone(),
60            Entry::StdErr { max_retries, .. } => max_retries.clone(),
61            Entry::File { max_retries, .. } => max_retries.clone(),
62            Entry::BufferedFile { max_retries, .. } => max_retries.clone(),
63            Entry::String { max_retries, .. } => max_retries.clone(),
64            Entry::Vector { max_retries, .. } => max_retries.clone(),
65            #[cfg(feature = "loki")]
66            Entry::Loki { max_retries, .. } => Some(*max_retries),
67            #[cfg(feature = "awscout")]
68            Entry::CloudWatchCout { max_retries, .. } => max_retries.clone(),
69            _ => None,
70        }
71    }
72
73    /// Sets the concurrency strategy for this entry.
74    pub fn concurrency(mut self, concurrency: Concurrency) -> Self {
75        match &mut self {
76            Entry::StdOut {
77                concurrency: conc, ..
78            }
79            | Entry::StdErr {
80                concurrency: conc, ..
81            }
82            | Entry::File {
83                concurrency: conc, ..
84            }
85            | Entry::BufferedFile {
86                concurrency: conc, ..
87            }
88            | Entry::String {
89                concurrency: conc, ..
90            }
91            | Entry::Vector {
92                concurrency: conc, ..
93            } => {
94                *conc = concurrency;
95            }
96            #[cfg(feature = "awscout")]
97            Entry::CloudWatchCout {
98                concurrency: conc, ..
99            } => {
100                *conc = concurrency;
101            }
102            _ => {}
103        }
104
105        self
106    }
107
108    /// Sets the pre-allocation capacity for variants that support it (String, Vector).
109    pub fn capacity(mut self, capacity: usize) -> Self {
110        match &mut self {
111            Entry::String { capacity: cap, .. } => *cap = Some(capacity),
112            Entry::Vector { capacity: cap, .. } => *cap = Some(capacity),
113            _ => {}
114        }
115
116        self
117    }
118
119    /// Sets the background worker count for this entry.
120    pub fn worker_count(mut self, worker_count: usize) -> Self {
121        match &mut self {
122            Entry::StdOut {
123                worker_count: wk, ..
124            }
125            | Entry::StdErr {
126                worker_count: wk, ..
127            }
128            | Entry::File {
129                worker_count: wk, ..
130            }
131            | Entry::BufferedFile {
132                worker_count: wk, ..
133            }
134            | Entry::String {
135                worker_count: wk, ..
136            }
137            | Entry::Vector {
138                worker_count: wk, ..
139            } => *wk = Some(worker_count),
140            #[cfg(feature = "loki")]
141            Entry::Loki {
142                worker_count: wk, ..
143            } => *wk = worker_count,
144            #[cfg(feature = "awscout")]
145            Entry::CloudWatchCout {
146                worker_count: wk, ..
147            } => *wk = Some(worker_count),
148            _ => {}
149        }
150        self
151    }
152
153    /// Sets the maximum number of retry attempts for this entry.
154    pub fn max_retries(mut self, max_retries: usize) -> Self {
155        match &mut self {
156            Entry::StdOut {
157                max_retries: ret, ..
158            }
159            | Entry::StdErr {
160                max_retries: ret, ..
161            }
162            | Entry::File {
163                max_retries: ret, ..
164            }
165            | Entry::BufferedFile {
166                max_retries: ret, ..
167            }
168            | Entry::String {
169                max_retries: ret, ..
170            }
171            | Entry::Vector {
172                max_retries: ret, ..
173            } => *ret = Some(max_retries),
174            #[cfg(feature = "loki")]
175            Entry::Loki {
176                max_retries: wk, ..
177            } => *wk = max_retries,
178            #[cfg(feature = "awscout")]
179            Entry::CloudWatchCout {
180                max_retries: wk, ..
181            } => *wk = Some(max_retries),
182            _ => {}
183        }
184        self
185    }
186
187    #[cfg(feature = "loki")]
188    #[cfg_attr(docsrs, doc(cfg(feature = "loki")))]
189    /// Attempts to extract and build a [`LokiConfig`] from this entry.
190    pub fn build_loki_config(self) -> Option<LokiConfig> {
191        match self {
192            Entry::Loki {
193                url,
194                app,
195                job,
196                env,
197                basic_auth,
198                bearer_auth,
199                connection_timeout,
200                request_timeout,
201                max_retries,
202                worker_count,
203            } => {
204                let config = LokiConfig::new(url)
205                    .app(app)
206                    .job(job)
207                    .env(env)
208                    .basic_auth(basic_auth)
209                    .bearer_auth(bearer_auth)
210                    .connection_timeout(connection_timeout)
211                    .request_timeout(request_timeout)
212                    .max_retries(max_retries)
213                    .worker_count(worker_count);
214                Some(config)
215            }
216            _ => None,
217        }
218    }
219
220    #[cfg(feature = "aws")]
221    #[cfg_attr(docsrs, doc(cfg(feature = "aws")))]
222    /// Attempts to extract and build a [`CloudWatchConfig`] from this entry.
223    pub fn build_cloudwatch_config(self) -> Option<CloudWatchConfig> {
224        match self {
225            Entry::CloudWatchConfig {
226                access_key_id,
227                access_key_secret,
228                session_token,
229                expires_in,
230                log_group,
231                region,
232            } => {
233                let config =
234                    CloudWatchConfig::new(access_key_id, access_key_secret, log_group, region)
235                        .session_token(session_token)
236                        .expires_in(expires_in.map(|t| SystemTime::from(t)));
237                Some(config)
238            }
239            _ => None,
240        }
241    }
242}