1pub use self::{
2    country::Country,
3    environment::{Environment, EnvironmentParseError},
4};
5#[cfg(feature = "json-logger")]
6use crate::json::formatter::DefaultEventFormatter;
7
8mod country;
9mod environment;
10
11#[cfg(not(feature = "json-logger"))]
12use crate::subscriber::NopEventFormatter;
13
14pub struct SubscriberConfig<T> {
20    pub country: Country,
21    pub env: Environment,
22    pub telemetry: Option<TelemetryConfig>,
23    pub service: String,
24    pub version: Option<String>,
25    pub json_formatter: T,
26}
27
28#[cfg(not(feature = "json-logger"))]
29pub fn builder(
31    service: &str,
32) -> SubscriberConfigBuilder<NopEventFormatter, WithoutCountry, WithoutEnvironment> {
33    SubscriberConfigBuilder::<NopEventFormatter, WithoutCountry, WithoutEnvironment>::new(service)
34}
35
36#[cfg(feature = "json-logger")]
37pub fn builder(
39    service: &str,
40) -> SubscriberConfigBuilder<DefaultEventFormatter, WithoutCountry, WithoutEnvironment> {
41    SubscriberConfigBuilder::<DefaultEventFormatter, WithoutCountry, WithoutEnvironment>::new(
42        service,
43    )
44}
45
46pub struct TelemetryConfig {
47    pub collector_url: String,
48    pub service_name: String,
49}
50
51pub struct WithoutCountry;
52pub struct WithCountry(Country);
53
54pub struct WithoutEnvironment;
55pub struct WithEnvironment(Environment);
56
57pub struct SubscriberConfigBuilder<F, C, E> {
58    country: C,
59    env: E,
60    telemetry: Option<TelemetryConfig>,
61    service: String,
62    version: Option<String>,
63    formatter: F,
64}
65
66impl<F, C, E> SubscriberConfigBuilder<F, C, E> {
67    pub fn with_version(mut self, version: String) -> Self {
69        self.version = Some(version);
70        self
71    }
72
73    pub fn with_telemetry(mut self, collector_url: String, service_name: String) -> Self {
75        self.telemetry = Some(TelemetryConfig {
76            collector_url,
77            service_name,
78        });
79
80        self
81    }
82
83    pub fn with_custom_json_formatter<G>(self, formatter: G) -> SubscriberConfigBuilder<G, C, E> {
85        SubscriberConfigBuilder {
86            formatter,
87            country: self.country,
88            env: self.env,
89            service: self.service,
90            version: self.version,
91            telemetry: self.telemetry,
92        }
93    }
94}
95
96impl<F> SubscriberConfigBuilder<F, WithoutCountry, WithoutEnvironment> {
97    #[cfg(not(feature = "json-logger"))]
98    pub fn new(
100        service: &str,
101    ) -> SubscriberConfigBuilder<NopEventFormatter, WithoutCountry, WithoutEnvironment> {
102        Self::_new(service, NopEventFormatter)
103    }
104
105    #[cfg(feature = "json-logger")]
106    pub fn new(
108        service: &str,
109    ) -> SubscriberConfigBuilder<DefaultEventFormatter, WithoutCountry, WithoutEnvironment> {
110        Self::_new(service, DefaultEventFormatter)
111    }
112
113    fn _new<G>(
114        service: &str,
115        formatter: G,
116    ) -> SubscriberConfigBuilder<G, WithoutCountry, WithoutEnvironment> {
117        SubscriberConfigBuilder {
118            service: service.to_owned(),
119            country: WithoutCountry,
120            env: WithoutEnvironment,
121            telemetry: None,
122            version: None,
123            formatter,
124        }
125    }
126}
127
128impl<F, E> SubscriberConfigBuilder<F, WithoutCountry, E> {
129    pub fn with_country(self, country: Country) -> SubscriberConfigBuilder<F, WithCountry, E> {
131        SubscriberConfigBuilder {
132            country: WithCountry(country),
133            env: self.env,
134            telemetry: self.telemetry,
135            service: self.service,
136            version: self.version,
137            formatter: self.formatter,
138        }
139    }
140}
141
142impl<F, C> SubscriberConfigBuilder<F, C, WithoutEnvironment> {
143    pub fn with_env(self, env: Environment) -> SubscriberConfigBuilder<F, C, WithEnvironment> {
145        SubscriberConfigBuilder {
146            country: self.country,
147            env: WithEnvironment(env),
148            telemetry: self.telemetry,
149            service: self.service,
150            version: self.version,
151            formatter: self.formatter,
152        }
153    }
154}
155
156impl<F> SubscriberConfigBuilder<F, WithCountry, WithEnvironment> {
157    pub fn build(self) -> SubscriberConfig<F> {
159        SubscriberConfig {
160            country: self.country.0,
161            env: self.env.0,
162            telemetry: self.telemetry,
163            service: self.service,
164            version: self.version,
165            json_formatter: self.formatter,
166        }
167    }
168}