tiny_crypto/encoding/
mod.rs

1//! Text Encoders
2//! 
3
4/// The trait for encoders to encode to / decode from string.
5pub trait Encoder {
6    type Error: std::error::Error;
7
8    fn to_text(&self, input: &[u8]) -> String;
9    fn from_text(&self, input: &str) -> Result<Vec<u8>, Self::Error>;
10}
11
12mod base64;
13/// BASE64 is a constant Base64 instance for convenience.
14pub const BASE64: base64::Base64 = base64::Base64;
15
16mod hex;
17/// HEX is a constant Hex instance for convenience.
18pub const HEX: hex::Hex = hex::Hex;
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    use rstest::*;
24
25    #[rstest]
26    #[case(BASE64)]
27    #[case(HEX)]
28    fn to_and_from_text(#[case] encoder: impl Encoder) {
29        let plain = "some text".as_bytes();
30        assert_eq!(plain, &encoder.from_text(&encoder.to_text(plain)).unwrap());
31    }
32}