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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//!
//! Debug helper methods for enum variants that are familiar from [`Option`] & [`Result`] such as [`Option::unwrap_or`] or [`Result::and_then`].
//! # Example
//! ```
//! #[derive(variantly::Variantly)]
//! enum Color {
//!     RGB(u8, u8, u8),
//!     HSV(u8, u8, u8),
//!     Grey(u8),
//!     FromOutOfSpace,
//!     #[variantly(rename = "darkness")]
//!     Black,
//! }
//!
//! fn example() {
//!     let color = Color::HSV(123, 45, 67);
//!
//!     // boolean helper method for determining variant:
//!     assert!(color.is_hsv());
//!     assert!(!color.is_rgb());
//!
//!     // Get inner values:
//!     let (h, s, v) = color.unwrap_hsv();
//!     assert_eq!((h, s, v), (123, 45, 67));
//!
//!     // Single values don't require tuple destructuring:
//!     let color = Color::Grey(128);
//!     let value = color.unwrap_grey();
//!     assert_eq!(value, 128);
//!
//!     // Alter inner value, only if hsv:
//!     let color = Color::HSV(111, 22, 33);
//!     let color = color.and_then_hsv(|(h, s, _)| (h, s, 100));
//!     assert_eq!(color.unwrap_hsv(), (111, 22, 100));
//!
//!     // Safely unwrap with a fallback:
//!     let color = Color::RGB(255, 255, 0);
//!     let (r, g, b) = color.unwrap_or_rgb((0, 0, 0));
//!     assert_eq!((r, g, b), (255, 255, 0));
//!     // Since color is of the HSV variant, the default is not used.
//!
//!     // Safely unwrap using the fallback
//!     let color = Color::FromOutOfSpace;
//!     let (r, g, b) = color.unwrap_or_rgb((0, 0, 0));
//!     assert_eq!((r, g, b), (0, 0, 0));
//!
//!     // Convert into an Option
//!     let color = Color::RGB(0, 255, 255);
//!     let optional_rgb = color.rgb();
//!     assert_eq!(Some((0, 255, 255)), optional_rgb);
//!
//!     // Convert into a Result
//!     let color = Color::RGB(255, 0, 255);
//!     let result_rgb = color.rgb_or("Error: This is not an RGB variant!");
//!     assert_eq!(Ok((255, 0, 255)), result_rgb);
//!
//!     // Operations like this can also use their familiar `_else` versions:
//!     let color = Color::FromOutOfSpace;
//!     let result_rgb = color.rgb_or_else(|| Some("This is a computationally expensive error!"));
//!     assert!(result_rgb.is_err());
//!
//!     // The `#[variantly(rename = "darkness")]` attribute renames derived methods:
//!     let color = Color::Black;
//!     assert!(color.is_darkness())
//! }
//! ```
//! # Derived Methods
//! In the naming of all methods described here, replace the `{variant_name}` with the snake_case formatted name of the given variant.
//!
//! ## Option & Result Conversion
//! Use the below methods to convert the enum into either an option or result:
//!
//! ### `pub fn {variant_name}(self) -> Option(...)`
//! If the enum is of the given variant, returns a [`Some`] containing the inner variant value. Otherwise, return [`None`].
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color = Color::HSV(1,2,3);
//!
//! let option = color.hsv();
//! assert_eq!(Some((1, 2, 3)), option);
//!
//! let color = Color::FromOutOfSpace;
//! assert_eq!(None, color.rgb());
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! ### `pub fn {variant_name}_or<E>(self, err: E) -> Result<(...), E>`
//! If the enum is of the given variant, returns a [`Result::Ok`] containing the inner value. Otherwise, return [`Result::Err`] containing `err`.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color = Color::HSV(1,2,3);
//!
//! let result = color.hsv_or("Error: Not an HSV!");
//! assert_eq!(Ok((1, 2, 3)), result);
//!
//! let color = Color::FromOutOfSpace;
//! let result = color.hsv_or("Error: Not an HSV!");
//! assert_eq!(Err("Error: Not an HSV!"), result);
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! ### `pub fn {variant_name}_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(...), E>`
//! If the enum is of the given variant, returns a [`Result::Ok`] containing the inner variant value. Otherwise, calls `f` to calculate a [`Result::Err`].
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color = Color::HSV(1,2,3);
//!
//! let result = color.hsv_or_else(|| "This is an expensive error to create.");
//! assert_eq!(Ok((1, 2, 3)), result);
//!
//! let color = Color::FromOutOfSpace;
//! let result = color.hsv_or_else(|| "This is an expensive error to create.");
//! assert_eq!(Err("This is an expensive error to create."), result);
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! ## Accessing Inner Values
//! Use the below methods to easily access the inner value of a given variant.
//!
//! ### `pub fn expect_{variant_name}(self, msg: &str) -> (...)`
//! Returns the contained value.
//!
//! #### Panics
//! Panics if the enum is not of the given variant with the custom message `msg`.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color_a = Color::HSV(1,2,3);
//! let color_b = Color::Grey(10);
//!
//! let (h, s, v) = color_a.expect_hsv("This should be an hsv");
//! assert_eq!((h, s, v), (1, 2, 3));
//!
//! let grey = color_b.expect_grey("This should be grey");
//! assert_eq!(grey, 10);
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! ### `pub fn unwrap_{variant_name}(self) -> (...)`
//! Returns the contained value.
//!
//! #### Panics
//! Panics if the enum is not of the given variant.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color_a = Color::HSV(1,2,3);
//! let color_b = Color::Grey(10);
//!
//! let (h, s, v) = color_a.unwrap_hsv();
//! assert_eq!((h, s, v), (1, 2, 3));
//!
//! let grey = color_b.unwrap_grey();
//! assert_eq!(grey, 10);
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! ### `pub fn unwrap_or_{variant_name}(self, fallback: (...)) -> (...)`
//! Returns the contained value if the enum is of the given variant, otherwise returns the provided `fallback`.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color_a = Color::HSV(1,2,3);
//! let color_b = Color::Grey(10);
//!
//! let (h, s, v) = color_a.unwrap_or_hsv((4, 5, 6));
//! assert_eq!((h, s, v), (1, 2, 3));
//!
//! let color = color_b.unwrap_or_rgb((4, 5, 6));
//! assert_eq!(color, (4, 5, 6));
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! ### `pub fn unwrap_or_else_{variant_name}<F: FnOnce() -> (...)>(self, f: F) -> (...)`
//! Returns the contained value if the enum is of the given variant, otherwise computes a fallback from `f`.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color_a = Color::HSV(1,2,3);
//! let color_b = Color::Grey(10);
//!
//! let (h, s, v) = color_a.unwrap_or_else_hsv(|| (4,5,6));
//! assert_eq!((h, s, v), (1, 2, 3));
//!
//! let (h, s, v) = color_b.unwrap_or_else_hsv(|| (4,5,6));
//! assert_eq!((h, s, v), (4, 5, 6));
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! ## Testing Variant Type
//! Use the below methods to test whether a variant is of the given type.
//!
//! ### `pub fn is_{variant_name}(self) -> bool`
//! Returns `true` if the enum is of the given variant.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color = Color::FromOutOfSpace;
//! assert!(color.is_from_out_of_space());
//! ```
//!
//! *Note: Available for all variant types*
//!
//! ### `pub fn is_not_{variant_name}(self) -> bool`
//! Returns `true` if the enum is *not* of the given variant.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color = Color::HSV(1,2,3);
//! assert!(color.is_not_rgb());
//! ```
//!
//! *Note: Available for all variant types*
//!
//! ## Compare & Process Specific Variant
//! Use the below to process and compare a specific enum variant.
//!
//! ### `pub fn and_{variant_name}(self, enum_b: GivenEnum) -> GivenEnum`
//! Returns `enum_b` if both self and `enum_b` are of the given variant. Otherwise returns `self`.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color_a = Color::HSV(1,2,3);
//! let color_b = Color::HSV(4,5,6);
//! let and = color_a.and_hsv(color_b);
//! assert_eq!(
//!     and,
//!     Color::HSV(4,5,6),
//! );
//! ```
//!
//! *Available for all variant types*
//!
//! ### `pub fn and_then_{variant_name}<F: FnOnce((...)) -> (...)>(self, f: F) -> Self`
//! Returns the enum as is if it is not of the given variant, otherwise calls `f` with the wrapped value and returns the result.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color_a = Color::HSV(1,2,3);
//!
//! let and = color_a.and_then_hsv(|(h, s, _)| (h, s, 4));
//! assert_eq!(
//!     and,
//!     Color::HSV(1, 2, 4),
//! );
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! ### `pub fn or_{variant_name}(self, enum_b: GivenEnum) -> GivenEnum`
//! Returns `self` if it is of the given variant, otherwise returns `enum_b`.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color_a = Color::HSV(1,2,3);
//! let color_b = Color::RGB(4,5,6);
//! let or = color_a.or_rgb(color_b);
//! assert_eq!(
//!     or,
//!     Color::RGB(4,5,6),
//! );
//! ```
//!
//! *Available for all variant types*
//!
//! ### `pub fn or_else_{variant_name}<F: FnOnce() -> (...)>(self, f: F) -> Self {`
//! Returns `self` if it is of the given variant, otherwise calls `f` and returns the result.
//!
//! #### Example
//! ```
//! # #[derive(variantly::Variantly, Debug, PartialEq)]
//! # enum Color {
//! #     RGB(u8, u8, u8),
//! #     HSV(u8, u8, u8),
//! #     Grey(u8),
//! #     FromOutOfSpace,
//! #     #[variantly(rename = "darkness")]
//! #     Black,
//! # }
//! let color = Color::HSV(1,2,3);
//! let color = color.or_else_rgb(|| (4,5,6));
//! assert_eq!(
//!     color,
//!     Color::RGB(4,5,6),
//! );
//! ```
//!
//! *Note: Available only for tuple-style variants such as Color::RGB(200, 40, 180), or Color::Grey(10)*
//!
//! # Renaming Methods
//! The `variantly` attribute may be placed on a variant in order to customize the resulting method names. The value set against `rename` inside the attribute will be used in place of the snake_cased variant name when constructing derived method names.
//! ```
//! #[derive(variantly::Variantly)]
//! enum SomeEnum {
//!     #[variantly(rename = "variant_a")]
//!     SomeVariantWithALongName(String),
//!     VariantB,
//! }
//!
//! let variant = SomeEnum::SomeVariantWithALongName(String::from("Hello"));
//! assert!(variant.is_variant_a());
//! ```
//! Methods associated with `SomeVariantWithALongName` will now be accessible only with the `variant_a`
//! suffix, such as `.unwrap_or_else_variant_a()`. This can help control overly verbose fn names.
//! Note that the input to `rename` is used as is and is not coerced into snake_case.
//!
//! The above is also relevant when two variant names would expand to create conflicting method names:
//! ```
//! #[derive(variantly::Variantly)]
//! enum SomeEnum {
//!     #[variantly(rename = "capital")]
//!     ABC,
//!     #[variantly(rename = "lower")]
//!     abc,
//! }
//! ```
//! Without the `rename` attribute in the above, both variants would create conflicting methods such as `.is_abc()` due to the coercion to snake_case.
//! This is avoided by using the rename input to create meaningful and unique fn names.
//!
//! #### License
//!
//! <sup>
//! Licensed under <a href="LICENSE">MIT license</a>.
//! </sup>
//!
//! <br>
//!
//! <sub>
//! Unless you explicitly state otherwise, any contribution intentionally submitted
//! for inclusion in this crate shall be licensed as above, without any additional terms or conditions.
//! </sub>

#[macro_use]
extern crate darling;
extern crate proc_macro;

#[macro_use]
mod idents;

mod derive;
mod error;
mod input;

use derive::derive_variantly_fns;
use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemEnum};

/// The `Variantly` derive macro. See [the module level documentation](self) for more information.
#[proc_macro_derive(Variantly, attributes(variantly))]
pub fn variantly(input: TokenStream) -> TokenStream {
    let item_enum = parse_macro_input!(input as ItemEnum);
    derive_variantly_fns(item_enum).unwrap_or_else(|err| err.to_compile_error())
}