aws_config/environment/
region.rsuse crate::meta::region::{future, ProvideRegion};
use aws_types::os_shim_internal::Env;
use aws_types::region::Region;
#[derive(Debug, Default)]
pub struct EnvironmentVariableRegionProvider {
env: Env,
}
impl EnvironmentVariableRegionProvider {
pub fn new() -> Self {
EnvironmentVariableRegionProvider { env: Env::real() }
}
pub(crate) fn new_with_env(env: Env) -> Self {
EnvironmentVariableRegionProvider { env }
}
}
impl ProvideRegion for EnvironmentVariableRegionProvider {
fn region(&self) -> future::ProvideRegion<'_> {
let region = self
.env
.get("AWS_REGION")
.or_else(|_| self.env.get("AWS_DEFAULT_REGION"))
.map(Region::new)
.ok();
future::ProvideRegion::ready(region)
}
}
#[cfg(test)]
mod test {
use crate::environment::region::EnvironmentVariableRegionProvider;
use crate::meta::region::ProvideRegion;
use aws_types::os_shim_internal::Env;
use aws_types::region::Region;
use futures_util::FutureExt;
fn test_provider(vars: &[(&str, &str)]) -> EnvironmentVariableRegionProvider {
EnvironmentVariableRegionProvider::new_with_env(Env::from_slice(vars))
}
#[test]
fn no_region() {
assert_eq!(
test_provider(&[])
.region()
.now_or_never()
.expect("no polling"),
None
);
}
#[test]
fn prioritize_aws_region() {
let provider = test_provider(&[
("AWS_REGION", "us-east-1"),
("AWS_DEFAULT_REGION", "us-east-2"),
]);
assert_eq!(
provider.region().now_or_never().expect("no polling"),
Some(Region::new("us-east-1"))
);
}
#[test]
fn fallback_to_default_region() {
assert_eq!(
test_provider(&[("AWS_DEFAULT_REGION", "us-east-2")])
.region()
.now_or_never()
.expect("no polling"),
Some(Region::new("us-east-2"))
);
}
}