2023-09-17 00:31:43 +02:00
|
|
|
use base64::{engine::general_purpose, Engine};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use crate::EnvelopeError;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Envelope {
|
|
|
|
content: String,
|
|
|
|
metadata: Metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Metadata {
|
|
|
|
domain: String,
|
|
|
|
entity: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wrap<'a>(domain: &'a str, entity: &'a str, content: &'a [u8]) -> Vec<u8> {
|
2023-09-24 01:14:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
serde_json::to_vec(&Envelope {
|
2023-09-17 00:31:43 +02:00
|
|
|
content: general_purpose::URL_SAFE_NO_PAD.encode(content),
|
|
|
|
metadata: Metadata {
|
|
|
|
domain: domain.to_string(),
|
|
|
|
entity: entity.to_string(),
|
|
|
|
},
|
|
|
|
})
|
2023-09-24 01:14:50 +02:00
|
|
|
.unwrap()
|
2023-09-17 00:31:43 +02:00
|
|
|
}
|
2023-09-24 01:14:50 +02:00
|
|
|
pub fn unwrap(message: &[u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
|
2023-09-17 00:31:43 +02:00
|
|
|
let envelope: Envelope = serde_json::from_slice(message).map_err(EnvelopeError::JsonError)?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
general_purpose::URL_SAFE_NO_PAD
|
|
|
|
.decode(envelope.content)
|
|
|
|
.map_err(EnvelopeError::Base64Error)?,
|
|
|
|
envelope.metadata,
|
|
|
|
))
|
|
|
|
}
|