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
//! Tweak provides when/then clauses to your context in described state.
//!
//! # Installation
//!
//! Add the following to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! tweak = "*"
//! ```
//!
//! # Examples
//!
//! Simple context manipulation.
//!
//! ```rust
//! extern crate tweak;
//!
//! use tweak::Case;
//!
//! struct XY {
//!     x: i32,
//!     y: i32,
//! }
//!
//! impl XY {
//!     fn new(x: i32, y: i32) -> Self {
//!         Self { x, y }
//!     }
//! }
//!
//! let mut xy = XY::new(5, 0);
//! let res = Case::<XY, &str>::new("coords")
//!     .when("x > 0", |xy| Ok(xy.x > 0))
//!     .then_case("tweak x", |case| {
//!         case.when("x == 5", |xy| Ok(xy.x == 5))
//!             .then("multiply x by 3", |xy| {
//!                 xy.x *= 3;
//!                 Ok(())
//!             })
//!             .when("when x > 10", |xy| Ok(xy.x > 10))
//!             .then("set x to 10", |xy| {
//!                 xy.x = 10;
//!                 Ok(())
//!             })
//!     })
//!     .when("y > 0", |xy| Ok(xy.y > 0))
//!     .then_case("tweak y", |case| {
//!         case.when("y > 0", |xy| Ok(xy.y > 0))
//!             .then("divide 10 by y", |xy| {
//!                 xy.y = 10 / xy.y;
//!                 Ok(())
//!             })
//!     })
//!     .run(&mut xy);
//!
//! assert_eq!(Ok(true), res);
//! assert_eq!(xy.x, 10);
//! assert_eq!(xy.y, 0);
//! ```

use std::result;

mod case;
mod then;
mod when;
pub use case::Case;

type WhenFn<C, E> = fn(&mut C) -> Result<bool, E>;
type ThenFn<C, E> = fn(&mut C) -> Result<(), E>;
type Result<T, E> = result::Result<T, E>;

trait Execute<C, E> {
    fn exec(&self, ctx: &mut C) -> Result<bool, E>;
}