gcs_rsync/gcp/storage/
mod.rs

1mod client;
2mod object;
3mod resources;
4
5pub use object::ObjectClient;
6pub use resources::object::{
7    Bucket, Metadata, Object, ObjectMetadata, ObjectsListRequest, PartialObject,
8};
9
10pub mod credentials {
11
12    pub mod serviceaccount {
13
14        use crate::gcp::oauth2::token::ServiceAccountCredentials;
15
16        pub async fn default(
17            scope: &str,
18        ) -> super::super::StorageResult<ServiceAccountCredentials> {
19            ServiceAccountCredentials::default()
20                .await
21                .map(|x| x.with_scope(scope))
22                .map_err(super::super::Error::GcsTokenError)
23        }
24
25        pub fn from_str(
26            str: &str,
27            scope: &str,
28        ) -> super::super::StorageResult<ServiceAccountCredentials> {
29            ServiceAccountCredentials::from(str)
30                .map(|x| x.with_scope(scope))
31                .map_err(super::super::Error::GcsTokenError)
32        }
33
34        pub async fn from_file<T>(
35            file_path: T,
36            scope: &str,
37        ) -> super::super::StorageResult<ServiceAccountCredentials>
38        where
39            T: AsRef<std::path::Path>,
40        {
41            ServiceAccountCredentials::from_file(file_path)
42                .await
43                .map(|x| x.with_scope(scope))
44                .map_err(super::super::Error::GcsTokenError)
45        }
46    }
47
48    pub mod authorizeduser {
49
50        use crate::gcp::oauth2::token::AuthorizedUserCredentials;
51
52        pub async fn default() -> super::super::StorageResult<AuthorizedUserCredentials> {
53            AuthorizedUserCredentials::default()
54                .await
55                .map_err(super::super::Error::GcsTokenError)
56        }
57
58        pub fn from_str(str: &str) -> super::super::StorageResult<AuthorizedUserCredentials> {
59            AuthorizedUserCredentials::from(str).map_err(super::super::Error::GcsTokenError)
60        }
61
62        pub async fn from_file<T>(
63            file_path: T,
64        ) -> super::super::StorageResult<AuthorizedUserCredentials>
65        where
66            T: AsRef<std::path::Path>,
67        {
68            AuthorizedUserCredentials::from_file(file_path)
69                .await
70                .map_err(super::super::Error::GcsTokenError)
71        }
72    }
73
74    pub mod metadata {
75
76        use crate::oauth2::token::GoogleMetadataServerCredentials;
77
78        pub fn default() -> super::super::StorageResult<GoogleMetadataServerCredentials> {
79            GoogleMetadataServerCredentials::new().map_err(super::super::Error::GcsTokenError)
80        }
81        pub fn with_scope(
82            scope: &str,
83        ) -> super::super::StorageResult<GoogleMetadataServerCredentials> {
84            GoogleMetadataServerCredentials::new()
85                .map(|x| x.with_scope(scope))
86                .map_err(super::super::Error::GcsTokenError)
87        }
88    }
89}
90
91#[derive(Debug)]
92pub enum Error {
93    GcsTokenError(super::oauth2::Error),
94    GcsHttpJsonRequestError(reqwest::Error),
95    GcsHttpJsonResponseError(reqwest::Error),
96    GcsHttpBytesStreamError(reqwest::Error),
97    GcsHttpGetAsStreamError(reqwest::Error),
98    GcsHttpPostMultipartError(reqwest::Error),
99    GcsHttpPostError(reqwest::Error),
100    GcsHttpDeleteError(reqwest::Error),
101    GcsHttpNoTextError(reqwest::Error),
102    GcsUnexpectedResponse {
103        url: String,
104        value: String,
105    },
106    GcsUnexpectedJson {
107        url: String,
108        expected_type: String,
109        json: serde_json::Value,
110    },
111    GcsPartialResponseError(String),
112    GcsInvalidUrl {
113        url: String,
114        message: String,
115    },
116    GcsInvalidObjectName,
117    GcsResourceNotFound {
118        url: String,
119    },
120    InvalidMetadata {
121        expected_type: String,
122        error: serde_json::Error,
123    },
124}
125
126impl std::fmt::Display for Error {
127    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
128        write!(f, "{:?}", self)
129    }
130}
131impl std::error::Error for Error {}
132
133impl Error {
134    fn gcs_invalid_metadata<T>(error: serde_json::Error) -> Self {
135        Self::InvalidMetadata {
136            expected_type: std::any::type_name::<T>().to_owned(),
137            error,
138        }
139    }
140    fn gcs_unexpected_response_error<T, U>(url: T, value: U) -> Self
141    where
142        T: AsRef<str>,
143        U: AsRef<str>,
144    {
145        Self::GcsUnexpectedResponse {
146            url: url.as_ref().to_owned(),
147            value: value.as_ref().to_owned(),
148        }
149    }
150
151    fn gcs_unexpected_json<T>(url: &str, json: serde_json::Value) -> Self {
152        let expected_type = std::any::type_name::<T>().to_owned();
153        Self::GcsUnexpectedJson {
154            url: url.to_owned(),
155            expected_type,
156            json,
157        }
158    }
159}
160
161pub type StorageResult<T> = std::result::Result<T, Error>;
162
163#[cfg(test)]
164mod tests {
165    use crate::storage::Error;
166    #[test]
167    fn test_error_display() {
168        let e = Error::gcs_unexpected_response_error("url", "value");
169        let actual = format!("{}", e);
170
171        assert_eq!(
172            "GcsUnexpectedResponse { url: \"url\", value: \"value\" }",
173            actual
174        );
175    }
176}