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
//! Contains the macros `try_or!`, `try_or_else!`, `try_opt_or!` and
//! `try_opt_or_else!`. These are helper macros for unwrapping a `Result` or an
//! `Option` while returning early for `Err` and `None` values. The semantics
//! are similar to `unwrap_or` and `unwrap_or_else`.
//!
//! If you want a `try_opt!` macro, there's already another crate (`try_opt`)
//! for that.

#![no_std]


/// Helper macro for unwrapping `Result` values. Returns early with the value
/// of the second parameter if the first parameter is `Err`.
#[macro_export]
macro_rules! try_or {
	( $expr:expr , $or:expr ) => {{
		match { $expr } {
			Ok(res) => res,
			Err(_) => { return { $or }; },
		}
	}}
}

/// Unwraps a `Result`. If the result is `Err`, calls the function `$or_fn` with
/// its value and returns early with its result.
#[macro_export]
macro_rules! try_or_else {
	( $expr:expr , $or_fn:expr ) => {{
		match { $expr } {
			Ok(res) => res,
			Err(err) => { return { $or_fn }(err); },
		}
	}}
}


/*/// Unwraps an `Option`, returning `None` if the option is `None`.
#[macro_export]
macro_rules! try_opt {
	( $expr:expr ) => {{
		match { $expr } {
			Some(val) => val,
			None => return None,
		}
	}}
}*/

/// Unwraps an `Option`, returning the second parameter if the option is `None`.
#[macro_export]
macro_rules! try_opt_or {
	( $expr:expr , $or:expr ) => {{
		match { $expr } {
			Some(res) => res,
			None => return { $or },
		}
	}}
}

/// Unwraps an `Option`. If the result is `None`, calls the function `$or_fn`
/// and returns its result.
#[macro_export]
macro_rules! try_opt_or_else {
	( $expr:expr , $or_fn:expr ) => {{
		match { $expr } {
			Some(res) => res,
			None => return { $or_fn }(),
		}
	}}
}


#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
		// test try_or!
		assert_eq!({ || try_or!("5".parse::<u32>(), 1) } (), 5);
		assert_eq!({ || try_or!("a".parse::<u32>(), 1) } (), 1);
		
		// test try_or_else!
		assert_eq!({ || try_or_else!("1".parse::<u32>(), |_| 5) } (), 1);
		assert_eq!({ || try_or_else!("a".parse::<u32>(), |_| 5) } (), 5);
		
		// test try_opt_or!
		assert_eq!({ || try_opt_or!(Some(1), 2) } (), 1);
		assert_eq!({ || try_opt_or!(None, 2) } (), 2);
		
		// test try_or_else!
		assert_eq!({ || try_opt_or_else!(Some(1), || 2) } (), 1);
		assert_eq!({ || try_opt_or_else!(None, || 2) } (), 2);
    }
}