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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2022 Google LLC
//
// 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
//
// https://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.

mod result_or_value;
mod ok;
mod err;
mod check_no_error;
mod and_then2;
mod and_then;
mod unwrap_type_result;

pub use result_or_value::*;
pub use ok::*;
pub use err::*;
pub use check_no_error::*;
pub use and_then2::*;
pub use and_then::*;
pub use unwrap_type_result::*;

use std::marker::PhantomData;
use internal::*;
use crate::bool::*;
use crate::expr_wrapper::*;

pub struct Result<K: Kind> {
    k: PhantomData<K>,
}

impl<K: Kind> Kind for Result<K> {}

impl<K: KindWithDefault> KindWithDefault for Result<K> {
    type Default = Ok<K, K::Default>;
}

pub trait ResultVisitor<K: Kind, ResultK: Kind> {
    type VisitOk<V: Expr<K>>: Expr<ResultK>;
    type VisitErr<E>: Expr<ResultK>;
}

meta!{
    pub type VisitResult<
        K: Kind,
        OutK: Kind,
        R: Expr<Result<K>>,
        V: ResultVisitor<K, OutK>
    >: Expr<OutK> =
        <AsResult<K, R> as ResultTrait<K>>::Visit<OutK, V>;
}

impl<K: EqualityComparableKind> EqualityComparableKind for Result<K> {
    type Eq<X: Expr<Result<K>>, Y: Expr<Result<K>>> = VisitResult<K, Bool, X, ResultEqualsVisitor<K, Y>>;
}

impl<K: KindWithDebugForm + KindWithDefault> KindWithDebugForm for Result<K> {
    type DebugForm<R: Expr<Result<K>>> = VisitResult<K, ExprWrapper<Result<K>>, R, ResultDebugFormVisitor<K>>;
}

// These have to be public because otherwise Rust would complain that "can't leak private type".
// But they should never be explicitly referenced elsewhere.
mod internal {
    use std::marker::PhantomData;
    pub use crate::*;
    pub use super::*;
    use crate::r#type::*;

    meta!{
        pub struct ResultDebugFormVisitor<
            K: KindWithDebugForm + KindWithDefault
        >: ResultVisitor<K, ExprWrapper<Result<K>>> {
            type VisitErr<E> = WrapExpr<Result<K>, Err<K, E>>;
            type VisitOk<V: Expr<K>> = WrapExpr<
                Result<K>,
                Ok<K,
                    UnwrapExpr<K, K::DebugForm<V>>
                >
            >;
        }
    }

    pub trait ResultValue<K: Kind> {
        type Impl: ResultTrait<K>;
    }

    meta!{
        pub struct WrapResultValue<
            K: Kind, 
            R: ResultValue<K>
        >: Value<Result<K>> {
            type UnconstrainedImpl = <R as ResultValue<K>>::Impl;
        }
    }
    
    pub trait ResultTrait<K: Kind> {
        type Visit<ResultK: Kind, V: ResultVisitor<K, ResultK>>: Expr<ResultK>;
    }

    // Error returned by AsResult when it's called on a Value<Result<K>> that doesn't implement ResultTrait<K>.
    pub struct AsResultError {}

    pub struct AsResult<K: Kind, V: Expr<Result<K>>> {
        k: PhantomData<K>,
        v: PhantomData<V>,
    }

    impl<K: Kind, V: Expr<Result<K>>> ResultTrait<K> for AsResult<K, V> {
        default type Visit<ResultK: Kind, Visitor: ResultVisitor<K, ResultK>> = Visitor::VisitErr<AsResultError>;
    }

    impl<K: Kind, V: Expr<Result<K>>> ResultTrait<K> for AsResult<K, V> where <<V as Expr<Result<K>>>::Eval as Value<Result<K>>>::UnconstrainedImpl: ResultTrait<K> {
        type Visit<ResultK: Kind, Visitor: ResultVisitor<K, ResultK>> = <<<V as Expr<Result<K>>>::Eval as Value<Result<K>>>::UnconstrainedImpl as ResultTrait<K>>::Visit<ResultK, Visitor>;
    }

    meta!{
        pub struct ResultEqualsVisitor<
            K: EqualityComparableKind,
            Other: Expr<Result<K>>
        >: ResultVisitor<K, Bool> {
            type VisitOk<V: Expr<K>> = VisitResult<K, Bool, Other, ResultEqualsValueVisitor<K, V>>;
            type VisitErr<E> = VisitResult<K, Bool, Other, ResultEqualsErrorVisitor<K, E>>;
        }

        pub struct ResultEqualsValueVisitor<
            K: EqualityComparableKind,
            OtherV: Expr<K>
        >: ResultVisitor<K, Bool> {
            type VisitOk<V: Expr<K>> = Equals<K, V, OtherV>;
            type VisitErr<E> = False;
        }

        pub struct ResultEqualsErrorVisitor<
            K: EqualityComparableKind,
            OtherE
        >: ResultVisitor<K, Bool> {
            type VisitOk<V: Expr<K>> = False;
            type VisitErr<E> = Equals<Type, WrapType<E>, WrapType<OtherE>>;
        }
    }
}

#[cfg(test)]
#[allow(dead_code)]
mod tests {
    use crate::*;
    use crate::r#type::*;
    use crate::result::*;

    struct MyError {}

    #[test]
    fn equals() {
        meta_assert_eq!(Result<Type>, Err<Type, MyError>, Err<Type, MyError>);
        meta_assert_eq!(Result<Type>, Ok<Type, WrapType<i32>>, Ok<Type, WrapType<i32>>);
        meta_assert_not_eq!(Result<Type>, Err<Type, MyError>, Ok<Type, WrapType<i32>>);
        meta_assert_not_eq!(Result<Type>, Ok<Type, WrapType<i32>>, Ok<Type, WrapType<i64>>);
    }

    #[test]
    fn default() {
        meta_assert_eq!(Result<Type>, Default<Result<Type>>, Ok<Type, WrapType<()>>);
    }

    #[test]
    fn debug_form() {
        meta_assert_eq!(ExprWrapper<Result<Bool>>, DebugForm<Result<Bool>, Err<Bool, MyError>>, WrapExpr<Result<Bool>, Err<Bool, MyError>>);
        meta_assert_eq!(ExprWrapper<Result<Bool>>, DebugForm<Result<Bool>, Ok<Bool, And<True, False>>>, WrapExpr<Result<Bool>, Ok<Bool, False>>);
    }
}