const INVALID_CREATE_BUCKET_CONFIGURATION: &str = "invalid create-bucket-configuration. only LocationConstraint=<region> is supported \
(e.g. LocationConstraint=ap-northeast-1).";
pub fn parse_create_bucket_configuration(value: &str) -> Result<String, String> {
let location_constraint = value
.strip_prefix("LocationConstraint=")
.ok_or_else(|| INVALID_CREATE_BUCKET_CONFIGURATION.to_string())?;
if location_constraint.is_empty()
|| location_constraint.contains(',')
|| location_constraint.contains('=')
{
return Err(INVALID_CREATE_BUCKET_CONFIGURATION.to_string());
}
Ok(location_constraint.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_location_constraint() {
assert_eq!(
parse_create_bucket_configuration("LocationConstraint=ap-northeast-1").unwrap(),
"ap-northeast-1"
);
}
#[test]
fn accepts_various_region_values_verbatim() {
for region in [
"us-east-1",
"us-west-2",
"eu-west-1",
"ap-northeast-1",
"me-central-1",
"some-future-region-99",
] {
let input = format!("LocationConstraint={region}");
assert_eq!(parse_create_bucket_configuration(&input).unwrap(), region);
}
}
#[test]
fn rejects_missing_or_wrong_key() {
for value in [
"ap-northeast-1", "Location=ap-northeast-1", "locationconstraint=ap-northeast-1", "LocationConstraint", "LocationConstraint ap-northeast-1", "LocationType=AvailabilityZone", ] {
let err = parse_create_bucket_configuration(value).unwrap_err();
assert!(
err.contains("only LocationConstraint="),
"expected {value:?} to be rejected with the LocationConstraint hint"
);
}
}
#[test]
fn rejects_empty_region() {
assert!(parse_create_bucket_configuration("LocationConstraint=").is_err());
}
#[test]
fn rejects_multiple_pairs() {
assert!(
parse_create_bucket_configuration("LocationConstraint=ap-northeast-1,Foo=bar").is_err()
);
assert!(parse_create_bucket_configuration("LocationConstraint=a=b").is_err());
assert!(
parse_create_bucket_configuration("LocationConstraint=ap-northeast-1,LocationType=Foo")
.is_err()
);
}
}