tencent_qcloud_cos_rs/
acl.rs1use 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
35pub struct AclHeader {
36 headers: HashMap<String, String>,
37}
38
39impl AclHeader {
40 pub fn new() -> AclHeader {
41 let m = HashMap::new();
42 AclHeader { headers: m }
43 }
44
45 pub fn get_headers(&self) -> &HashMap<String, String> {
46 &self.headers
47 }
48
49 pub fn insert_object_x_cos_acl(&mut self, x_cos_acl: ObjectAcl) -> &mut Self {
54 let v;
55 match x_cos_acl {
56 ObjectAcl::AuthenticatedRead => v = "authenticated-read",
57 ObjectAcl::DEFAULT => v = "default",
58 ObjectAcl::PublicRead => v = "public-read",
59 ObjectAcl::PRIVATE => v = "private",
60 ObjectAcl::BucketOwnerRead => v = "bucket-owner-read",
61 ObjectAcl::BucketOwnerFullControl => v = "bucket-owner-full-control",
62 }
63 self.headers.insert("x-cos-acl".to_string(), v.to_string());
64 self
65 }
66
67 pub fn insert_x_cos_grant_read(&mut self, x_cos_grant_read: String) -> &mut Self {
70 self.headers
71 .insert("x-cos-grant-read".to_string(), x_cos_grant_read);
72 self
73 }
74
75 pub fn insert_x_cos_grant_read_acp(&mut self, x_cos_grant_read_acp: String) -> &mut Self {
78 self.headers
79 .insert("x-cos-grant-read-acp".to_string(), x_cos_grant_read_acp);
80 self
81 }
82 pub fn insert_x_cos_grant_write_acp(&mut self, x_cos_grant_write_acp: String) -> &mut Self {
85 self.headers
86 .insert("x-cos-grant-write-acp".to_string(), x_cos_grant_write_acp);
87 self
88 }
89 pub fn insert_x_cos_grant_full_control(
92 &mut self,
93 x_cos_grant_full_control: String,
94 ) -> &mut Self {
95 self.headers.insert(
96 "x-cos-grant-full-control".to_string(),
97 x_cos_grant_full_control,
98 );
99 self
100 }
101
102 pub fn insert_bucket_x_cos_acl(&mut self, x_cos_acl: BucketAcl) -> &mut Self {
105 let v;
106 match x_cos_acl {
107 BucketAcl::AuthenticatedRead => v = "authenticated-read",
108 BucketAcl::PRIVATE => v = "private",
109 BucketAcl::PublicRead => v = "publish-read",
110 BucketAcl::PublicReadWrite => v = "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}