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
//! # url_encor Crate
//!
//! This crate provides functionality for URL encoding and decoding.
//! It includes both standalone functions and a trait implementation for convenient use with `String` types.
//! The trait uses generics, therefore the return value can be manipulated!
//!
//! # Examples
//!
//! #### _Encoding_ a String is as easy as it gets
//! ```rust
//! use url_encor::Encoder;
//!
//! fn main() {
//! let string_to_encode = String::from("Hello, World!");
//! println!("{}", string_to_encode.url_encode());
//! //OUTPUT: Hello%2C%20World%21
//!
//! assert_eq!(string_to_encode.url_encode(), "Hello%2C%20World%21")
//! }
//! ```
//!
//!
//! #### _Decoding_ is easy, too
//! ```rust
//! use url_encor::Encoder;
//!
//! fn main() {
//! let string_to_decode = String::from("Hello%2C%20World%21");
//! println!("{}", string_to_decode.url_decode());
//! //OUTPUT: Hello, World!
//!
//! assert_eq!(string_to_decode.url_decode(), "Hello, World!")
//! }
//! ```
//!
//!
//! #### Implementing custom _encoding_ logic is easy as well
//! ```rust
//! use std::fmt::{Debug, Formatter};
//! use url_encor::{Encoder, encode};
//!
//! fn main() {
//! let custom_type_to_encode = CharVector(vec!['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' ]);
//! println!("{:?}", custom_type_to_encode.url_encode());
//! //OUTPUT: ['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1']
//!
//!
//! assert_eq!(custom_type_to_encode.url_encode().0, vec!['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1'])
//! }
//!
//! pub struct CharVector(Vec<char>);
//!
//! impl Debug for CharVector {
//! fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
//! self.0.fmt(f)
//! }
//! }
//!
//! impl Encoder<CharVector> for CharVector {
//! fn url_encode(&self) -> CharVector {
//! CharVector(encode(&self.0.iter().collect::<String>()).chars().collect())
//! }
//!
//! fn url_decode(&self) -> CharVector {
//! todo!()
//! }
//! }
//! ```
//!
//!
//! #### Implementation of custom _decoding_ logic
//! ```rust
//! use std::fmt::{Debug, Formatter};
//! use url_encor::{Encoder, decode};
//!
//! fn main() {
//! let custom_type_to_decode = CharVector(vec!['H', 'e', 'l', 'l', 'o', '%', '2', 'C', '%', '2', '0', 'W', 'o', 'r', 'l', 'd', '%', '2', '1']);
//! println!("{:?}", custom_type_to_decode.url_decode());
//! //OUTPUT: ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
//!
//!
//! assert_eq!(custom_type_to_decode.url_decode().0, vec!['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' ])
//! }
//!
//! pub struct CharVector(Vec<char>);
//!
//! impl Debug for CharVector {
//! fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
//! self.0.fmt(f)
//! }
//! }
//!
//! impl Encoder<CharVector> for CharVector {
//! fn url_encode(&self) -> CharVector {
//! todo!()
//! }
//!
//! fn url_decode(&self) -> CharVector {
//! CharVector(decode(&self.0.iter().collect::<String>()).chars().collect())
//! }
//! }
//! ```
//! # Issues?!
//!
//! If you encounter any problem, bug or issue, please open a new [issue](https://github.com/Dari-OS/url_encor/issues/new)
use ;
/// Encodes a string using url_encor.
///
/// This function iterates through each byte of the input string and encodes it if necessary.
/// Characters that don't need encoding are left as-is.
///
/// # Arguments
///
/// * `str_to_encode` - A string slice that holds the text to be URL encoded.
///
/// # Returns
///
/// A new `String` containing the URL encoded text.
///
/// # Examples
///
/// ```
/// use url_encor::encode;
///
/// let encoded = encode("Hello, World!");
/// assert_eq!(encoded, "Hello%2C%20World%21");
/// ```
/// Decodes a URL-encoded string.
///
/// This function iterates through the input string, decoding percent-encoded characters
/// and handling the special case of '+' being decoded as a space.
///
/// # Arguments
///
/// * `str_to_decode` - A string slice that holds the text to be URL decoded.
///
/// # Returns
///
/// A new `String` containing the decoded text.
///
/// # Examples
///
/// ```
/// use url_encor::decode;
///
/// let decoded = decode("Hello%2C%20World%21");
/// assert_eq!(decoded, "Hello, World!");
/// ```
/// Converts a hexadecimal character (represented as byte) to its corresponding decimal value (represented as byte as well).
///
/// # Arguments
///
/// * `c` - A byte representing a hexadecimal character.
///
/// # Returns
///
/// An `Option<u8>` containing the numeric value if valid, or `None` if invalid.
/// A trait for types that can be URL encoded and decoded.
/// Implementation of the `Encoder` trait for `String`.