use aws_types::region::Region;
use crate::environment::region::EnvironmentVariableRegionProvider;
use crate::meta::region::{ProvideRegion, RegionProviderChain};
use crate::provider_config::ProviderConfig;
use crate::{imds, profile};
pub fn default_provider() -> impl ProvideRegion {
Builder::default().build()
}
#[derive(Debug)]
pub struct DefaultRegionChain(RegionProviderChain);
impl DefaultRegionChain {
pub async fn region(&self) -> Option<Region> {
self.0.region().await
}
pub fn builder() -> Builder {
Builder::default()
}
}
#[derive(Debug, Default)]
pub struct Builder {
env_provider: EnvironmentVariableRegionProvider,
profile_file: profile::region::Builder,
imds: imds::region::Builder,
}
impl Builder {
pub(crate) fn configure(mut self, configuration: &ProviderConfig) -> Self {
self.env_provider = EnvironmentVariableRegionProvider::new_with_env(configuration.env());
self.profile_file = self.profile_file.configure(configuration);
self.imds = self.imds.configure(configuration);
self
}
pub fn profile_name(mut self, name: &str) -> Self {
self.profile_file = self.profile_file.profile_name(name);
self
}
pub fn build(self) -> DefaultRegionChain {
DefaultRegionChain(
RegionProviderChain::first_try(self.env_provider)
.or_else(self.profile_file.build())
.or_else(self.imds.build()),
)
}
}
impl ProvideRegion for DefaultRegionChain {
fn region(&self) -> crate::meta::region::future::ProvideRegion<'_> {
ProvideRegion::region(&self.0)
}
}