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
//! # Enum Cycling
//!
//! Enum Cycling is a crate that allows one to
//! more easily navigate enums in Rust.
//!
//! The full version of the README can be found on Github
//!
//! # How to include Enum Cycling
//!
//! Import enum_cycling into your project by adding this line to your
//! Cargo.toml.
//!
//!  ```toml
//! [dependencies]
//!  enum_cycling = "0.1.0"
//!  enum_cycling_derive = "0.1.0"
//!
//!  # You can also just use the "derive" feature
//!  # enum_cycling = { version = "0.1.0", features = ["derive"]}
//! ```

/// This trait is the central piece to move up and down an `Enum`.
/// It may auto generated for you using the `#[derive(EnumCycle]`
/// feature.
///
/// # Example
///
/// ```rust
/// //Bring it into scope
/// use enum_cycling::IntoEnumCycle;
/// use enum_cycling_derive::EnumCycle;
///
/// #[derive(PartialEq, Debug, EnumCycle)]
/// enum MainMenu {
///     NewGame,
///     Continue,
///     Quit,
/// }
///
/// fn main() {
///     assert_eq!(MainMenu::NewGame.down(), MainMenu::Continue);
/// }
/// ```
///
/// The generated code should look something along the lines of:
///
/// ```nocompile
/// impl EnumCycle for MainMenu {
///     fn up(&self) -> Self {
///         match *self {
///             Self::NewGame => Self::Quit,
///             Self::Continue => Self::NewGame,
///             Self::Quit => Self::Continue,
///         }
///     }
///     fn down(&self) -> Self {
///         match *self {
///             Self::NewGame => Self::Continue,
///             Self::Continue => Self::Quit,
///             Self::Quit => Self::NewGame,
///         }
///     }
/// }
/// ```
pub trait IntoEnumCycle {
    fn up(&self) -> Self;
    fn down(&self) -> Self;
}

#[cfg(feature = "derive")]
pub use enum_cycling_derive::EnumCycle;