-
Notifications
You must be signed in to change notification settings - Fork 83
/
plaintext.rs
39 lines (34 loc) · 965 Bytes
/
plaintext.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use crate::envelope::EnvelopeExt;
use crate::error::Result;
use identity_core::convert::ToJson as _;
use serde::Deserialize;
use serde::Serialize;
/// A DIDComm Plaintext Message
///
/// [Reference](https://identity.foundation/didcomm-messaging/spec/#didcomm-plaintext-messages)
///
/// # Layout
///
/// `JWM(Content)`
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Envelope(String);
impl Envelope {
pub fn from_message<T: Serialize>(message: &T) -> Result<Self> {
message.to_json().map_err(Into::into).map(Self)
}
pub fn to_message<T>(&self) -> Result<T>
where
for<'a> T: Deserialize<'a>,
{
serde_json::from_str(&self.0).map_err(Into::into)
}
}
impl EnvelopeExt for Envelope {
const FEXT: &'static str = "dcpm";
const MIME: &'static str = "application/didcomm-plain+json";
fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}