barter_integration/protocol/http/private/
encoder.rs

1use base64::Engine;
2
3/// Encodes bytes data.
4pub trait Encoder {
5    /// Encodes the bytes data into some `String` format.
6    fn encode<Bytes>(&self, data: Bytes) -> String
7    where
8        Bytes: AsRef<[u8]>;
9}
10
11/// Encodes bytes data as a hex `String` using lowercase characters.
12#[derive(Debug, Copy, Clone)]
13pub struct HexEncoder;
14
15impl Encoder for HexEncoder {
16    fn encode<Bytes>(&self, data: Bytes) -> String
17    where
18        Bytes: AsRef<[u8]>,
19    {
20        hex::encode(data)
21    }
22}
23
24/// Encodes bytes data as a base64 `String`.
25#[derive(Debug, Copy, Clone)]
26pub struct Base64Encoder;
27
28impl Encoder for Base64Encoder {
29    fn encode<Bytes>(&self, data: Bytes) -> String
30    where
31        Bytes: AsRef<[u8]>,
32    {
33        base64::engine::general_purpose::STANDARD.encode(data)
34    }
35}