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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! [![github]](https://github.com/DrakeN-inc/crate-add-macro) [![crates-io]](https://crates.io/crates/add_macro) [![docs-rs]](https://docs.rs/add_macro)
//!
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
//!
//! This crate provides the more additional macros to help you write code faster!
//!
//! # Examples:
//! ```
//! use add_macro::{ re, Display, From, FromStr };
//!
//! pub type Result<T> = std::result::Result<T, Error>;
//!
//! #[derive(Debug, Display, From)]
//! enum Error {
//! #[from]
//! Io(std::io::Error),
//!
//! #[display = "Incorrect E-mail address format"]
//! IncorrectEmail,
//! }
//!
//! #[derive(Debug, Display, FromStr)]
//! #[display = "{value}"]
//! struct Email {
//! value: String,
//! }
//!
//! impl Email {
//! fn parse(s: &str) -> Result<Self> {
//! let re = re!(r"^[\w\-]+@[\w\-]+\.\w+$");
//!
//! if re.is_match(s) {
//! Ok(Self {
//! value: s.into(),
//! })
//! } else {
//! Err(Error::IncorrectEmail)
//! }
//! }
//! }
//!
//! #[derive(Debug, Display)]
//! #[display = "Name: {name}, Age: {age}, E-mail: {email}"]
//! struct Person {
//! name: String,
//! age: u8,
//! email: Email,
//! }
//!
//! impl Person {
//! pub fn new<S>(name: S, age: u8, email: Email) -> Self
//! where S: Into<String> {
//! Self {
//! name: name.into(),
//! age,
//! email,
//! }
//! }
//! }
//!
//! fn main() -> Result<()> {
//! let bob = Person::new("Bob", 22, "bob@example.loc".parse()?);
//!
//! println!("{bob}");
//!
//! Ok(())
//! }
//! ```
/// This macros provides the implementation of trait [Display](std::fmt::Display)
///
/// # Examples:
/// ```
/// use add_macro_impl_display::Display;
///
/// #[derive(Display)]
/// enum Animals {
/// Turtle,
///
/// #[display]
/// Bird,
///
/// #[display = "The Cat"]
/// Cat,
///
/// #[display("The Dog")]
/// Dog,
///
/// Other(&'static str),
///
/// #[display]
/// Other2(&'static str),
///
/// #[display("{0} {1}, {2} years old")]
/// Info(Box<Self>, &'static str, i32),
///
/// #[display("{kind} {name}, {age} years old")]
/// Info2 {
/// kind: Box<Self>,
/// name: &'static str,
/// age: i32,
/// },
/// }
///
/// fn main() {
/// assert_eq!(format!("{}", Animals::Turtle), "Turtle");
/// assert_eq!(format!("{}", Animals::Bird), "Bird");
/// assert_eq!(format!("{}", Animals::Cat), "The Cat");
/// assert_eq!(format!("{}", Animals::Dog), "The Dog");
/// assert_eq!(format!("{}", Animals::Other("Tiger")), "Tiger");
/// assert_eq!(format!("{}", Animals::Other2("Tiger")), "Tiger");
/// assert_eq!(format!("{}", Animals::Info(Box::new(Animals::Cat), "Tomas", 2)), "The Cat Tomas, 2 years old");
/// assert_eq!(format!("{}",
/// Animals::Info2 {
/// kind: Box::new(Animals::Cat),
/// name: "Tomas",
/// age: 2,
/// }),
/// "The Cat Tomas, 2 years old"
/// );
/// }
/// ```
/// ```
/// use add_macro_impl_display::Display;
///
/// #[derive(Debug, Display)]
/// struct Person {
/// pub name: String,
/// pub age: u8,
/// }
///
/// #[derive(Debug, Display)]
/// #[display("Hello, {name}! Your age is {age} years old.")]
/// struct Person2 {
/// pub name: String,
/// pub age: u8,
/// }
///
/// fn main() {
/// assert_eq!(
/// format!("{}", Person {
/// name: "Bob".to_owned(),
/// age: 22
/// }),
/// "Person { name: \"Bob\", age: 22 }"
/// );
///
/// assert_eq!(
/// format!("{}", Person2 {
/// name: "Bob".to_owned(),
/// age: 22
/// }),
/// "Hello, Bob! Your age is 22 years old."
/// );
/// }
/// ```
pub use Display;
/// This macros provides the implementation of trait [Error](std::error::Error)
///
/// # Examples:
/// ```
/// use add_macro_impl_error::Error;
///
/// #[derive(Debug, Error)]
/// enum CustomError {
/// Io(std::io::Error),
/// Wrong,
/// }
///
/// impl std::fmt::Display for CustomError {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// match &self {
/// Self::Io(e) => write!(f, "{e}"),
/// Self::Wrong => write!(f, "Something went wrong.. =/"),
/// }
/// }
/// }
///
/// #[derive(Debug, Error)]
/// pub struct Error {
/// source: CustomError,
/// }
///
/// impl std::fmt::Display for Error {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "{}", &self.source)
/// }
/// }
///
/// fn main() {
/// let err = CustomError::Wrong;
/// assert_eq!(format!("{err}"), "Something went wrong.. =/");
///
/// let err = Error { source: CustomError::Wrong };
/// assert_eq!(format!("{err}"), "Something went wrong.. =/");
/// }
/// ```
pub use Error;
/// This macros provides the implementation of the trait [FromStr](std::str::FromStr)
///
/// # Examples:
/// ```
/// use add_macro_impl_fromstr::FromStr;
///
/// #[derive(Debug)]
/// enum Error {
/// ParseError,
/// }
///
/// #[derive(FromStr)]
/// struct Email {
/// name: String,
/// host: String
/// }
///
/// impl Email {
/// // WARNING: this method needs for working the implementation trait FromStr
/// pub fn parse(s: &str) -> Result<Self, Error> {
/// let spl = s.trim().splitn(2, "@").collect::<Vec<_>>();
///
/// if spl.len() == 2 {
/// Ok(Self {
/// name: spl[0].to_owned(),
/// host: spl[1].to_owned(),
/// })
/// } else {
/// Err(Error::ParseError)
/// }
/// }
/// }
///
/// fn main() -> Result<(), Error> {
/// let _bob_email: Email = "bob@example.loc".parse()?;
///
/// Ok(())
/// }
/// ```
pub use FromStr;
/// This macros provides the implementation of trait [From<T>](std::convert::From)
///
/// # Examples:
/// ```
/// use add_macro_impl_from::From;
///
/// #[derive(Debug)]
/// enum SimpleError {
/// Wrong,
/// }
///
/// impl std::fmt::Display for SimpleError {
/// fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
/// match &self {
/// Self::Wrong => write!(f, "Something went wrong.. =/"),
/// }
/// }
/// }
///
/// #[derive(Debug)]
/// struct SuperError {
/// source: String,
/// }
///
/// impl std::fmt::Display for SuperError {
/// fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
/// write!(f, "{}", &self.source)
/// }
/// }
///
/// #[derive(Debug, From)]
/// #[from("std::io::Error" = "Self::Io(v)")] // result: impl From<std::io::Error> for Error { fn from(v: std::io::Error) -> Self { Self::Io(v) } }
/// enum Error {
/// Io(std::io::Error),
///
/// #[from]
/// Simple(SimpleError),
///
/// #[from = "SuperError { source: format!(\"Super error: {}\", v.source) }"]
/// Super(SuperError),
///
/// #[from("String")]
/// #[from("&str" = "v.to_owned()")]
/// #[from("i32" = "format!(\"Error code: {v}\")")]
/// Stringify(String),
/// }
///
/// impl std::fmt::Display for Error {
/// fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
/// match &self {
/// Self::Io(e) => write!(f, "{e}"),
/// Self::Simple(e) => write!(f, "{e}"),
/// Self::Super(e) => write!(f, "{e}"),
/// Self::Stringify(e) => write!(f, "{e}"),
/// }
/// }
/// }
///
/// fn main() {
/// let io_err = Error::from( std::fs::read("fake/path/to/file").unwrap_err() );
/// assert_eq!(format!("{io_err}"), "No such file or directory (os error 2)");
///
/// let simple_err = Error::from( SimpleError::Wrong );
/// assert_eq!(format!("{simple_err}"), "Something went wrong.. =/");
///
/// let super_err = Error::from( SuperError { source: "Bad request".to_owned() } );
/// assert_eq!(format!("{super_err}"), "Super error: Bad request");
///
/// let str_err = Error::from( String::from("Something went wrong.. =/") );
/// assert_eq!(format!("{str_err}"), "Something went wrong.. =/");
///
/// let str_err2 = Error::from("Something went wrong.. =/");
/// assert_eq!(format!("{str_err2}"), "Something went wrong.. =/");
///
/// let str_err3 = Error::from(404);
/// assert_eq!(format!("{str_err3}"), "Error code: 404");
/// }
/// ```
pub use From;
/// This macros provides the implementation of trait [Into<T>](std::convert::Into)
///
/// # Examples:
/// ```
/// use add_macro_impl_into::Into;
///
/// #[derive(Debug, PartialEq)]
/// struct User {
/// name: String,
/// subname: Option<String>,
/// age: u8
/// }
///
/// #[derive(Debug, Clone, Into)]
/// #[into("User" = "User { name: self.name, subname: None, age: self.age }")]
/// #[into("String" = "format!(\"name: {}, age: {}\", self.name, self.age)")]
/// struct Person {
/// name: String,
/// age: u8
/// }
///
/// fn main() {
/// let bob = Person {
/// name: "Bob".to_owned(),
/// age: 22
/// };
///
/// let bob_user: User = bob.clone().into();
/// assert_eq!(
/// bob_user,
/// User {
/// name: "Bob".to_owned(),
/// subname: None,
/// age: 22
/// }
/// );
///
/// let bob_str: String = bob.into();
/// assert_eq!(bob_str, "name: Bob, age: 22");
/// }
/// ```
/// ```
/// use add_macro_impl_into::Into;
///
/// #[derive(Debug, PartialEq)]
/// struct Cat;
///
/// #[derive(Debug, PartialEq)]
/// struct Dog;
///
/// #[derive(Debug, PartialEq)]
/// struct Bird;
///
/// #[derive(Debug, PartialEq)]
/// struct Python;
///
/// #[derive(Debug, PartialEq, Into)]
/// #[into("String" = "format!(\"Animal::{self:?}\")")]
/// #[into("Option<Cat>" = "if let Self::Cat(v) = self { Some(v) }else{ None }")]
/// enum Animal {
/// Cat(Cat),
///
/// #[into]
/// Dog(Dog), // Option<Dog>
///
/// #[into = "if let Self::Bird(value) = self { value }else{ panic!(\"It's not a dog.\") }"]
/// // #[into("if let Self::Bird(value) = self { value }else{ panic!(\"It's not a dog.\") }")]
/// Bird(Bird),
///
/// #[into("Option<Python>" = "if let Self::Python(v) = self { Some(v) }else{ None }")]
/// #[into("Python" = "Into::<Option<Python>>::into(self).expect(\"It's not a Python\")")]
/// Python(Python),
/// }
///
/// fn main() {
/// let cat_str: String = Animal::Cat(Cat {}).into();
/// assert_eq!(cat_str, "Animal::Cat(Cat)");
///
/// let cat: Option<Cat> = Animal::Cat(Cat {}).into();
/// assert_eq!(cat, Some(Cat {}));
///
/// let dog: Option<Dog> = Animal::Dog(Dog {}).into();
/// assert_eq!(dog, Some(Dog {}));
///
/// let bird: Bird = Animal::Bird(Bird {}).into();
/// assert_eq!(bird, Bird {});
///
/// let python: Option<Python> = Animal::Python(Python {}).into();
/// assert_eq!(python, Some(Python {}));
///
/// let python: Python = Animal::Python(Python {}).into();
/// assert_eq!(python, Python {});
/// }
/// ```
pub use Into;