timber_rust/def.rs
1use serde::{Deserialize, Serialize};
2use std::fmt::{Display, Formatter};
3
4/// Defines the execution strategy for log processing and delivery.
5///
6/// This setting determines whether the logging operations will block the
7/// current thread or run concurrently using an asynchronous runtime.
8#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
9#[serde(rename_all = "lowercase")]
10pub enum Concurrency {
11 /// Synchronous execution.
12 ///
13 /// Logging operations are performed on the caller's thread. The program
14 /// execution will wait until the log is processed/sent before continuing.
15 /// Recommended for CLI tools or simple scripts where latency is not critical.
16 Sync,
17
18 /// Asynchronous execution.
19 ///
20 /// Logging operations are offloaded to an async task. This prevents
21 /// blocking the main application flow, making it suitable for high-performance
22 /// servers and applications using runtimes like `tokio` or `async-std`.
23 Async,
24}
25
26impl Display for Concurrency {
27 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28 match &self {
29 Self::Sync => write!(f, "sync"),
30 Self::Async => write!(f, "async"),
31 }
32 }
33}
34
35/// Credentials for HTTP Basic Authentication.
36///
37/// This structure is used by network-based loggers (like Loki or CloudWatch)
38/// to authenticate requests. It follows the standard `username:password`
39/// pattern.
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct BasicAuth {
42 /// The identification string for the user or service account.
43 ///
44 /// In many cloud-native logging services (e.g., Grafana Cloud),
45 /// this corresponds to the "Instance ID" or "User ID".
46 pub username: String,
47 /// The secret component of the credentials.
48 ///
49 /// This is optional because some internal proxies or development
50 /// environments may only require a username (or use an empty password).
51 /// When provided, it is combined with the username and Base64 encoded
52 /// in the HTTP `Authorization` header.
53 pub password: Option<String>,
54}
55
56impl BasicAuth {
57 // Creates a new [`BasicAuth`]
58 pub fn new<S1, S2>(username: S1, password: Option<S2>) -> BasicAuth
59 where
60 S1: Into<String>,
61 S2: Into<String>,
62 {
63 BasicAuth {
64 username: username.into(),
65 password: password.map(|password| password.into()),
66 }
67 }
68
69 // Creates a new [`BasicAuth`] wrapped in [`Option`]
70 pub fn some<S1, S2>(username: S1, password: Option<S2>) -> Option<BasicAuth>
71 where
72 S1: Into<String>,
73 S2: Into<String>,
74 {
75 Some(BasicAuth {
76 username: username.into(),
77 password: password.map(|password| password.into()),
78 })
79 }
80}