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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
mod client;
mod object;
mod resources;
pub use object::ObjectClient;
pub use resources::object::{
Bucket, Metadata, Object, ObjectMetadata, ObjectsListRequest, PartialObject,
};
pub mod credentials {
pub mod serviceaccount {
use crate::gcp::oauth2::token::ServiceAccountCredentials;
pub async fn default(
scope: &str,
) -> super::super::StorageResult<ServiceAccountCredentials> {
ServiceAccountCredentials::default()
.await
.map(|x| x.with_scope(scope))
.map_err(super::super::Error::GcsTokenError)
}
pub fn from_str(
str: &str,
scope: &str,
) -> super::super::StorageResult<ServiceAccountCredentials> {
ServiceAccountCredentials::from(str)
.map(|x| x.with_scope(scope))
.map_err(super::super::Error::GcsTokenError)
}
pub async fn from_file<T>(
file_path: T,
scope: &str,
) -> super::super::StorageResult<ServiceAccountCredentials>
where
T: AsRef<std::path::Path>,
{
ServiceAccountCredentials::from_file(file_path)
.await
.map(|x| x.with_scope(scope))
.map_err(super::super::Error::GcsTokenError)
}
}
pub mod authorizeduser {
use crate::gcp::oauth2::token::AuthorizedUserCredentials;
pub async fn default() -> super::super::StorageResult<AuthorizedUserCredentials> {
AuthorizedUserCredentials::default()
.await
.map_err(super::super::Error::GcsTokenError)
}
pub fn from_str(str: &str) -> super::super::StorageResult<AuthorizedUserCredentials> {
AuthorizedUserCredentials::from(str).map_err(super::super::Error::GcsTokenError)
}
pub async fn from_file<T>(
file_path: T,
) -> super::super::StorageResult<AuthorizedUserCredentials>
where
T: AsRef<std::path::Path>,
{
AuthorizedUserCredentials::from_file(file_path)
.await
.map_err(super::super::Error::GcsTokenError)
}
}
pub mod metadata {
use crate::oauth2::token::GoogleMetadataServerCredentials;
pub fn default() -> super::super::StorageResult<GoogleMetadataServerCredentials> {
GoogleMetadataServerCredentials::default().map_err(super::super::Error::GcsTokenError)
}
}
}
#[derive(Debug)]
pub enum Error {
GcsTokenError(super::oauth2::Error),
GcsHttpError(reqwest::Error),
GcsUnexpectedResponse {
url: String,
value: String,
},
GcsUnexpectedJson {
url: String,
expected_type: String,
json: serde_json::Value,
},
GcsPartialResponseError(String),
GcsInvalidUrl {
url: String,
message: String,
},
GcsInvalidObjectName,
GcsResourceNotFound {
url: String,
},
InvalidMetadata {
expected_type: String,
error: serde_json::Error,
},
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for Error {}
impl Error {
fn gcs_invalid_metadata<T>(error: serde_json::Error) -> Self {
Self::InvalidMetadata {
expected_type: std::any::type_name::<T>().to_owned(),
error,
}
}
fn gcs_unexpected_response_error<T, U>(url: T, value: U) -> Self
where
T: AsRef<str>,
U: AsRef<str>,
{
Self::GcsUnexpectedResponse {
url: url.as_ref().to_owned(),
value: value.as_ref().to_owned(),
}
}
fn gcs_unexpected_json<T>(url: &str, json: serde_json::Value) -> Self {
let expected_type = std::any::type_name::<T>().to_owned();
Self::GcsUnexpectedJson {
url: url.to_owned(),
expected_type,
json,
}
}
}
pub type StorageResult<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use crate::storage::Error;
#[test]
fn test_error_display() {
let e = Error::gcs_unexpected_response_error("url", "value");
let actual = format!("{}", e);
assert_eq!(
"GcsUnexpectedResponse { url: \"url\", value: \"value\" }",
actual
);
}
}