scuffle_bootstrap/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
pub trait ConfigParser: Sized {
	fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>>;
}

impl ConfigParser for () {
	#[inline(always)]
	fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>> {
		std::future::ready(Ok(()))
	}
}

pub struct EmptyConfig;

impl ConfigParser for EmptyConfig {
	#[inline(always)]
	fn parse() -> impl std::future::Future<Output = anyhow::Result<Self>> {
		std::future::ready(Ok(EmptyConfig))
	}
}

/// A macro to create a config parser from a CLI struct
/// This macro will automatically parse the CLI struct into the given type
/// using the `scuffle-settings` crate
#[cfg(feature = "settings")]
#[macro_export]
macro_rules! cli_config {
	($ty:ty) => {
		impl $crate::config::ConfigParser for $ty {
			async fn parse() -> anyhow::Result<Self> {
				use $crate::prelude::anyhow::Context;

				$crate::prelude::scuffle_settings::parse_settings(
					$crate::prelude::scuffle_settings::Options::builder()
						.cli($crate::prelude::scuffle_settings::cli!())
						.build(),
				)
				.context("config")
			}
		}
	};
}