feat: with base envelope

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-09-17 00:31:43 +02:00
parent c6b903fa58
commit 795d009cd0
10 changed files with 1279 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
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> {
let output = serde_json::to_vec(&Envelope {
content: general_purpose::URL_SAFE_NO_PAD.encode(content),
metadata: Metadata {
domain: domain.to_string(),
entity: entity.to_string(),
},
})
.unwrap();
output
}
pub fn unwrap<'a>(message: &'a [u8]) -> Result<(Vec<u8>, Metadata), EnvelopeError> {
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,
))
}