pear/
result.rs

1use crate::error::ParseError;
2
3/// An alias to a Result where:
4///
5/// * `Ok` is `T`.
6/// * `Err` is a `ParseError` with context `C` and error `E`
7///
8/// For a `Result` that is parameterized only by the input type, see
9/// [`input::Result`](crate::input::Result).
10pub type Result<T, C, E> = std::result::Result<T, ParseError<C, E>>;
11
12#[doc(hidden)]
13pub trait IntoResult<T, C, E> {
14    fn into_result(self) -> Result<T, C, E>;
15}
16
17impl<T, C, E> IntoResult<T, C, E> for T {
18    #[inline(always)]
19    fn into_result(self) -> Result<T, C, E> {
20        Ok(self)
21    }
22}
23
24impl<T, C, E> IntoResult<T, C, E> for Result<T, C, E> {
25    #[inline(always)]
26    fn into_result(self) -> Result<T, C, E> {
27        self
28    }
29}
30
31// // This one will result in inference issues when `Ok(T)` is returned.
32// impl<T, I: Input, E: ::std::fmt::Display> IntoResult<T, I> for ::std::result::Result<T, E> {
33//     fn into_result(self) -> Result<T, I> {
34//         let name = unsafe { ::std::intrinsics::type_name::<E>() };
35//         self.map_err(|e| ParseError::new(name, e.to_string()))
36//     }
37// }
38
39// // This one won't but makes some things uglier to write.
40// impl<T, I: Input, E2, E1: Into<E2>> IntoResult<T, I, E2> for Result<T, I, E1> {
41//     fn into_result(self) -> Result<T, I, E2> {
42//         match self {
43//             Ok(v) => Ok(v),
44//             Err(e) => Err(ParseError {
45//                 error: e.error.into(),
46//                 contexts: e.contexts
47//             })
48//         }
49//     }
50// }
51
52// // This one won't but makes some things uglier to write.
53// impl<T, I: Input, E> IntoResult<T, I, B> for Result<T, I, A> {
54//     fn into_result(self) -> Result<T, I, B> {
55//         self
56//     }
57// }