#[derive(Debug, Clone)]
pub struct BedrockConfig {
pub region: String,
pub profile_name: Option<String>,
pub endpoint_override: Option<String>,
}
impl BedrockConfig {
pub fn new(region: impl Into<String>) -> Self {
Self {
region: region.into(),
profile_name: None,
endpoint_override: None,
}
}
pub fn from_env() -> Self {
let region = std::env::var("AWS_REGION")
.or_else(|_| std::env::var("AWS_DEFAULT_REGION"))
.unwrap_or_else(|_| "us-east-1".to_string());
let profile_name = std::env::var("AWS_PROFILE").ok();
Self {
region,
profile_name,
endpoint_override: None,
}
}
pub fn with_profile_name(mut self, profile_name: impl Into<String>) -> Self {
self.profile_name = Some(profile_name.into());
self
}
pub fn with_endpoint_override(mut self, endpoint: impl Into<String>) -> Self {
self.endpoint_override = Some(endpoint.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_sets_region_and_defaults() {
let config = BedrockConfig::new("us-west-2");
assert_eq!(config.region, "us-west-2");
assert!(config.profile_name.is_none());
assert!(config.endpoint_override.is_none());
}
#[test]
fn test_with_profile_name() {
let config = BedrockConfig::new("eu-west-1").with_profile_name("staging");
assert_eq!(config.region, "eu-west-1");
assert_eq!(config.profile_name.as_deref(), Some("staging"));
}
#[test]
fn test_with_endpoint_override() {
let config = BedrockConfig::new("us-east-1").with_endpoint_override(
"https://vpce-xxx.bedrock-runtime.us-east-1.vpce.amazonaws.com",
);
assert_eq!(
config.endpoint_override.as_deref(),
Some("https://vpce-xxx.bedrock-runtime.us-east-1.vpce.amazonaws.com")
);
}
#[test]
fn test_builder_chaining() {
let config = BedrockConfig::new("ap-southeast-1")
.with_profile_name("prod")
.with_endpoint_override("http://localhost:4566");
assert_eq!(config.region, "ap-southeast-1");
assert_eq!(config.profile_name.as_deref(), Some("prod"));
assert_eq!(
config.endpoint_override.as_deref(),
Some("http://localhost:4566")
);
}
#[test]
fn test_from_env_returns_a_config() {
let config = BedrockConfig::from_env();
assert!(!config.region.is_empty());
}
}