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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2015 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.0.  This, along with the
// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

/// A replacement for calling `unwrap()` on a `Result`.
///
/// This macro is intended to be used in all cases where we `unwrap` a `Result` to deliberately
/// panic in case of error, e.g. in test-cases.  Such `unwrap`s don't give a precise point of
/// failure in our code and instead indicate some line number in the Rust core library.  This macro
/// provides a precise point of failure and decorates the failure for easy viewing.
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # extern crate maidsafe_utilities;
/// # fn main() {
/// let some_result: Result<String, std::io::Error> = Ok("Hello".to_string());
/// let string_length = unwrap_result!(some_result).len();
/// assert_eq!(string_length, 5);
/// # }
/// ```
#[macro_export]
macro_rules! unwrap_result {
    ($result:expr) => {
        $result.unwrap_or_else(|error| {
            let mut message =
                "Result evaluated to Err: ".to_owned();
            message.push_str(&format!("{:?}", error)[..]);
            let decorator_length = ::std::cmp::min(message.len() + 2, 100);
            let decorator = (0..decorator_length).map(|_| "=").collect::<String>();
            panic!("\n\n {}\n| {} |\n {}\n\n", decorator, message, decorator)
        })
    }
}

/// A replacement for calling `unwrap()` on an `Option`.
///
/// This macro is intended to be used in all cases where we `unwrap` an `Option` to deliberately
/// panic in case of error, e.g. in test-cases.  Such `unwrap`s don't give a precise point of
/// failure in our code and instead indicate some line number in the Rust core library.  This macro
/// provides a precise point of failure and decorates the failure for easy viewing.
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # extern crate maidsafe_utilities;
/// # fn main() {
/// let some_option = Some("Hello".to_string());
/// let string_length = unwrap_option!(some_option, "This is a user-supplied text.").len();
/// assert_eq!(string_length, 5);
/// # }
/// ```
#[macro_export]
macro_rules! unwrap_option {
    ($option:expr, $user_string:expr) => {
        $option.unwrap_or_else(|| {
            let mut error = "Option evaluated to None".to_owned();
            if !$user_string.is_empty() {
                error.push_str(": \"");
                error.push_str($user_string);
                error.push_str("\"");
            }
            let decorator_length = ::std::cmp::min(error.len() + 2, 100);
            let decorator = (0..decorator_length).map(|_| "=").collect::<String>();
            panic!("\n\n {}\n| {} |\n {}\n\n", decorator, error, decorator)
        })
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn unwrap_good_result() {
        let result: Result<u8, ()> = Ok(1);
        assert_eq!(unwrap_result!(result), 1);
    }

    #[test]
    fn unwrap_good_option() {
        let option = Some(1);
        assert_eq!(unwrap_option!(option, "Error text."), 1);
    }

    #[test]
    #[should_panic(expected = "Message on failure.")]
    fn unwrap_bad_result() {
        let result: Result<(), String> = Err("Message on failure.".to_owned());
        unwrap_result!(result);
    }

    #[test]
    #[should_panic(expected = "Message on failure.")]
    fn unwrap_bad_option() {
        let option: Option<()> = None;
        unwrap_option!(option, "Message on failure.");
    }
}