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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/// Async data processing utilities for compression, serialization, and cryptography.
///
/// This module provides non-blocking operations for data transformation using brotli compression,
/// serde serialization, and cryptographic functions with ahash and base64.
///
/// # Examples
///
/// Basic compression and decompression:
/// ```rust
/// use trash_utilities::async::data::{compress_data_async, decompress_data_async};
/// use smol;
///
/// # smol::block_on(async {
/// let data = b"Hello, world!";
/// let compressed = compress_data_async(data, 6).await.unwrap();
/// let decompressed = decompress_data_async(&compressed).await.unwrap();
/// assert_eq!(decompressed, data);
/// # });
/// ```
// Standard library imports
use ;
// External crate imports
use ;
use ;
use ;
use serde_json;
use smol;
/// Compresses the given data using Brotli compression at the specified level.
///
/// This function performs compression asynchronously using the smol runtime,
/// allowing other tasks to run while compression is in progress.
///
/// # Parameters
///
/// * `data` - The data to compress as a byte slice.
/// * `level` - The compression level (0-11, where 0 is fastest and 11 is most compressed).
///
/// # Returns
///
/// Returns the compressed data as a `Vec<u8>` on success.
///
/// # Errors
///
/// Returns an `std::io::Error` if compression fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::data::compress_data_async;
/// use smol;
///
/// # smol::block_on(async {
/// let data = b"Hello, world!";
/// let compressed = compress_data_async(data, 6).await.unwrap();
/// assert!(!compressed.is_empty());
/// # });
/// ```
pub async
/// Decompresses the given Brotli-compressed data.
///
/// This function performs decompression asynchronously using the smol runtime.
///
/// # Parameters
///
/// * `data` - The compressed data as a byte slice.
///
/// # Returns
///
/// Returns the decompressed data as a `Vec<u8>` on success.
///
/// # Errors
///
/// Returns an `std::io::Error` if decompression fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::data::{compress_data_async, decompress_data_async};
/// use smol;
///
/// # smol::block_on(async {
/// let data = b"Hello, world!";
/// let compressed = compress_data_async(data, 6).await.unwrap();
/// let decompressed = decompress_data_async(&compressed).await.unwrap();
/// assert_eq!(decompressed, data);
/// # });
/// ```
pub async
/// Serializes the given value to a JSON string.
///
/// This function uses `serde_json` for serialization.
///
/// # Parameters
///
/// * `value` - The value to serialize, must implement `Serialize`.
///
/// # Type Parameters
///
/// * `T` - The type of the value to serialize.
///
/// # Returns
///
/// Returns the JSON string representation on success.
///
/// # Errors
///
/// Returns a `serde_json::Error` if serialization fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::data::serialize_async;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// let person = Person { name: "Alice".to_string(), age: 30 };
/// let json = serialize_async(&person).unwrap();
/// assert!(json.contains("Alice"));
/// ```
/// Deserializes a value from a JSON string.
///
/// This function uses `serde_json` for deserialization.
///
/// # Parameters
///
/// * `json` - The JSON string to deserialize.
///
/// # Type Parameters
///
/// * `T` - The type to deserialize into, must implement `Deserialize`.
///
/// # Returns
///
/// Returns the deserialized value on success.
///
/// # Errors
///
/// Returns a `serde_json::Error` if deserialization fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::data::deserialize_async;
/// use serde::Deserialize;
///
/// #[derive(Deserialize, Debug, PartialEq)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// let json = r#"{"name":"Alice","age":30}"#;
/// let person: Person = deserialize_async(json).unwrap();
/// assert_eq!(person.name, "Alice");
/// ```
/// Async cryptographic operations (non-blocking)
///
/// Computes a 64-bit hash of the given data using the ahash algorithm.
///
/// # Parameters
///
/// * `data` - The data to hash as a byte slice.
///
/// # Returns
///
/// Returns the 64-bit hash value.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::data::hash_data_async;
/// use smol;
///
/// # smol::block_on(async {
/// let data = b"Hello, world!";
/// let hash = hash_data_async(data).await;
/// assert_ne!(hash, 0);
/// # });
/// ```
pub async
/// Encodes the given data to a base64 string.
///
/// Uses the standard base64 encoding.
///
/// # Parameters
///
/// * `data` - The data to encode as a byte slice.
///
/// # Returns
///
/// Returns the base64-encoded string.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::data::encode_base64_async;
/// use smol;
///
/// # smol::block_on(async {
/// let data = b"Hello, world!";
/// let encoded = encode_base64_async(data).await;
/// assert!(!encoded.is_empty());
/// # });
/// ```
pub async
/// Decodes a base64-encoded string to bytes.
///
/// Uses the standard base64 decoding.
///
/// # Parameters
///
/// * `data` - The base64-encoded string to decode.
///
/// # Returns
///
/// Returns the decoded bytes as a `Vec<u8>` on success.
///
/// # Errors
///
/// Returns a `base64::DecodeError` if the input is not valid base64.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::data::{encode_base64_async, decode_base64_async};
/// use smol;
///
/// # smol::block_on(async {
/// let data = b"Hello, world!";
/// let encoded = encode_base64_async(data).await;
/// let decoded = decode_base64_async(&encoded).await.unwrap();
/// assert_eq!(decoded, data);
/// # });
/// ```
pub async