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
/// String processing and I/O utilities.
///
/// This module provides JSON processing, parallel string operations,
/// and asynchronous file I/O for strings, leveraging serde for serialization,
/// parallel processing capabilities, and async file operations.
///
/// # Examples
///
/// JSON processing:
/// ```rust
/// use trash_utilities::chars::processing::*;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize)]
/// struct Person { name: String, age: u32 }
///
/// let json = r#"{"name":"Alice","age":30}"#;
/// let person: Person = parse_and_validate_json(json).unwrap();
/// assert_eq!(person.name, "Alice");
/// assert_eq!(person.age, 30);
/// ```
// Standard library imports
// External crate imports
use ;
use serde_json;
use fs;
/// Convenience: Parse JSON and validate
///
/// Parses a JSON string and deserializes it into the specified type.
/// This is a convenience wrapper around `serde_json` operations.
///
/// # Type Parameters
///
/// * `T` - The type to deserialize into (must implement `serde::Deserialize`).
///
/// # Parameters
///
/// * `s` - The JSON string to parse.
///
/// # Returns
///
/// The deserialized value on success.
///
/// # Errors
///
/// Returns a `serde_json::Error` if parsing fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::chars::processing::parse_and_validate_json;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Config { debug: bool, port: u16 }
///
/// let json = r#"{"debug":true,"port":8080}"#;
/// let config: Config = parse_and_validate_json(json).unwrap();
/// assert!(config.debug);
/// assert_eq!(config.port, 8080);
/// ```
/// Result type alias for processing operations
pub type ProcessingResult<T> = ;
/// Parallel string processing: split string and process chunks
///
/// Splits a string into chunks and processes each chunk in parallel using the provided function.
/// Useful for processing large strings efficiently across multiple threads.
///
/// # Type Parameters
///
/// * `F` - The processing function type.
/// * `R` - The result type returned by the processing function.
///
/// # Parameters
///
/// * `s` - The string to process.
/// * `chunk_size` - The size of each chunk in bytes.
/// * `processor` - Function to process each chunk.
///
/// # Returns
///
/// A vector of results from processing each chunk.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::chars::processing::parallel_process_string;
///
/// let text = "The quick brown fox jumps over the lazy dog";
/// let results = parallel_process_string(text, 10, |chunk| chunk.len());
/// // Each chunk's length is calculated
/// assert!(!results.is_empty());
/// ```
/// Asynchronously read a file to string
///
/// Reads the entire contents of a file into a string using async I/O.
///
/// # Parameters
///
/// * `path` - The path to the file to read.
///
/// # Returns
///
/// The file contents as a string on success.
///
/// # Errors
///
/// Returns an `std::io::Error` if the file cannot be opened or read.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::chars::processing::read_file_to_string_async;
/// use smol;
///
/// # smol::block_on(async {
/// // This would work with an actual file
/// // let content = read_file_to_string_async("example.txt").await.unwrap();
/// // assert!(!content.is_empty());
/// # });
/// ```
pub async
/// Asynchronously write string to file
///
/// Writes a string to a file using async I/O, creating the file if it doesn't exist
/// and truncating it if it does.
///
/// # Parameters
///
/// * `path` - The path to the file to write.
/// * `contents` - The string content to write.
///
/// # Errors
///
/// Returns an `std::io::Error` if the file cannot be created or written to.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::chars::processing::write_string_to_file_async;
/// use smol;
///
/// # smol::block_on(async {
/// // This would work with write permissions
/// // write_string_to_file_async("output.txt", "Hello, world!").await.unwrap();
/// # });
/// ```
pub async
/// Extract all JSON values by key from a JSON array string
///
/// Parses a JSON array and extracts all values associated with a specific key
/// from objects within the array.
///
/// # Parameters
///
/// * `json_array` - A JSON string representing an array of objects.
/// * `key` - The key to extract values for.
///
/// # Returns
///
/// A vector of extracted JSON values.
///
/// # Errors
///
/// Returns an error if the input is not valid JSON or if parsing fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::chars::processing::extract_json_values_by_key;
///
/// let json = r#"[
/// {"name": "Alice", "age": 30},
/// {"name": "Bob", "age": 25},
/// {"name": "Charlie", "age": 35}
/// ]"#;
///
/// let names = extract_json_values_by_key(json, "name").unwrap();
/// assert_eq!(names.len(), 3);
/// ```