1use coven_format::code_fields::{FieldReader, FieldWriter};
2use coven_format::Error as FieldError;
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub enum CloudProvider {
8 S3,
10 Dropbox,
12 OneDrive,
14 CloudKit,
16}
17
18coven_foundation::column_names!(CloudProvider, "provider", {
20 S3 => "S3",
21 Dropbox => "Dropbox",
22 OneDrive => "OneDrive",
23 CloudKit => "CloudKit",
24});
25
26impl CloudProvider {
27 pub fn sign_out(self) -> Option<crate::ProviderSignOut> {
31 match self {
32 Self::S3 => None,
33 Self::CloudKit => Some(crate::ProviderSignOut::RemoveFromAppleAccount),
34 provider => Some(crate::ProviderSignOut::RemoveAppAccess { provider }),
35 }
36 }
37
38 pub fn signs_in_with_oauth(self) -> bool {
41 matches!(self, Self::Dropbox | Self::OneDrive)
42 }
43}
44
45#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
47#[serde(deny_unknown_fields)]
48pub enum StorageConfig {
49 S3 {
53 bucket: String,
55 region: String,
57 endpoint: Option<url::Url>,
59 profile: Option<crate::S3Profile>,
66 },
67 Dropbox {
69 location: crate::DropboxLocation,
71 account_label: Option<String>,
73 folder_label: Option<FolderLabel>,
76 },
77 OneDrive {
79 drive_id: String,
81 folder_id: String,
83 account_label: Option<String>,
85 folder_label: Option<FolderLabel>,
88 },
89 CloudKit {
91 container: String,
93 owner: String,
95 zone: String,
97 },
98}
99
100#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
104#[serde(deny_unknown_fields)]
105pub struct FolderLabel {
106 pub name: String,
108 pub web_url: url::Url,
110}
111
112impl FolderLabel {
113 pub(crate) fn answered(name: &str, web_url: &str) -> Result<Self, crate::StorageError> {
116 let web_url = url::Url::parse(web_url)
117 .map_err(|error| crate::StorageFailure::Protocol.with_source(error))?;
118 let label = Self { name: name.into(), web_url };
119 label.validate().map_err(|error| crate::StorageFailure::Protocol.with_source(error))?;
120 Ok(label)
121 }
122
123 fn validate(&self) -> Result<(), crate::StorageError> {
125 if self.name.trim().is_empty() || self.name.chars().any(char::is_control) {
126 return Err(crate::StorageFailure::InvalidConfiguration
127 .with_source("empty or invalid folder name"));
128 }
129 if self.web_url.scheme() != "https" || !crate::web_url::is_web_url(&self.web_url) {
130 return Err(crate::StorageFailure::InvalidConfiguration
131 .with_source("a folder's web address must be https"));
132 }
133 Ok(())
134 }
135}
136
137impl StorageConfig {
138 pub fn same_location(&self, other: &Self) -> bool {
140 match (self, other) {
141 (Self::Dropbox { location: a, .. }, Self::Dropbox { location: b, .. }) => a == b,
142 (
143 Self::OneDrive { drive_id: ad, folder_id: af, .. },
144 Self::OneDrive { drive_id: bd, folder_id: bf, .. },
145 ) => ad == bd && af == bf,
146 (Self::S3 { .. }, Self::S3 { .. }) | (Self::CloudKit { .. }, Self::CloudKit { .. }) => {
148 self == other
149 }
150 _ => false,
151 }
152 }
153
154 pub fn same_s3_bucket(&self, other: &Self) -> bool {
157 match (self, other) {
158 (
159 Self::S3 { bucket: ab, region: ar, endpoint: ae, .. },
160 Self::S3 { bucket: bb, region: br, endpoint: be, .. },
161 ) => ab == bb && ar == br && ae == be,
162 _ => false,
163 }
164 }
165
166 pub fn holds_one_store(&self) -> bool {
171 match self {
172 Self::S3 { .. } | Self::OneDrive { .. } => true,
173 Self::Dropbox { location, .. } => match location {
174 crate::DropboxLocation::SharedFolder { .. } => true,
175 crate::DropboxLocation::AppFolder => false,
176 },
177 Self::CloudKit { .. } => false,
178 }
179 }
180
181 pub fn oauth_access(&self) -> Option<crate::OAuthAccess> {
183 match self {
184 Self::Dropbox { location, .. } => Some(crate::OAuthAccess::Dropbox(location.access())),
185 Self::OneDrive { .. } => Some(crate::OAuthAccess::OneDrive),
186 Self::S3 { .. } | Self::CloudKit { .. } => None,
187 }
188 }
189 pub fn provider(&self) -> CloudProvider {
191 match self {
192 Self::S3 { .. } => CloudProvider::S3,
193 Self::Dropbox { .. } => CloudProvider::Dropbox,
194 Self::OneDrive { .. } => CloudProvider::OneDrive,
195 Self::CloudKit { .. } => CloudProvider::CloudKit,
196 }
197 }
198
199 pub fn validate(&self) -> Result<(), crate::StorageError> {
201 let fields: Vec<&str> = match self {
202 Self::S3 { bucket, region, endpoint, profile } => {
203 if let Some(endpoint) = endpoint {
204 if !crate::web_url::is_web_url(endpoint)
205 || endpoint.query().is_some()
206 || endpoint.fragment().is_some()
207 {
208 return Err(
209 crate::StorageFailure::InvalidConfiguration.with_source("S3 endpoint")
210 );
211 }
212 }
213 crate::providers::check_s3_profile(endpoint.as_ref(), profile.as_ref())?;
214 vec![bucket, region]
215 }
216 Self::Dropbox {
217 location: crate::DropboxLocation::AppFolder,
218 folder_label: Some(_),
219 ..
220 } => {
221 return Err(crate::StorageFailure::InvalidConfiguration
222 .with_source("the Dropbox App folder has no folder label"));
223 }
224 Self::Dropbox { location: crate::DropboxLocation::AppFolder, .. } => vec![],
225 Self::Dropbox {
226 location: crate::DropboxLocation::SharedFolder { namespace_id },
227 folder_label,
228 ..
229 } => {
230 if let Some(label) = folder_label {
231 label.validate()?;
232 }
233 vec![namespace_id]
234 }
235 Self::OneDrive { drive_id, folder_id, folder_label, .. } => {
236 if let Some(label) = folder_label {
237 label.validate()?;
238 }
239 vec![drive_id, folder_id]
240 }
241 Self::CloudKit { container, owner, zone } => vec![container, owner, zone],
242 };
243 if fields.iter().any(|value| value.trim().is_empty() || value.chars().any(char::is_control))
244 {
245 return Err(crate::StorageFailure::InvalidConfiguration
246 .with_source("empty or invalid location"));
247 }
248 Ok(())
249 }
250}
251
252const S3: u8 = 0;
255const DROPBOX: u8 = 1;
256const ONEDRIVE: u8 = 2;
257const CLOUDKIT: u8 = 3;
258
259impl StorageConfig {
260 pub(crate) fn pack(&self, out: &mut FieldWriter) -> Result<(), FieldError> {
262 match self {
263 Self::S3 { bucket, region, endpoint, profile } => {
264 out.u8(S3)?;
265 out.text(bucket)?;
266 out.text(region)?;
267 out.option(endpoint.as_ref(), |out, url| out.text(url.as_str()))?;
268 out.option(profile.as_ref(), |out, profile| profile.pack(out))
269 }
270 Self::Dropbox { location, account_label, folder_label: _ } => {
272 out.u8(DROPBOX)?;
273 match location {
274 crate::DropboxLocation::AppFolder => out.u8(0)?,
275 crate::DropboxLocation::SharedFolder { namespace_id } => {
276 out.u8(1)?;
277 out.text(namespace_id)?;
278 }
279 }
280 out.option(account_label.as_deref(), FieldWriter::text)
281 }
282 Self::OneDrive { drive_id, folder_id, account_label, folder_label: _ } => {
283 out.u8(ONEDRIVE)?;
284 out.text(drive_id)?;
285 out.text(folder_id)?;
286 out.option(account_label.as_deref(), FieldWriter::text)
287 }
288 Self::CloudKit { container, owner, zone } => {
289 out.u8(CLOUDKIT)?;
290 out.text(container)?;
291 out.text(owner)?;
292 out.text(zone)
293 }
294 }
295 }
296
297 pub(crate) fn unpack(input: &mut FieldReader<'_>) -> Result<Self, crate::StorageError> {
301 let location = match input.u8()? {
302 S3 => Self::S3 {
303 bucket: input.text("bucket")?,
304 region: input.text("region")?,
305 endpoint: input
306 .option(|input| input.text("endpoint"))?
307 .map(|text| recorded_url(&text))
308 .transpose()?,
309 profile: input.option(crate::S3Profile::unpack)?,
310 },
311 DROPBOX => Self::Dropbox {
312 location: match input.u8()? {
313 0 => crate::DropboxLocation::AppFolder,
314 1 => crate::DropboxLocation::SharedFolder {
315 namespace_id: input.text("namespace id")?,
316 },
317 tag => {
318 return Err(FieldError::UnknownTag { field: "Dropbox location", tag }.into())
319 }
320 },
321 account_label: input.option(|input| input.text("account label"))?,
322 folder_label: None,
323 },
324 ONEDRIVE => Self::OneDrive {
325 drive_id: input.text("drive id")?,
326 folder_id: input.text("folder id")?,
327 account_label: input.option(|input| input.text("account label"))?,
328 folder_label: None,
329 },
330 CLOUDKIT => Self::CloudKit {
331 container: input.text("container")?,
332 owner: input.text("owner")?,
333 zone: input.text("zone")?,
334 },
335 tag => return Err(FieldError::UnknownTag { field: "provider", tag }.into()),
336 };
337 location.validate()?;
338 Ok(location)
339 }
340}
341
342fn recorded_url(text: &str) -> Result<url::Url, crate::StorageError> {
344 let url = url::Url::parse(text)
345 .map_err(|error| crate::StorageFailure::Encoding.with_source(error))?;
346 if url.as_str() != text {
347 return Err(crate::StorageFailure::Encoding
348 .with_source("a recorded S3 endpoint is not in its canonical spelling"));
349 }
350 Ok(url)
351}
352
353#[cfg(test)]
354#[path = "config_tests.rs"]
355mod tests;