1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::path::S3Path;
use regex::Regex;
#[derive(Debug)]
#[allow(clippy::exhaustive_enums)]
pub enum AmzCopySource<'a> {
Bucket {
bucket: &'a str,
key: &'a str,
},
AccessPoint {
region: &'a str,
account_id: &'a str,
access_point_name: &'a str,
key: &'a str,
},
}
#[allow(missing_copy_implementations)]
#[derive(Debug, thiserror::Error)]
pub enum ParseAmzCopySourceError {
#[error("ParseAmzCopySourceError: PatternMismatch")]
PatternMismatch,
#[error("ParseAmzCopySourceError: InvalidBucketName")]
InvalidBucketName,
#[error("ParseAmzCopySourceError: InvalidKey")]
InvalidKey,
}
impl<'a> AmzCopySource<'a> {
pub fn try_match(header: &str) -> Result<(), ParseAmzCopySourceError> {
let pattern: &Regex = static_regex!(".+?/.+");
if pattern.is_match(header) {
Ok(())
} else {
Err(ParseAmzCopySourceError::PatternMismatch)
}
}
#[allow(clippy::unwrap_in_result, clippy::missing_panics_doc)]
pub fn from_header_str(header: &'a str) -> Result<Self, ParseAmzCopySourceError> {
let pattern: &Regex = static_regex!("^(.+?)/(.+)$");
match pattern.captures(header) {
None => Err(ParseAmzCopySourceError::PatternMismatch),
Some(captures) => {
let bucket = captures.get(1).unwrap().as_str();
let key = captures.get(2).unwrap().as_str();
if !S3Path::check_bucket_name(bucket) {
return Err(ParseAmzCopySourceError::InvalidBucketName);
}
if !S3Path::check_key(key) {
return Err(ParseAmzCopySourceError::InvalidKey);
}
Ok(Self::Bucket { bucket, key })
}
}
}
}