voided_core/formats/
mod.rs1use alloc::{string::String, vec::Vec};
4use base64::{engine::general_purpose::STANDARD, Engine};
5
6pub fn base64_encode(data: &[u8]) -> String {
8 STANDARD.encode(data)
9}
10
11pub fn base64_decode(encoded: &str) -> crate::Result<Vec<u8>> {
13 STANDARD
14 .decode(encoded)
15 .map_err(|e| crate::Error::InvalidBase64(e.to_string()))
16}
17
18pub fn hex_encode(data: &[u8]) -> String {
20 hex::encode(data)
21}
22
23pub fn hex_decode(encoded: &str) -> crate::Result<Vec<u8>> {
25 hex::decode(encoded).map_err(|e| crate::Error::InvalidHex(e.to_string()))
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31
32 #[test]
33 fn test_base64_roundtrip() {
34 let data = b"Hello, World!";
35 let encoded = base64_encode(data);
36 let decoded = base64_decode(&encoded).unwrap();
37 assert_eq!(data, &decoded[..]);
38 }
39
40 #[test]
41 fn test_hex_roundtrip() {
42 let data = b"Hello, World!";
43 let encoded = hex_encode(data);
44 let decoded = hex_decode(&encoded).unwrap();
45 assert_eq!(data, &decoded[..]);
46 }
47}