scuffle_bootstrap/config.rs
1//! Config parsing.
2
3/// This trait is used to parse a configuration for the application.
4///
5/// The avoid having to manually implement this trait, the `bootstrap!` macro in
6/// the [`scuffle-settings`] crate can be used to
7/// generate an implementation.
8///
9/// # See Also
10///
11/// - [`Global`](crate::Global)
12/// - [`scuffle-settings`]
13///
14/// [scuffle-settings]: ../scuffle-settings
15pub trait ConfigParser: Sized {
16 /// Parse the configuration for the application.
17 fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>>;
18}
19
20impl ConfigParser for () {
21 #[inline(always)]
22 fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>> {
23 std::future::ready(Ok(()))
24 }
25}
26
27/// An empty configuration that can be used when no configuration is needed.
28pub struct EmptyConfig;
29
30impl ConfigParser for EmptyConfig {
31 #[inline(always)]
32 fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>> {
33 std::future::ready(Ok(EmptyConfig))
34 }
35}
36
37#[cfg(test)]
38#[cfg_attr(all(test, coverage_nightly), coverage(off))]
39mod tests {
40 use super::{ConfigParser, EmptyConfig};
41
42 #[tokio::test]
43 async fn unit_config() {
44 assert!(matches!(<()>::parse().await, Ok(())));
45 }
46
47 #[tokio::test]
48 async fn empty_config() {
49 assert!(matches!(EmptyConfig::parse().await, Ok(EmptyConfig)));
50 }
51}