use std::collections::HashMap;
#[derive(Debug, PartialEq)]
pub enum ObjectAcl {
DEFAULT,
PRIVATE,
PublicRead,
AuthenticatedRead,
BucketOwnerRead,
BucketOwnerFullControl,
}
impl From<ObjectAcl> for String {
fn from(val: ObjectAcl) -> Self {
match val {
ObjectAcl::DEFAULT => String::from("default"),
ObjectAcl::PRIVATE => String::from("private"),
ObjectAcl::PublicRead => String::from("public-read"),
ObjectAcl::BucketOwnerRead => String::from("bucket-owner-read"),
ObjectAcl::AuthenticatedRead => String::from("authenticated-read"),
ObjectAcl::BucketOwnerFullControl => String::from("bucket-owner-full-control"),
}
}
}
#[derive(Debug, PartialEq)]
pub enum BucketAcl {
PRIVATE,
PublicRead,
PublicReadWrite,
AuthenticatedRead,
}
impl From<BucketAcl> for String {
fn from(value: BucketAcl) -> Self {
match value {
BucketAcl::PRIVATE => String::from("private"),
BucketAcl::PublicRead => String::from("public-read"),
BucketAcl::PublicReadWrite => String::from("public-read-write"),
BucketAcl::AuthenticatedRead => String::from("authenticated-read"),
}
}
}
#[derive(Debug, Clone)]
pub struct AclHeader {
headers: HashMap<String, String>,
}
impl Default for AclHeader {
fn default() -> Self {
Self::new()
}
}
impl AclHeader {
pub fn new() -> AclHeader {
let m = HashMap::new();
AclHeader { headers: m }
}
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 {
self.headers
.insert("x-cos-acl".to_string(), x_cos_acl.into());
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 {
self.headers
.insert("x-cos-acl".to_string(), x_cos_acl.into());
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"], "public-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()
);
}
}