Skip to main content

xmtp_db/encrypted_store/consent_record/
convert.rs

1use super::*;
2
3impl TryFrom<ConsentSave> for StoredConsentRecord {
4    type Error = ConversionError;
5    fn try_from(value: ConsentSave) -> Result<Self, Self::Error> {
6        let entity_type = value.entity_type().try_into()?;
7        let state = value.state().try_into()?;
8
9        Ok(Self {
10            entity_type,
11            state,
12            entity: value.entity,
13            consented_at_ns: value.consented_at_ns,
14        })
15    }
16}
17
18impl From<StoredConsentRecord> for ConsentSave {
19    fn from(value: StoredConsentRecord) -> Self {
20        let entity_type: ConsentTypeSave = value.entity_type.into();
21        let state: ConsentStateSave = value.state.into();
22
23        Self {
24            entity_type: entity_type as i32,
25            state: state as i32,
26            entity: value.entity,
27            consented_at_ns: value.consented_at_ns,
28        }
29    }
30}
31
32impl From<ConsentType> for ConsentTypeSave {
33    fn from(value: ConsentType) -> Self {
34        match value {
35            ConsentType::InboxId => Self::InboxId,
36            ConsentType::ConversationId => Self::ConversationId,
37        }
38    }
39}
40
41impl TryFrom<ConsentTypeSave> for ConsentType {
42    type Error = ConversionError;
43    #[allow(deprecated)]
44    fn try_from(value: ConsentTypeSave) -> Result<Self, Self::Error> {
45        Ok(match value {
46            ConsentTypeSave::InboxId => Self::InboxId,
47            ConsentTypeSave::ConversationId => Self::ConversationId,
48            ConsentTypeSave::Address => return Err(ConversionError::Deprecated("address")),
49            ConsentTypeSave::Unspecified => {
50                return Err(ConversionError::Unspecified("consent_type"));
51            }
52        })
53    }
54}
55
56impl TryFrom<ConsentStateSave> for ConsentState {
57    type Error = ConversionError;
58    fn try_from(value: ConsentStateSave) -> Result<Self, Self::Error> {
59        Ok(match value {
60            ConsentStateSave::Allowed => Self::Allowed,
61            ConsentStateSave::Denied => Self::Denied,
62            ConsentStateSave::Unknown => Self::Unknown,
63            ConsentStateSave::Unspecified => {
64                return Err(ConversionError::Unspecified("consent_state"));
65            }
66        })
67    }
68}
69
70impl From<ConsentState> for ConsentStateSave {
71    fn from(value: ConsentState) -> Self {
72        match value {
73            ConsentState::Allowed => Self::Allowed,
74            ConsentState::Denied => Self::Denied,
75            ConsentState::Unknown => Self::Unknown,
76        }
77    }
78}