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
//! Module containing [`crate::railway_exec::RailwayBinder`] struct-ce
//! and [`crate::railway_exec::RailwayExec`] trait.
/// The struct for [`crate::yer`] macro to run railway execution over Result and Option types.
///
/// Raylway execution helps to write less amount of code not when you are worried about all returned
/// Option's Nones and Result's Errs, but when you want to try to execute a bunch of
/// instructions and see if it was executed without problems.
///
/// Example:
/// ```rust
/// use yerevan::{yer, railway_exec::*};
///
/// let railway_positive_scenario = yer! {
/// RailwayBinder =>
/// let! some_one = Result::<i32, ()>::Ok(1); // stores 1 from Result
/// let! some_two = Result::<i32, String>::Ok(2); // stores 2 from Result
/// let! some_one_and_two = Some(some_one + some_two); // wraps 1 + 2 = 3 to Option and unwraps then
/// ret some_one_and_two * 2 // 3 * 2 = 6 -> Option wrap
/// }; // returns Some(6)
///
/// let railway_negative_scenario = yer! {
/// RailwayBinder =>
/// let! something = Some("something");
/// do! Option::<()>::None; // stop execution here
/// ret something
/// }; // Does not execute "ret" instruction and returns None
/// ```
/// Use this trait to implement function `bind` over your struct you want to use in
/// [`crate::railway_exec::RailwayBinder`] CE.
///
/// Example of implementation for Result:
/// ```rust,ignore
/// impl<T, E> RailwayExec<T> for Result<T, E> {
/// fn bind<U>(self, f: &dyn Fn(T) -> Option<U>) -> Option<U> {
/// match self {
/// Ok(val) => f(val),
/// Err(_) => None,
/// }
/// }
/// }
/// ```
///
/// Example of implementation for Option:
/// ```rust,ignore
/// impl<T> RailwayExec<T> for Option<T> {
/// fn bind<U>(self, f: &dyn Fn(T) -> Option<U>) -> Option<U> {
/// match self {
/// Some(val) => f(val),
/// None => None,
/// }
/// }
/// }
/// ```
/// Implementation for simple railway-executions.