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
50
51
52
//! This module houses various implementations of [`BinarySerializable`] which
//! are natively supported payloads for secrets. This is intentionally not
//! implemented as a global impl for all `TryFrom<u8>`/`TryInto<u8>` as the
//! binary conversion must be stable to survive cold storage.

use crate::errors::StdError;

/// A trait enabling saving a secret of the implementing type to the secrets
/// store.
pub trait BinarySerializable {
    fn serialize<'a>(&'a self) -> &'a [u8];
}

/// A trait enabling retrieving a secret from the secrets store as an instance
/// of the implementing type.
pub trait BinaryDeserializable
where
    Self: Sized,
{
    fn deserialize(bytes: Vec<u8>) -> Result<Self, Box<StdError>>;
}

impl BinarySerializable for String {
    fn serialize<'a>(&'a self) -> &'a [u8] {
        self.as_bytes()
    }
}

impl BinaryDeserializable for String {
    fn deserialize(bytes: Vec<u8>) -> Result<Self, Box<StdError>> {
        let s = String::from_utf8(bytes)?;
        Ok(s)
    }
}

impl BinarySerializable for &str {
    fn serialize<'a>(&'a self) -> &'a [u8] {
        self.as_bytes()
    }
}

impl BinarySerializable for &[u8] {
    fn serialize<'a>(&'a self) -> &'a [u8] {
        self
    }
}

impl BinaryDeserializable for Vec<u8> {
    fn deserialize(bytes: Vec<u8>) -> Result<Self, Box<StdError>> {
        Ok(bytes)
    }
}