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`](../../scuffle_settings) crate can be used to
7/// generate an implementation.
8///
9/// # See Also
10///
11/// - [`Global`](crate::Global)
12/// - [`scuffle_settings`](../../scuffle_settings)
13pub trait ConfigParser: Sized {
14    /// Parse the configuration for the application.
15    fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>>;
16}
17
18impl ConfigParser for () {
19    #[inline(always)]
20    fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>> {
21        std::future::ready(Ok(()))
22    }
23}
24
25/// An empty configuration that can be used when no configuration is needed.
26pub struct EmptyConfig;
27
28impl ConfigParser for EmptyConfig {
29    #[inline(always)]
30    fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>> {
31        std::future::ready(Ok(EmptyConfig))
32    }
33}
34
35#[cfg(test)]
36#[cfg_attr(all(test, coverage_nightly), coverage(off))]
37mod tests {
38    use super::{ConfigParser, EmptyConfig};
39
40    #[tokio::test]
41    async fn unit_config() {
42        assert!(matches!(<()>::parse().await, Ok(())));
43    }
44
45    #[tokio::test]
46    async fn empty_config() {
47        assert!(matches!(EmptyConfig::parse().await, Ok(EmptyConfig)));
48    }
49}