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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2015 Hugo Duncan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Enable conversion between non-local `Error` types.
//!
//! When using libraries, your code often needs to convert between
//! [`Error`](http://doc.rust-lang.org/std/error/trait.Error.html)
//! types which aren't part of your local crate. In this case you
//! can't implement
//! [`std::error::FromError`](http://doc.rust-lang.org/std/error/trait.FromError.html).
//!
//! This is also useful if you need context dependent translation of
//! error types.
//!
//!
//! # Examples
//!
//! # Simple
//!
//! ```rust
//! #[macro_use] extern crate from_error_scope;
//!
//! use from_error_scope::FromErrorScope;
//! use std::str::Utf8Error;
//! use std::io::CharsError;
//!
//! struct ErrorScope;
//!
//! impl FromErrorScope<Utf8Error, CharsError> for ErrorScope {
//!     fn from_error(&self, err: Utf8Error) -> CharsError {
//!        CharsError::NotUtf8
//!     }
//! }
//!
//! fn from_utf8(v: &[u8]) -> Result<&str, CharsError> {
//!     Ok(trys!(ErrorScope, std::str::from_utf8(v)))
//! }
//!
//! fn main () {
//!     let abc = vec![65,66,67];
//!     println!("{}", from_utf8(&abc).unwrap())
//! }
//!
//! ```
//!


/// A
/// [`FromError`](http://doc.rust-lang.org/std/error/trait.FromError.html)
/// like trait, that can be implemented on an explicit Scope type,
/// enabling conversion between non-local
/// [`Error`](http://doc.rust-lang.org/std/error/trait.Error.html)
/// types.
pub trait FromErrorScope<E,T> {
    fn from_error(&self, err: E) -> T;
}

/// A [`try!`](http://doc.rust-lang.org/std/macro.try!.html) like
/// macro that takes an additional type argument for a type that
/// implements [`FromErrorScope`](./trait.FromErrorScope.html).  You
/// may wish to wrap this macro for a specific error scope, to shorten
/// invocations.
#[macro_export]
macro_rules! trys {
    ($tr:expr, $expr:expr) => (match $expr {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(err) => {
            return ::std::result::Result::Err($tr.from_error(err))
        }
    })
}

#[cfg(test)]
mod tests {
    use super::FromErrorScope;

    #[derive(Debug,PartialEq)]
    pub enum E1{
        Error(String)
    }

    #[derive(Debug,PartialEq)]
    pub enum E2{
        Error(String)
    }

    #[derive(Debug,PartialEq)]
    pub enum E3{
        Error(String)
    }

    pub struct MyScope;

    impl FromErrorScope<E1,E2> for MyScope {
        fn from_error(&self, err: E1) ->  E2 {
            match err {
                E1::Error(s) => E2::Error(s)
            }
        }
    }

    impl FromErrorScope<E1,E3> for MyScope {
        fn from_error(&self, err: E1) ->  E3 {
            match err {
                E1::Error(s) => E3::Error(s)
            }
        }
    }


    fn one() -> Result<u32,E1> {
        Ok(1)
    }

    fn err1() -> Result<u32,E1> {
        Err(E1::Error("e1".to_string()))
    }

    fn foo() -> Result<u32, E2> {
        assert_eq!(trys!(MyScope, one()), 1);
        Ok(trys!(MyScope, err1()))
    }

    #[test]
    fn test_from_error_scope() {
        let res = foo();
        println!("res {:?}",res);
        assert_eq!(res, Err(E2::Error("e1".to_string())))
    }
}