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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use core::ops::ControlFlow;

use crate::{Flow, Status};

mod recoverable;
mod request_hint;
mod sequence;
mod tag;
mod value;

use effectful::{
    bound::HasSendAndSync,
    effective::{Canonical, Effective, UpdatePartial},
    environment::{Environment, InEnvironment},
    DynBind, SendSync,
};
pub use recoverable::*;
pub use request_hint::*;
pub use sequence::*;
pub use tag::*;
pub use value::*;

#[derive(Copy, Clone, SendSync)]
#[must_use]
pub enum VisitResult<S = ()> {
    /// The protocol was not used.
    ///
    /// This either means the visitor doesn't support the protocol at all, or
    /// it didn't want to use the protocol right now.
    Skipped(S),

    /// How control flow should proceed.
    Control(Flow),
}

impl<S> VisitResult<S> {
    pub fn unit_skipped(self) -> VisitResult<()> {
        match self {
            VisitResult::Skipped(_) => VisitResult::Skipped(()),
            VisitResult::Control(flow) => VisitResult::Control(flow),
        }
    }

    pub fn to_status(self) -> Status {
        match self {
            VisitResult::Skipped(_) => Status::Ok,
            VisitResult::Control(flow) => flow.to_status(),
        }
    }

    pub fn to_flow(self) -> Option<Flow> {
        match self {
            VisitResult::Skipped(_) => None,
            VisitResult::Control(flow) => Some(flow),
        }
    }

    pub fn dont_break_if_skipped(self) -> Option<Status> {
        match self {
            VisitResult::Skipped(_) => None,
            VisitResult::Control(Flow::Continue) => Some(Status::Ok),
            VisitResult::Control(Flow::Done) => Some(Status::Ok),
            VisitResult::Control(Flow::Err) => Some(Status::Err),
        }
    }

    pub fn break_if_done_or_err(self) -> Option<Status> {
        match self {
            VisitResult::Skipped(_) => None,
            VisitResult::Control(Flow::Continue) => None,
            VisitResult::Control(Flow::Done) => Some(Status::Ok),
            VisitResult::Control(Flow::Err) => Some(Status::Err),
        }
    }

    pub fn break_if_err(self) -> Option<Status> {
        match self {
            VisitResult::Skipped(_) => None,
            VisitResult::Control(Flow::Continue) => None,
            VisitResult::Control(Flow::Done) => None,
            VisitResult::Control(Flow::Err) => Some(Status::Err),
        }
    }

    pub fn map_skipped<R, F>(self, f: F) -> VisitResult<R>
    where
        F: FnOnce(S) -> R,
    {
        match self {
            VisitResult::Skipped(s) => VisitResult::Skipped(f(s)),
            VisitResult::Control(flow) => VisitResult::Control(flow),
        }
    }
}

pub type IfSkippedEffective<'ctx, 'lt, 'wrap, Cap, Update, Eff> = UpdatePartial<
    'wrap,
    'lt,
    (),
    (
        Cap,
        HasSendAndSync<
            for<'a> fn(
                Cap,
                &'a mut Update,
            )
                -> Canonical<'a, VisitResult<()>, <Eff as InEnvironment>::Env, &'ctx ()>,
        >,
    ),
    (),
    Update,
    (),
    VisitResult<()>,
    (Update, VisitResult<()>),
    Eff,
>;

type IfSkippedF<'ctx, Cap, Update, Eff> =
    for<'a> fn(
        Cap,
        &'a mut Update,
    ) -> Canonical<'a, VisitResult<()>, <Eff as InEnvironment>::Env, &'ctx ()>;

pub trait EffectiveVisitExt<'lt>: Effective<'lt> {
    fn if_skipped<'ctx, 'wrap, Cap, Update>(
        self,
        cap: Cap,
        f: IfSkippedF<'ctx, Cap, Update, Self>,
    ) -> IfSkippedEffective<'ctx, 'lt, 'wrap, Cap, Update, Self>
    where
        Cap: DynBind<Self::Env>,
        Update: DynBind<Self::Env>,
        Self: Effective<'lt, Output = (Update, VisitResult<()>)>,
        'ctx: 'lt,
        'lt: 'wrap,
    {
        self.update_partial(
            (),
            |_, (update, result)| match result {
                VisitResult::Skipped(()) => (update, ControlFlow::Continue(())),
                VisitResult::Control(_) => (update, ControlFlow::Break(result)),
            },
            (cap, HasSendAndSync(f)),
            |(cap, HasSendAndSync(f)), update, ()| f(cap, update).cast().into_raw(),
            (),
            |_, update, out| (update, out),
        )
    }

    fn if_not_finished<'ctx, 'wrap, Cap, Update>(
        self,
        cap: Cap,
        f: IfSkippedF<'ctx, Cap, Update, Self>,
    ) -> IfSkippedEffective<'ctx, 'lt, 'wrap, Cap, Update, Self>
    where
        Cap: DynBind<Self::Env>,
        Update: DynBind<Self::Env>,
        Self: Effective<'lt, Output = (Update, VisitResult<()>)>,
        'ctx: 'lt,
        'lt: 'wrap,
    {
        self.update_partial(
            (),
            |_, (update, result)| match result {
                VisitResult::Skipped(()) | VisitResult::Control(Flow::Continue) => {
                    (update, ControlFlow::Continue(()))
                }
                VisitResult::Control(_) => (update, ControlFlow::Break(result)),
            },
            (cap, HasSendAndSync(f)),
            |(cap, HasSendAndSync(f)), update, ()| f(cap, update).cast().into_raw(),
            (),
            |_, update, out| (update, out),
        )
    }
}

impl<'lt, T: Effective<'lt>> EffectiveVisitExt<'lt> for T {}

impl<S> PartialEq for VisitResult<S> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Skipped(_), Self::Skipped(_)) => true,
            (Self::Control(l0), Self::Control(r0)) => l0 == r0,
            _ => false,
        }
    }
}

impl<S> core::fmt::Debug for VisitResult<S> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Skipped(_) => f.debug_tuple("Skipped").finish(),
            Self::Control(arg0) => f.debug_tuple("Control").field(arg0).finish(),
        }
    }
}

impl<S> From<Flow> for VisitResult<S> {
    fn from(value: Flow) -> Self {
        Self::Control(value)
    }
}