Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(macro_env_var): define env_vars! macro (#3986)

Will Hutchinson93ec27fc270d
diff --git a/rust/cloud-storage/macro_env_var/src/lib.rs b/rust/cloud-storage/macro_env_var/src/lib.rs
index 29c383263f..b666e5a8ca 100644
--- a/rust/cloud-storage/macro_env_var/src/lib.rs
+++ b/rust/cloud-storage/macro_env_var/src/lib.rs
@@ -293,6 +293,74 @@ macro_rules! env_var {
};
}
+/// A macro for defining multiple environment variable structs in a single invocation.
+///
+/// Each definition expands exactly as if it were passed to [`env_var!`] on its own.
+///
+/// # Example
+///
+/// ```
+/// use macro_env_var::env_vars;
+///
+/// env_vars! {
+/// pub struct BaseUrl;
+/// pub struct DatabaseUrl;
+/// #[derive(Debug, Clone)]
+/// pub struct RedisUri;
+/// }
+///
+/// let _url: Result<BaseUrl, _> = BaseUrl::new();
+/// ```
+#[macro_export]
+macro_rules! env_vars {
+ (
+ $(
+ $(#[$attr:meta])*
+ $v:vis struct $n:ident;
+ )*
+ ) => {
+ $(
+ $crate::env_var! {
+ $(#[$attr])*
+ $v struct $n;
+ }
+ )*
+ };
+}
+
+/// A macro for defining multiple optional environment variable structs in a single invocation.
+///
+/// Each definition expands exactly as if it were passed to [`maybe_env_var!`] on its own.
+///
+/// # Example
+///
+/// ```
+/// use macro_env_var::maybe_env_vars;
+///
+/// maybe_env_vars! {
+/// pub struct OptionalApiKey;
+/// pub struct OptionalFeatureFlag;
+/// }
+///
+/// let _key: Option<OptionalApiKey> = OptionalApiKey::new();
+/// ```
+#[macro_export]
+macro_rules! maybe_env_vars {
+ (
+ $(
+ $(#[$attr:meta])*
+ $v:vis struct $n:ident;
+ )*
+ ) => {
+ $(
+ $crate::maybe_env_var! {
+ $(#[$attr])*
+ $v struct $n;
+ }
+ )*
+ };
+}
+
/// A macro for defining optional environment variables that return `Option` instead of `Result`.
///
/// Use this when an environment variable is optional and its absence is expected behavior,
diff --git a/rust/cloud-storage/macro_env_var/src/tests.rs b/rust/cloud-storage/macro_env_var/src/tests.rs
index a4b59d0512..eea10bdb5c 100644
--- a/rust/cloud-storage/macro_env_var/src/tests.rs
+++ b/rust/cloud-storage/macro_env_var/src/tests.rs
@@ -175,6 +175,38 @@ fn it_should_panic() {
with_mock_env(mock_no_env, Config::unwrap_new);
}
+env_vars! {
+ #[derive(Debug, Clone)]
+ pub struct BatchVarOne;
+ pub struct BatchVarTwo;
+ #[derive(Debug)]
+ pub struct BatchVarThree;
+}
+
+fn mock_batch_vars(k: &'static str) -> Result<String, std::env::VarError> {
+ match k {
+ "BATCH_VAR_ONE" => Ok("one".to_string()),
+ "BATCH_VAR_TWO" => Ok("two".to_string()),
+ _ => Err(std::env::VarError::NotPresent),
+ }
+}
+
+#[test]
+fn env_vars_defines_multiple_readable_vars() {
+ let (one, two) = with_mock_env(mock_batch_vars, || {
+ (BatchVarOne::unwrap_new(), BatchVarTwo::unwrap_new())
+ });
+
+ assert_eq!(&*one, "one");
+ assert_eq!(&*two, "two");
+}
+
+#[test]
+fn env_vars_definitions_error_when_not_set() {
+ let result = with_mock_env(mock_batch_vars, BatchVarThree::new);
+ assert!(result.is_err());
+}
+
// Tests for maybe_env_var! macro
maybe_env_var! {