timber_rust/factory/
aws.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Dante Doménech Martinez dante19031999@gmail.com
3
4#![cfg(feature = "aws")]
5#![cfg_attr(docsrs, doc(cfg(feature = "awscout")))]
6
7use crate::factory::awscout::CloudWatch;
8use crate::{CloudWatchLogger, Logger, service};
9
10/// A builder state for constructing a [`CloudWatchLogger`] from a concrete [`CloudWatchConfig`].
11pub struct CloudWatchConfig {
12    config: service::CloudWatchConfig,
13}
14
15/// A builder state for constructing a [`CloudWatchLogger`] using environment-based
16/// AWS credentials and a specific log group.
17pub struct CloudWatchEnv {
18    log_group: String,
19}
20
21impl CloudWatch {
22    /// Begins building a CloudWatch logger using a manual configuration.
23    pub fn config(self, config: service::CloudWatchConfig) -> CloudWatchConfig {
24        CloudWatchConfig { config }
25    }
26
27    /// Begins building a CloudWatch logger that pulls credentials from the environment.
28    pub fn env<S>(self, log_group: S) -> CloudWatchEnv
29    where
30        S: Into<String>,
31    {
32        CloudWatchEnv {
33            log_group: log_group.into(),
34        }
35    }
36}
37
38impl CloudWatchConfig {
39    /// Creates a new config factory instance.
40    pub fn new(config: service::CloudWatchConfig) -> Self {
41        Self { config }
42    }
43
44    /// Returns a reference to the internal configuration.
45    pub fn get_config(&self) -> &service::CloudWatchConfig {
46        &self.config
47    }
48
49    /// Sets underlying configuration.
50    pub fn config(self, config: service::CloudWatchConfig) -> CloudWatchConfig {
51        Self { config, ..self }
52    }
53
54    /// Finalizes construction and returns a wrapped [`Logger`].
55    pub fn build(self) -> Logger {
56        Logger::new(self.build_impl())
57    }
58
59    /// Builds the underlying [`CloudWatchLogger`] implementation.
60    pub fn build_impl(self) -> Box<CloudWatchLogger> {
61        CloudWatchLogger::new(self.config)
62    }
63}
64
65impl CloudWatchEnv {
66    /// Creates a new environment-based factory instance.
67    pub fn new<S>(log_group: S) -> Self
68    where
69        S: Into<String>,
70    {
71        Self {
72            log_group: log_group.into(),
73        }
74    }
75
76    /// Returns the name of the target log group.
77    pub fn get_log_group(&self) -> &str {
78        self.log_group.as_str()
79    }
80
81    /// Sets or overrides the target log group.
82    pub fn log_group<S>(&mut self, value: S) -> Self
83    where
84        S: Into<String>,
85    {
86        CloudWatchEnv {
87            log_group: value.into(),
88        }
89    }
90
91    /// Finalizes construction and returns a wrapped [`Logger`].
92    pub fn build(self) -> Logger {
93        Logger::new(self.build_impl())
94    }
95
96    /// Builds the underlying [`CloudWatchLogger`] using environment discovery.
97    pub fn build_impl(self) -> Box<CloudWatchLogger> {
98        CloudWatchLogger::from_env(self.log_group)
99    }
100}