Skip to main content

coven_format/
member_access.rs

1//! The storage account carried by member additions and access updates (format.md D6).
2
3use crate::wire::{Decoder, Encoder, Wire};
4use crate::Error;
5use serde::{Deserialize, Serialize};
6
7/// The longest account or key id member access carries (D6).
8pub(crate) const MAX_TEXT: usize = 1024;
9
10/// An account to share with, or the id of a member's S3 key.
11#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
12pub enum MemberAccess {
13    /// The member's provider account email.
14    ProviderAccount(String),
15    /// A member's S3 key, made in the provider's console.
16    S3AccessKey {
17        /// Public identifier; never the secret access key.
18        access_key_id: String,
19    },
20}
21
22/// `MemberAccess::from_columns` returns this error when a row holds both an
23/// account and an S3 key id: a member's access holds only one of them.
24#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
25#[error("a member's access holds an account or an S3 key id, not both")]
26pub struct BothAccesses;
27
28impl MemberAccess {
29    /// The access the `account` and `access_key_id` columns of a
30    /// `coven_members` row hold, from the columns themselves or from the
31    /// text a JSON object of them holds (api.md E4): `None` when both are
32    /// NULL, as for the store's creator until Set up storage records theirs.
33    pub fn from_columns(
34        account: Option<String>,
35        access_key_id: Option<String>,
36    ) -> Result<Option<Self>, BothAccesses> {
37        match (account, access_key_id) {
38            (None, None) => Ok(None),
39            (Some(account), None) => Ok(Some(Self::ProviderAccount(account))),
40            (None, Some(access_key_id)) => Ok(Some(Self::S3AccessKey { access_key_id })),
41            (Some(_), Some(_)) => Err(BothAccesses),
42        }
43    }
44
45    /// Whether both name the same credential: one account, compared without
46    /// case or surrounding space, or one S3 key id.
47    pub fn same_credential(&self, other: &Self) -> bool {
48        match (self, other) {
49            (Self::ProviderAccount(a), Self::ProviderAccount(b)) => Self::same_account(a, b),
50            (Self::S3AccessKey { access_key_id: a }, Self::S3AccessKey { access_key_id: b }) => {
51                a == b
52            }
53            _ => false,
54        }
55    }
56
57    /// Whether two provider accounts are one: without case or surrounding space.
58    pub fn same_account(a: &str, b: &str) -> bool {
59        a.trim().eq_ignore_ascii_case(b.trim())
60    }
61
62    /// An account in the one spelling its equal spellings share: trimmed and
63    /// lowercase.
64    pub fn normalized_account(account: &str) -> String {
65        account.trim().to_ascii_lowercase()
66    }
67
68    /// Whether both are the same kind of access: provider accounts, or S3
69    /// keys.
70    pub fn same_kind(&self, other: &Self) -> bool {
71        matches!(
72            (self, other),
73            (Self::ProviderAccount(_), Self::ProviderAccount(_))
74                | (Self::S3AccessKey { .. }, Self::S3AccessKey { .. })
75        )
76    }
77}
78
79fn get_text(input: &mut Decoder<'_>) -> Result<String, Error> {
80    crate::wire::get_text(input, MAX_TEXT, "member access")
81}
82
83impl Wire for MemberAccess {
84    fn put(&self, out: &mut Encoder) -> Result<(), Error> {
85        match self {
86            Self::ProviderAccount(account) => {
87                crate::error::bound(account.len(), MAX_TEXT, "member access")?;
88                0u8.put(out)?;
89                account.put(out)
90            }
91            Self::S3AccessKey { access_key_id } => {
92                crate::error::bound(access_key_id.len(), MAX_TEXT, "member access")?;
93                1u8.put(out)?;
94                access_key_id.put(out)
95            }
96        }
97    }
98    fn get(input: &mut Decoder<'_>) -> Result<Self, Error> {
99        match u8::get(input)? {
100            0 => Ok(Self::ProviderAccount(get_text(input)?)),
101            1 => Ok(Self::S3AccessKey { access_key_id: get_text(input)? }),
102            tag => Err(Error::UnknownTag { field: "member access", tag }),
103        }
104    }
105}
106
107#[cfg(test)]
108#[path = "member_access_tests.rs"]
109mod tests;