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
40
41
42
43
44
45
46
47
48
49
use crate::error::Error;
use alloc::{format, vec::Vec};
use core::marker::PhantomData;
use serde::{Deserialize, Serialize};
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct SerializedPlain<T> {
serialized_plain: Vec<u8>,
_type: PhantomData<T>,
}
impl<T> SerializedPlain<T> {
pub(in crate::traits) fn new(serialized_plain: Vec<u8>) -> Self {
Self {
serialized_plain,
_type: PhantomData::default(),
}
}
pub fn deserialize<'de>(&'de self) -> Result<T, Error>
where
T: Sized + Deserialize<'de>,
{
serde_cbor::from_slice(&self.serialized_plain).map_err(|e| {
Error::deserialization_error(&format!(
"error on serde_cbor deserialization after decryption: {:?}",
e
))
})
}
}
pub(in crate::traits) fn serialize<T>(v: &T) -> Result<Vec<u8>, Error>
where
T: Serialize,
{
serde_cbor::to_vec(v).map_err(|e| {
Error::serialization_error(&format!("failed to serialize data by serde_cbor: {:?}", e))
})
}