coven_storage/provider_account.rs
1//! The provider account a sign-in reaches, and the account an invite shared
2//! the store's folder with (ยง13.2).
3
4use crate::{StorageError, StorageFailure};
5use coven_format::MemberAccess;
6use serde::{Deserialize, Serialize};
7
8/// The account a Dropbox or OneDrive sign-in belongs to, as the provider
9/// reports it: its primary address and, where an invite's share names the
10/// account by the same id, that id (Dropbox's `account_id`).
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct ProviderAccount {
13 /// The provider's id for the account, which no address change alters.
14 /// None on OneDrive: Microsoft's documentation does not say a
15 /// permission's user id is the id `/me` returns, so OneDrive accounts are
16 /// compared by address.
17 pub id: Option<String>,
18 /// The account's primary address, normalized as
19 /// [`MemberAccess::normalized_account`] normalizes one.
20 pub address: String,
21}
22
23/// The account an invite shared the store's folder with, as the inviting
24/// device recorded it when the provider granted the share.
25#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
26pub struct SharedWith {
27 address: String,
28 id: Option<String>,
29}
30
31impl SharedWith {
32 /// The share's account: the address the invite was sent to, and the
33 /// provider's id for the account when the share reports one. A share sent
34 /// to an address with no account yet has no id.
35 pub fn new(address: &str, id: Option<String>) -> Result<Self, StorageError> {
36 let address = MemberAccess::normalized_account(address);
37 if address.is_empty() || id.as_deref().is_some_and(str::is_empty) {
38 return Err(StorageFailure::InvalidConfiguration
39 .with_source("an invite's shared-with account is empty"));
40 }
41 Ok(Self { address, id })
42 }
43
44 /// The address the invite was sent to, which names the account to the
45 /// person.
46 pub fn address(&self) -> &str {
47 &self.address
48 }
49
50 /// The provider's id for the account, when the share named one (Dropbox's
51 /// `account_id`, which Dropbox documents as the account's unique and stable
52 /// ID in `users/get_current_account` and, as `UserInfo.account_id`, in a
53 /// folder's member list).
54 pub fn id(&self) -> Option<&str> {
55 self.id.as_deref()
56 }
57
58 /// Whether `signed_in` is the account the folder was shared with: by the
59 /// provider's id when the share named one, so an alias or secondary
60 /// address of the account is never read as another account; else by
61 /// primary address.
62 pub fn is(&self, signed_in: &ProviderAccount) -> bool {
63 match &self.id {
64 Some(id) => signed_in.id.as_deref() == Some(id.as_str()),
65 None => MemberAccess::same_account(&self.address, &signed_in.address),
66 }
67 }
68}
69
70#[cfg(test)]
71#[path = "provider_account_tests.rs"]
72mod tests;