use std::collections::HashMap;
#[derive(Debug, PartialEq)]
pub enum ObjectAcl {
DEFAULT,
PRIVATE,
PublicRead,
AuthenticatedRead,
BucketOwnerRead,
BucketOwnerFullControl,
}
#[derive(Debug, PartialEq)]
pub enum BucketAcl {
PRIVATE,
PublicRead,
PublicReadWrite,
AuthenticatedRead,
}
#[derive(Debug, Default)]
pub struct AclHeader {
headers: HashMap<String, String>,
}
impl AclHeader {
pub fn new() -> AclHeader {
AclHeader {
headers: HashMap::new(),
}
}
pub fn get_headers(&self) -> &HashMap<String, String> {
&self.headers
}
pub fn insert_object_x_cos_acl(&mut self, x_cos_acl: ObjectAcl) -> &mut Self {
let v = match x_cos_acl {
ObjectAcl::AuthenticatedRead => "authenticated-read",
ObjectAcl::DEFAULT => "default",
ObjectAcl::PublicRead => "public-read",
ObjectAcl::PRIVATE => "private",
ObjectAcl::BucketOwnerRead => "bucket-owner-read",
ObjectAcl::BucketOwnerFullControl => "bucket-owner-full-control",
};
self.headers.insert("x-cos-acl".to_string(), v.to_string());
self
}
pub fn insert_x_cos_grant_read(&mut self, x_cos_grant_read: String) -> &mut Self {
self.headers
.insert("x-cos-grant-read".to_string(), x_cos_grant_read);
self
}
pub fn insert_x_cos_grant_read_acp(&mut self, x_cos_grant_read_acp: String) -> &mut Self {
self.headers
.insert("x-cos-grant-read-acp".to_string(), x_cos_grant_read_acp);
self
}
pub fn insert_x_cos_grant_write_acp(&mut self, x_cos_grant_write_acp: String) -> &mut Self {
self.headers
.insert("x-cos-grant-write-acp".to_string(), x_cos_grant_write_acp);
self
}
pub fn insert_x_cos_grant_full_control(
&mut self,
x_cos_grant_full_control: String,
) -> &mut Self {
self.headers.insert(
"x-cos-grant-full-control".to_string(),
x_cos_grant_full_control,
);
self
}
pub fn insert_bucket_x_cos_acl(&mut self, x_cos_acl: BucketAcl) -> &mut Self {
let v = match x_cos_acl {
BucketAcl::AuthenticatedRead => "authenticated-read",
BucketAcl::PRIVATE => "private",
BucketAcl::PublicRead => "publish-read",
BucketAcl::PublicReadWrite => "public-read-write",
};
self.headers.insert("x-cos-acl".to_string(), v.to_string());
self
}
pub fn insert_bucket_x_cos_grant_write(&mut self, x_cos_grant_write: String) -> &mut Self {
self.headers
.insert("x-cos-grant-write".to_string(), x_cos_grant_write);
self
}
}
#[cfg(test)]
mod test {
use crate::acl;
#[test]
fn test_acl() {
let mut acl_header = acl::AclHeader::new();
acl_header
.insert_bucket_x_cos_acl(acl::BucketAcl::PublicRead)
.insert_x_cos_grant_read("x-cos-grant-read".to_string())
.insert_x_cos_grant_read_acp("x_cos_grant_read_acp".to_string())
.insert_x_cos_grant_write_acp("x_cos_grant_write_acp".to_string())
.insert_bucket_x_cos_grant_write("x_cos_grant_write".to_string());
assert_eq!(acl_header.headers["x-cos-acl"], "publish-read".to_string());
assert_eq!(
acl_header.headers["x-cos-grant-read"],
"x-cos-grant-read".to_string()
);
assert_eq!(
acl_header.headers["x-cos-grant-read-acp"],
"x_cos_grant_read_acp".to_string()
);
assert_eq!(
acl_header.headers["x-cos-grant-write-acp"],
"x_cos_grant_write_acp".to_string()
);
assert_eq!(
acl_header.headers["x-cos-grant-write"],
"x_cos_grant_write".to_string()
);
}
}