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    fn try_from(value: ConsentTypeSave) -> Result<Self, Self::Error> {
44        Ok(match value {
45            ConsentTypeSave::InboxId => Self::InboxId,
46            ConsentTypeSave::ConversationId => Self::ConversationId,
47            ConsentTypeSave::Address => return Err(ConversionError::Deprecated("address")),
48            ConsentTypeSave::Unspecified => {
49                return Err(ConversionError::Unspecified("consent_type"));
50            }
51        })
52    }
53}
54
55impl TryFrom<ConsentStateSave> for ConsentState {
56    type Error = ConversionError;
57    fn try_from(value: ConsentStateSave) -> Result<Self, Self::Error> {
58        Ok(match value {
59            ConsentStateSave::Allowed => Self::Allowed,
60            ConsentStateSave::Denied => Self::Denied,
61            ConsentStateSave::Unknown => Self::Unknown,
62            ConsentStateSave::Unspecified => {
63                return Err(ConversionError::Unspecified("consent_state"));
64            }
65        })
66    }
67}
68
69impl From<ConsentState> for ConsentStateSave {
70    fn from(value: ConsentState) -> Self {
71        match value {
72            ConsentState::Allowed => Self::Allowed,
73            ConsentState::Denied => Self::Denied,
74            ConsentState::Unknown => Self::Unknown,
75        }
76    }
77}