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
//! # Quick introduction to butchering on enums
//!
//! **Note**: this page does not cover how the butchering methods work, but
//! just shows some precise examples on how to use butcher on enums. In order
//! to learn how to work with butcher, it is advised to read the
//! [Quick introduction to butchering on structs][qibs]
//!
//! [qibs]: ../deriving_butcher_struct/index.html
//!
//! ## On a simple enum
//!
//! ```rust
//! use butcher::Butcher;
//!
//! #[derive(Clone, Butcher)]
//! enum Coin {
//! Euro {
//! value: usize,
//! },
//! Pound(f32),
//! Quoted(String),
//! None,
//! }
//! ```
//!
//! Want to add some butchering method? Just as simple as you expect:
//!
//! ```rust
//! use butcher::Butcher;
//!
//! #[derive(Clone, Butcher)]
//! enum Coin {
//! Euro {
//! #[butcher(copy)]
//! value: usize,
//! },
//! Pound(f32),
//! Quoted(
//! #[butcher(as_deref)]
//! String,
//! ),
//! None,
//! }
//! ```
//!
//! Generics? Say no more:
//!
//! ```rust
//! use butcher::Butcher;
//!
//! #[derive(Clone, Butcher)]
//! enum Foo<T> {
//! Bar(T),
//! }
//! ```
//!
//! Unbutchering can be used to create simple catch-all match arm:
//!
//! ```rust
//! # fn handle_special_event(_: LogEvent) {}
//! use butcher::Butcher;
//! use std::borrow::Cow;
//!
//! #[derive(Debug, Clone, Butcher)]
//! enum LogEvent {
//! Info,
//! Dbg(&'static str),
//! Warning(
//! #[butcher(as_deref)]
//! String,
//! ),
//! }
//!
//! let event: Cow<LogEvent> = Cow::Owned(LogEvent::Dbg("Heyo"));
//!
//! match LogEvent::butcher(event) {
//! ButcheredLogEvent::Info => {},
//! ButcheredLogEvent::Warning(w) => println!("Warning emitted: {}", w),
//! // A catch-all match arm
//! other => {
//! // We get back the original data
//! let e = LogEvent::unbutcher(other);
//!
//! handle_special_event(e);
//! }
//! }
//! ```