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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/// Base64 encoding and decoding utilities for binary data serialization.
///
/// This module provides efficient base64 encoding/decoding operations,
/// including direct binary data conversion and JSON-embedded serialization.
/// Built on the standard base64 encoding with optimized performance.
///
/// ## Features
///
/// - **Binary Data Encoding**: Convert binary data to base64 strings for text protocols
/// - **JSON Integration**: Embed structured data in JSON using base64 encoding
/// - **Round-trip Safety**: Guaranteed lossless encoding/decoding
/// - **Standard Compliance**: RFC 4648 base64 encoding
/// - **Error Handling**: Comprehensive error reporting for invalid input
///
/// ## Examples
///
/// ### Basic Binary Encoding
/// ```rust
/// use trash_utilities::serde::{encode_base64, decode_base64};
///
/// let binary_data = b"Hello, World! This is binary data.";
/// let encoded = encode_base64(binary_data);
/// let decoded = decode_base64(&encoded).unwrap();
/// assert_eq!(binary_data.to_vec(), decoded);
/// ```
///
/// ### JSON with Embedded Binary Data
/// ```rust
/// use trash_utilities::serde::{serialize_to_base64_json, deserialize_from_base64_json};
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// struct Message {
/// text: String,
/// binary_attachment: Vec<u8>,
/// }
///
/// let message = Message {
/// text: "Check out this file!".to_string(),
/// binary_attachment: vec![0xFF, 0xD8, 0xFF, 0xE0], // JPEG header
/// };
///
/// // Serialize with binary data embedded as base64
/// let b64_json = serialize_to_base64_json(&message).unwrap();
/// let decoded: Message = deserialize_from_base64_json(&b64_json).unwrap();
/// assert_eq!(message, decoded);
/// ```
///
/// ### Protocol Buffers over HTTP
/// ```rust
/// use trash_utilities::serde::{encode_base64, decode_base64};
///
/// // Simulate sending protobuf data over HTTP
/// let protobuf_bytes = vec![0x08, 0x96, 0x01, 0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67];
/// let b64_payload = encode_base64(&protobuf_bytes);
///
/// // Send b64_payload in HTTP request...
/// // Later, decode back to protobuf
/// let recovered_bytes = decode_base64(&b64_payload).unwrap();
/// assert_eq!(protobuf_bytes, recovered_bytes);
/// ```
/// Encode data as base64 string.
///
/// # Parameters
/// - `data`: The binary data to encode.
///
/// # Returns
/// A base64-encoded string.
///
/// # Examples
/// ```rust
/// use trash_analyzer::serde::encode_base64;
///
/// let data = b"Hello, world!";
/// let encoded = encode_base64(data);
/// println!("Base64: {}", encoded);
/// ```
/// Decode a base64 string to binary data.
///
/// This function decodes a base64 string back to binary data.
/// Returns an error if the input is not valid base64.
///
/// # Parameters
/// - `data`: The base64 string to decode.
///
/// # Returns
/// - `Ok(Vec<u8>)` containing the decoded bytes.
/// - `Err(base64::DecodeError)` if decoding fails.
///
/// # Errors
/// Returns `base64::DecodeError` if the input string is not valid base64.
///
/// # Examples
/// ```rust
/// use trash_analyzer::serde::{encode_base64, decode_base64};
///
/// let data = b"Hello, world!";
/// let encoded = encode_base64(data);
/// let decoded = decode_base64(&encoded).unwrap();
/// assert_eq!(decoded, data);
/// ```
/// Serialize a value to JSON and encode it as base64.
///
/// This convenience function serializes a value to JSON bytes and then
/// encodes those bytes as base64. Useful for embedding structured data
/// in text formats like JSON or XML.
///
/// # Type Parameters
/// - `T`: The type to serialize, must implement `Serialize`.
///
/// # Parameters
/// - `value`: The value to serialize and encode.
///
/// # Returns
/// - `Ok(String)` containing the base64-encoded JSON.
/// - `Err(serde_json::Error)` if serialization fails.
///
/// # Errors
/// Returns `serde_json::Error` if the value cannot be serialized to JSON.
///
/// # Examples
/// ```rust
/// use trash_analyzer::serde::serialize_to_base64_json;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Person { name: String, age: u32 }
///
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// let b64_json = serialize_to_base64_json(&person).unwrap();
/// println!("Base64 JSON: {}", b64_json);
/// ```
/// Decode base64 JSON and deserialize to a struct.
///
/// This function decodes a base64 string to JSON bytes and then deserializes
/// those bytes into a struct. The inverse of `serialize_to_base64_json`.
///
/// # Type Parameters
/// - `T`: The type to deserialize into, must implement `DeserializeOwned`.
///
/// # Parameters
/// - `b64_json`: The base64-encoded JSON string.
///
/// # Returns
/// - `Ok(T)` containing the deserialized value.
/// - `Err(Box<dyn std::error::Error>)` if decoding or deserialization fails.
///
/// # Errors
/// Returns an error if the base64 string is invalid or if the decoded JSON cannot be deserialized into the target type.
///
/// # Examples
/// ```rust
/// use trash_analyzer::serde::{serialize_to_base64_json, deserialize_from_base64_json};
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// struct Person { name: String, age: u32 }
///
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// let b64_json = serialize_to_base64_json(&person).unwrap();
/// let decoded: Person = deserialize_from_base64_json(&b64_json).unwrap();
/// assert_eq!(decoded, person);
/// ```