coven_format/
member_access.rs1use crate::wire::{Decoder, Encoder, Wire};
4use crate::Error;
5use serde::{Deserialize, Serialize};
6
7pub(crate) const MAX_TEXT: usize = 1024;
9
10#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
12pub enum MemberAccess {
13 ProviderAccount(String),
15 S3AccessKey {
17 access_key_id: String,
19 },
20}
21
22#[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 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 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 pub fn same_account(a: &str, b: &str) -> bool {
59 a.trim().eq_ignore_ascii_case(b.trim())
60 }
61
62 pub fn normalized_account(account: &str) -> String {
65 account.trim().to_ascii_lowercase()
66 }
67
68 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;