1use std::collections::HashMap;
4
5#[derive(Debug, PartialEq)]
7pub enum ObjectAcl {
8 DEFAULT,
10 PRIVATE,
12 PublicRead,
14 AuthenticatedRead,
16 BucketOwnerRead,
18 BucketOwnerFullControl,
20}
21
22#[derive(Debug, PartialEq)]
24pub enum BucketAcl {
25 PRIVATE,
27 PublicRead,
29 PublicReadWrite,
31 AuthenticatedRead,
33}
34
35#[derive(Debug, Default)]
36pub struct AclHeader {
37 headers: HashMap<String, String>,
38}
39
40impl AclHeader {
41 pub fn new() -> AclHeader {
42 AclHeader {
43 headers: HashMap::new(),
44 }
45 }
46
47 pub fn get_headers(&self) -> &HashMap<String, String> {
48 &self.headers
49 }
50
51 pub fn insert_object_x_cos_acl(&mut self, x_cos_acl: ObjectAcl) -> &mut Self {
56 let v = match x_cos_acl {
57 ObjectAcl::AuthenticatedRead => "authenticated-read",
58 ObjectAcl::DEFAULT => "default",
59 ObjectAcl::PublicRead => "public-read",
60 ObjectAcl::PRIVATE => "private",
61 ObjectAcl::BucketOwnerRead => "bucket-owner-read",
62 ObjectAcl::BucketOwnerFullControl => "bucket-owner-full-control",
63 };
64 self.headers.insert("x-cos-acl".to_string(), v.to_string());
65 self
66 }
67
68 pub fn insert_x_cos_grant_read(&mut self, x_cos_grant_read: String) -> &mut Self {
71 self.headers
72 .insert("x-cos-grant-read".to_string(), x_cos_grant_read);
73 self
74 }
75
76 pub fn insert_x_cos_grant_read_acp(&mut self, x_cos_grant_read_acp: String) -> &mut Self {
79 self.headers
80 .insert("x-cos-grant-read-acp".to_string(), x_cos_grant_read_acp);
81 self
82 }
83 pub fn insert_x_cos_grant_write_acp(&mut self, x_cos_grant_write_acp: String) -> &mut Self {
86 self.headers
87 .insert("x-cos-grant-write-acp".to_string(), x_cos_grant_write_acp);
88 self
89 }
90 pub fn insert_x_cos_grant_full_control(
93 &mut self,
94 x_cos_grant_full_control: String,
95 ) -> &mut Self {
96 self.headers.insert(
97 "x-cos-grant-full-control".to_string(),
98 x_cos_grant_full_control,
99 );
100 self
101 }
102
103 pub fn insert_bucket_x_cos_acl(&mut self, x_cos_acl: BucketAcl) -> &mut Self {
106 let v = match x_cos_acl {
107 BucketAcl::AuthenticatedRead => "authenticated-read",
108 BucketAcl::PRIVATE => "private",
109 BucketAcl::PublicRead => "publish-read",
110 BucketAcl::PublicReadWrite => "public-read-write",
111 };
112 self.headers.insert("x-cos-acl".to_string(), v.to_string());
113 self
114 }
115
116 pub fn insert_bucket_x_cos_grant_write(&mut self, x_cos_grant_write: String) -> &mut Self {
119 self.headers
120 .insert("x-cos-grant-write".to_string(), x_cos_grant_write);
121 self
122 }
123}
124
125#[cfg(test)]
126mod test {
127
128 use crate::acl;
129
130 #[test]
131 fn test_acl() {
132 let mut acl_header = acl::AclHeader::new();
133 acl_header
134 .insert_bucket_x_cos_acl(acl::BucketAcl::PublicRead)
135 .insert_x_cos_grant_read("x-cos-grant-read".to_string())
136 .insert_x_cos_grant_read_acp("x_cos_grant_read_acp".to_string())
137 .insert_x_cos_grant_write_acp("x_cos_grant_write_acp".to_string())
138 .insert_bucket_x_cos_grant_write("x_cos_grant_write".to_string());
139
140 assert_eq!(acl_header.headers["x-cos-acl"], "publish-read".to_string());
141 assert_eq!(
142 acl_header.headers["x-cos-grant-read"],
143 "x-cos-grant-read".to_string()
144 );
145 assert_eq!(
146 acl_header.headers["x-cos-grant-read-acp"],
147 "x_cos_grant_read_acp".to_string()
148 );
149 assert_eq!(
150 acl_header.headers["x-cos-grant-write-acp"],
151 "x_cos_grant_write_acp".to_string()
152 );
153 assert_eq!(
154 acl_header.headers["x-cos-grant-write"],
155 "x_cos_grant_write".to_string()
156 );
157 }
158}