pub const RUNTIME_API_ENV: &str = "AWS_LAMBDA_RUNTIME_API";
const MAX_CONCURRENCY_ENV: &str = "AWS_LAMBDA_MAX_CONCURRENCY";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Activation {
#[default]
Auto,
Always,
Never,
}
impl Activation {
pub(crate) const fn is_active(self, runtime_api_present: bool) -> bool {
match self {
Self::Auto => runtime_api_present,
Self::Always => true,
Self::Never => false,
}
}
}
const REQUIRED_ENV: [&str; 4] = [
RUNTIME_API_ENV,
"AWS_LAMBDA_FUNCTION_NAME",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE",
"AWS_LAMBDA_FUNCTION_VERSION",
];
pub fn missing_lambda_env(env: impl Fn(&str) -> Option<String>) -> Vec<&'static str> {
REQUIRED_ENV
.into_iter()
.filter(|name| {
env(name).is_none_or(|value| {
*name == "AWS_LAMBDA_FUNCTION_MEMORY_SIZE" && value.parse::<i32>().is_err()
})
})
.collect()
}
pub fn lambda_concurrency(env: impl Fn(&str) -> Option<String>) -> u32 {
env(MAX_CONCURRENCY_ENV)
.and_then(|value| value.parse::<u32>().ok())
.filter(|&n| n > 0)
.unwrap_or(1)
}
#[cfg(test)]
mod tests {
use super::Activation;
#[test]
fn auto_starts_only_on_lambda() {
assert!(Activation::Auto.is_active(true));
assert!(!Activation::Auto.is_active(false));
}
#[test]
fn always_and_never_ignore_the_environment() {
for present in [true, false] {
assert!(Activation::Always.is_active(present));
assert!(!Activation::Never.is_active(present));
}
}
#[test]
fn default_activation_is_auto() {
assert_eq!(Activation::default(), Activation::Auto);
}
}