Skip to main content

rings_core/message/encoder/
mod.rs

1use std::ops::Deref;
2
3use bytes::Bytes;
4use serde::Deserialize;
5use serde::Serialize;
6
7use crate::error::Error;
8use crate::error::Result;
9
10/// Encodes values into the base58-check wire representation.
11pub trait Encoder {
12    /// Encode this value into an [`Encoded`] wrapper.
13    fn encode(&self) -> Result<Encoded>;
14}
15
16/// Decodes values from the base58-check wire representation.
17pub trait Decoder: Sized {
18    /// Decode `Self` from an [`Encoded`] wrapper.
19    fn from_encoded(encoded: &Encoded) -> Result<Self>;
20}
21
22/// Base58-check encoded message data.
23#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
24pub struct Encoded(String);
25
26impl Encoded {
27    /// Borrow the encoded string value.
28    pub fn value(&self) -> &String {
29        &self.0
30    }
31}
32
33impl Deref for Encoded {
34    type Target = String;
35    fn deref(&self) -> &Self::Target {
36        self.value()
37    }
38}
39
40impl Encoder for String {
41    fn encode(&self) -> Result<Encoded> {
42        Ok(Encoded(
43            base58_monero::encode_check(self.as_bytes()).map_err(|_| Error::Encode)?,
44        ))
45    }
46}
47
48impl Decoder for String {
49    fn from_encoded(encoded: &Encoded) -> Result<String> {
50        let d = Vec::from_encoded(encoded)?;
51        String::from_utf8(d).map_err(|_| Error::Decode)
52    }
53}
54
55impl Encoder for &[u8] {
56    fn encode(&self) -> Result<Encoded> {
57        Ok(Encoded(
58            base58_monero::encode_check(self).map_err(|_| Error::Encode)?,
59        ))
60    }
61}
62
63impl Encoder for Vec<u8> {
64    fn encode(&self) -> Result<Encoded> {
65        Ok(Encoded(
66            base58_monero::encode_check(self).map_err(|_| Error::Encode)?,
67        ))
68    }
69}
70
71impl Encoder for Bytes {
72    fn encode(&self) -> Result<Encoded> {
73        self.as_ref().encode()
74    }
75}
76
77impl Decoder for Vec<u8> {
78    fn from_encoded(encoded: &Encoded) -> Result<Self> {
79        crate::base58_check::decode(encoded.deref()).map_err(|_| Error::Decode)
80    }
81}
82
83impl Decoder for Bytes {
84    fn from_encoded(encoded: &Encoded) -> Result<Self> {
85        let d = Vec::from_encoded(encoded)?;
86        Ok(Bytes::from(d))
87    }
88}
89
90#[allow(clippy::to_string_trait_impl)]
91impl ToString for Encoded {
92    fn to_string(&self) -> String {
93        self.deref().to_owned()
94    }
95}
96
97impl From<String> for Encoded {
98    fn from(v: String) -> Self {
99        Self(v)
100    }
101}
102
103impl From<&str> for Encoded {
104    fn from(v: &str) -> Self {
105        Self(v.to_owned())
106    }
107}
108
109impl From<Encoded> for Vec<u8> {
110    fn from(a: Encoded) -> Self {
111        a.to_string().as_bytes().to_vec()
112    }
113}
114
115impl Encoded {
116    /// Create an [`Encoded`] value from a string that is already encoded.
117    pub fn from_encoded_str(str: &str) -> Self {
118        Self(str.to_owned())
119    }
120
121    /// Decode this value into a target type implementing [`Decoder`].
122    pub fn decode<T>(&self) -> Result<T>
123    where T: Decoder {
124        T::from_encoded(self)
125    }
126}
127
128#[cfg(test)]
129mod test_encoder;