integrationos_domain/domain/access_key/
access_key_data.rs

1use crate::{IntegrationOSError, InternalError};
2use napi_derive::napi;
3use prost::Message;
4
5#[derive(Clone, Eq, PartialEq, prost::Message)]
6#[napi(object)]
7#[cfg_attr(feature = "dummy", derive(fake::Dummy))]
8pub struct AccessKeyData {
9    #[prost(string, tag = "1")]
10    pub id: String,
11    #[prost(string, tag = "2")]
12    pub namespace: String,
13    #[prost(string, tag = "3")]
14    pub event_type: String,
15    #[prost(string, tag = "4")]
16    pub group: String,
17    #[prost(string, tag = "5")]
18    pub event_path: String,
19    #[prost(string, optional, tag = "6")]
20    pub event_object_id_path: Option<String>,
21    #[prost(string, optional, tag = "7")]
22    pub timestamp_path: Option<String>,
23    #[prost(string, optional, tag = "8")]
24    pub parent_access_key: Option<String>,
25}
26
27impl TryFrom<&[u8]> for AccessKeyData {
28    type Error = IntegrationOSError;
29
30    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
31        Self::from_slice(value)
32    }
33}
34
35impl AccessKeyData {
36    pub fn from_slice(vec: &[u8]) -> Result<Self, IntegrationOSError> {
37        AccessKeyData::decode(vec).map_err(|e| {
38            InternalError::decryption_error(
39                &e.to_string(),
40                Some("Failed to decode access key data"),
41            )
42        })
43    }
44
45    pub fn to_vec(&self) -> Result<Vec<u8>, IntegrationOSError> {
46        let mut buf = Vec::with_capacity(self.encoded_len());
47        self.encode(&mut buf).map_err(|e| {
48            InternalError::encryption_error(
49                &e.to_string(),
50                Some("Failed to encode access key data"),
51            )
52        })?;
53        Ok(buf)
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_access_key_data() {
63        let access_key_data = AccessKeyData {
64            id: "foo".to_owned(),
65            namespace: "bar".to_owned(),
66            event_type: "baz".to_owned(),
67            group: "qux".to_owned(),
68            event_path: "quux".to_owned(),
69            event_object_id_path: Some("quuz".to_owned()),
70            timestamp_path: None,
71            parent_access_key: None,
72        };
73        let vec = access_key_data.to_vec().unwrap();
74        assert_eq!(access_key_data, AccessKeyData::from_slice(&vec).unwrap());
75    }
76}