[][src]Macro kubos_service::run

macro_rules! run {
    ($func:expr) => { ... };
    ($func:expr, $master:expr) => { ... };
}

Execute a function and return Result<func_data, String> Optionally: Add the error string to the master error string for later consumption, prefixed with the name of the function being called

Examples

#[macro_use]
extern crate kubos_service;
use kubos_service::run;
use failure::{Error, Fail};
use std::sync::{Arc, RwLock};

#[derive(Fail, Debug)]
pub enum RootError {
    #[fail(display = "RootError: {}", message)]
    RootError { message: String },
}

#[derive(Fail, Debug)]
pub enum TopError {
    #[fail(display = "TopError: {}", message)]
    Error {
        #[fail(cause)]
        cause: RootError,
        message: String,
    },
}

fn test_func(fail: bool, output: String) -> Result<String, Error> {
    match fail {
        true => {
            let chain: TopError = TopError::Error {
                cause: RootError::RootError { message: "root".to_owned() },
                message: "top".to_owned(),
            };

            Err(chain.into())
        }
        false => Ok(output),
    }
}

fn main() {
    let master_err = Arc::new(RwLock::new(vec![]));
    let result = run!(test_func(true, "test".to_owned()), master_err);

    assert_eq!(result, Err("TopError: top, RootError: root".to_owned()));
    assert_eq!(
        vec!["test_func (src/macros.rs:40): TopError: top, RootError: root".to_owned()],
        *master_err.read().unwrap()
    );
}