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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use std::{cell::RefCell, rc::Rc};

use cosmwasm_std::{
    Addr, Api, Binary, BlockInfo, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcMsg,
    IbcPacketReceiveMsg, Storage,
};
use cw_multi_test::AppResponse;

use crate::{
    error::AppResult,
    ibc::IbcChannelWrapper,
    ibc_application::{IbcApplication, IbcPortInterface, PacketReceiveFailing, PacketReceiveOk},
    ibc_module::{AckPacket, TimeoutPacket},
    iper_app::InfallibleResult,
    response::AppResponseExt,
    router::RouterWrapper,
    stargate::{StargateApplication, StargateName, StargateUrls},
};

pub trait IbcAndStargate: IbcApplication + StargateApplication {}

/// Enum rappresenting how the flow has to be controlled when triggering the `before` variant of [`Middleware`] functions.
/// - **Stop**: The inner application will not called and the Value `S` will be returned;
/// - **Continue**: The inner application will called. After the execution of the inner call, the `after` variant of the [`Middleware`] function will be called.

pub enum MiddlewareResponse<S, C> {
    /// The inner application will not called and the Value `S` will be returned.
    Stop(S),
    /// The inner application will called. After the execution of the inner call, the `after` variant of the [`Middleware`] function will be called.
    Continue(C),
}

/// This trait allow to reproduce the functionality of Middleware ibc application (like IbcHook).
/// [`Middleware`] allow to wrap another [`IbcApplication`] and and alter/implement functionality when one of the various functions that the [`IbcApplication`] implements is called.
///
/// The core logic about []
///
/// [`Middleware`] trait alredy implements [`IbcApplication`] and [`StargateApplication`] by default, so implementing [`Middleware`] implement also [`IbcApplication`] and [`StargateApplication`].
///
/// ## How it works
///
/// The core logic of the [`Middleware`] involves the implementation of `functions` that wrap the individual functions
/// of the [`IbcApplication`] into two distinct `functions`, called `before` and `after`. For example, considering
/// the [`IbcApplication::packet_receive`] function, [`Middleware`] managed through [`Middleware::mid_packet_receive_before`]
/// and [`Middleware::mid_packet_receive_after`] to handle actions before and after the execution of [`IbcApplication::packet_receive`].

/// Specifically, the `before` functions are called before the linked `function` of the `inner` [`IbcApplication`] is invoked.
/// These `before` functions return a type of [`MiddlewareResponse`], which offers two alternatives:

/// - **Stop**: The `inner function` will not be called, and the value is returned directly.
/// - **Continue**: The `inner function` will be called. The result of the `inner function` is then passed to the
///   corresponding `after` function of the Middleware, where further actions can be performed.

/// ## Realistic Example: Implementing [`IbcHook`](crate::ibc_applications::IbcHook)
///
/// In the [`IbcHook`](crate::ibc_applications::IbcHook), during [`Middleware::mid_packet_receive_before`], if the `memo` of the [`FungibleTokenPacketData`] is set
/// to trigger the `IBC hook`, the `packet` is modified, and the `sender` is set according to the [`IBC hook standard`](https://github.com/osmosis-labs/osmosis/tree/main/x/ibc-hooks#:~:text=Sender%3A%20We%20cannot,the%20local%20chain.).
/// This `packet` is then returned with `Continue`, and passed to the inner [`IbcApplication`] (e.g., `Ics20` or
/// another [`Middleware`] if there are multiple wraps).
///
/// Once [`Ics20`](crate::ibc_applications::Ics20) completes the execution of [`IbcApplication::packet_receive`], [`Middleware::mid_packet_receive_after`] in the
/// [`IbcHook`](crate::ibc_applications::IbcHook) is triggered, where the smart contract is triggered to send the tokens. This functionality is
/// managed in the `after` function as the [`Ics20`](crate::ibc_applications::Ics20) module must mint the tokens first.
pub trait Middleware {
    /// Return the inner [`IbcApplication`]
    fn get_inner(&self) -> &dyn IbcAndStargate;

    /// Function triggered before the calling of inner [`IbcApplication::handle_outgoing_packet`].
    ///
    /// If the return type is [`MiddlewareResponse::Continue(IbcMsg)`], the returned [`IbcMsg`] will forwarded to the inner [`IbcApplication::handle_outgoing_packet`].
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]
    fn mid_handle_outgoing_packet(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        sender: Addr,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        msg: IbcMsg,
        channel: IbcChannelWrapper,
    ) -> AppResult<MiddlewareResponse<AppResponse, IbcMsg>> {
        Ok(MiddlewareResponse::Continue(msg))
    }

    /// Function triggered after [`IbcApplication::handle_outgoing_packet`] only if [`Middleware::mid_handle_outgoing_packet`] returned [`MiddlewareResponse::Continue`]
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]
    fn mid_handle_outgoing_packet_after(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        sender: Addr,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        original_msg: IbcMsg,
        forwarded_msg: IbcMsg,
        returning_reponse: AppResponse,
        channel: IbcChannelWrapper,
    ) -> AppResult<AppResponse> {
        Ok(AppResponse::default())
    }

    /// Function triggered before the calling of inner [`IbcApplication::packet_receive`].
    ///
    /// If the return type is [`MiddlewareResponse::Continue(IbcPacketReceiveMsg)`], the returned [`IbcPacketReceiveMsg`] will forwarded to the inner [`IbcApplication::packet_receive`].
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]
    fn mid_packet_receive_before(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        packet: IbcPacketReceiveMsg,
    ) -> InfallibleResult<
        MiddlewareResponse<PacketReceiveOk, IbcPacketReceiveMsg>,
        PacketReceiveFailing,
    > {
        InfallibleResult::Ok(MiddlewareResponse::Continue(packet))
    }

    /// Function triggered after [`IbcApplication::packet_receive`] only if [`Middleware::mid_packet_receive_before`] returned [`MiddlewareResponse::Continue`]
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]
    fn mid_packet_receive_after(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        original_packet: IbcPacketReceiveMsg,
        forwarded_packet: IbcPacketReceiveMsg,
        returning_reponse: InfallibleResult<PacketReceiveOk, PacketReceiveFailing>,
    ) -> InfallibleResult<MidRecOk, MidRecFailing> {
        InfallibleResult::Ok(MidRecOk {
            response: AppResponse::default(),
            ack: AckSetting::UseChildren,
        })
    }

    /// Function triggered before the calling of inner [`IbcApplication::packet_ack`].
    ///
    /// If the return type is [`MiddlewareResponse::Continue(AckPacket)`], the returned [`AckPacket`] will forwarded to the inner [`IbcApplication::packet_ack`].
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]

    fn mid_packet_ack_before(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        packet: AckPacket,
    ) -> AppResult<MiddlewareResponse<AppResponse, AckPacket>> {
        Ok(MiddlewareResponse::Continue(packet))
    }
    /// Function triggered after [`IbcApplication::packet_ack`] only if [`Middleware::mid_packet_ack_before`] returned [`MiddlewareResponse::Continue`]
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]

    fn mid_packet_ack_after(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        original_packet: AckPacket,
        forwarded_packet: AckPacket,
        returning_reponse: AppResponse,
    ) -> AppResult<AppResponse> {
        Ok(AppResponse::default())
    }

    /// Function triggered before the calling of inner [`IbcApplication::packet_timeout`].
    ///
    /// If the return type is [`MiddlewareResponse::Continue(TimeoutPacket)`], the returned [`TimeoutPacket`] will forwarded to the inner [`IbcApplication::packet_timeout`].
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]
    fn mid_packet_timeout_before(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        packet: TimeoutPacket,
    ) -> AppResult<MiddlewareResponse<AppResponse, TimeoutPacket>> {
        Ok(MiddlewareResponse::Continue(packet))
    }

    /// Function triggered after [`IbcApplication::packet_timeout`] only if [`Middleware::mid_packet_timeout_before`] returned [`MiddlewareResponse::Continue`]
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]
    fn mid_packet_timeout_after(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        torage: Rc<RefCell<&mut dyn Storage>>,
        original_packet: TimeoutPacket,
        forwarded_packet: TimeoutPacket,
        returning_reponse: AppResponse,
    ) -> AppResult<AppResponse> {
        Ok(AppResponse::default())
    }

    /// Function triggered before the calling of inner [`IbcApplication::open_channel`].
    ///
    /// If the return type is [`MiddlewareResponse::Continue(IbcChannelOpenMsg)`], the returned [`IbcChannelOpenMsg`] will forwarded to the inner [`IbcApplication::open_channel`].
    #[allow(unused_variables)]
    fn mid_open_channel_before(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        msg: IbcChannelOpenMsg,
    ) -> AppResult<MiddlewareResponse<AppResponse, IbcChannelOpenMsg>> {
        Ok(MiddlewareResponse::Continue(msg))
    }

    /// Function triggered after [`IbcApplication::open_channel`] only if [`Middleware::mid_open_channel_before`] returned [`MiddlewareResponse::Continue`]
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]
    fn mid_open_channel_after(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        original_msg: IbcChannelOpenMsg,
        forwarded_msg: IbcChannelOpenMsg,
        returning_reponse: AppResponse,
    ) -> AppResult<AppResponse> {
        Ok(AppResponse::default())
    }

    /// Function triggered before the calling of inner [`IbcApplication::channel_connect`].
    ///
    /// If the return type is [`MiddlewareResponse::Continue(IbcChannelConnectMsg)`], the returned [`IbcChannelConnectMsg`] will forwarded to the inner [`IbcApplication::channel_connect`].
    #[allow(unused_variables)]
    fn mid_channel_connect_before(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        msg: IbcChannelConnectMsg,
    ) -> AppResult<MiddlewareResponse<AppResponse, IbcChannelConnectMsg>> {
        Ok(MiddlewareResponse::Continue(msg))
    }

    /// Function triggered after [`IbcApplication::channel_connect`] only if [`Middleware::mid_channel_connect_before`] returned [`MiddlewareResponse::Continue`]
    #[allow(unused_variables)]
    #[allow(clippy::too_many_arguments)]
    fn mid_channel_connect_after(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        original_msg: IbcChannelConnectMsg,
        forwarded_msg: IbcChannelConnectMsg,
        returning_reponse: AppResponse,
    ) -> AppResult<AppResponse> {
        Ok(AppResponse::default())
    }
}

impl<T> IbcPortInterface for T
where
    T: Middleware,
{
    fn port_name(&self) -> String {
        self.get_inner().port_name()
    }
}

impl<T> IbcApplication for T
where
    T: Middleware + StargateUrls + 'static,
{
    fn init(&self, api: &cw_multi_test::MockApiBech32, storage: &mut dyn Storage) {
        self.get_inner().init(api, storage)
    }

    fn handle_outgoing_packet(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        sender: Addr,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        msg: IbcMsg,
        channel: IbcChannelWrapper,
    ) -> AppResult<AppResponse> {
        match self.mid_handle_outgoing_packet(
            api,
            block,
            sender.clone(),
            router,
            storage.clone(),
            msg.clone(),
            channel.clone(),
        )? {
            MiddlewareResponse::Stop(response) => Ok(response),
            MiddlewareResponse::Continue(next_msg) => {
                let sub_response = self.get_inner().handle_outgoing_packet(
                    api,
                    block,
                    sender.clone(),
                    router,
                    storage.clone(),
                    next_msg.clone(),
                    channel.clone(),
                )?;

                let res = self.mid_handle_outgoing_packet_after(
                    api,
                    block,
                    sender,
                    router,
                    storage,
                    msg,
                    next_msg,
                    sub_response.clone(),
                    channel,
                )?;

                Ok(res.merge(sub_response))
            }
        }
    }

    fn packet_receive(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        original_packet: IbcPacketReceiveMsg,
    ) -> InfallibleResult<PacketReceiveOk, PacketReceiveFailing> {
        match self.mid_packet_receive_before(
            api,
            block,
            router,
            storage.clone(),
            original_packet.clone(),
        ) {
            InfallibleResult::Ok(res) => match res {
                MiddlewareResponse::Stop(res) => InfallibleResult::Ok(res),
                MiddlewareResponse::Continue(next_packet) => {
                    let sub_response = self.get_inner().packet_receive(
                        api,
                        block,
                        router,
                        storage.clone(),
                        next_packet.clone(),
                    );

                    match self.mid_packet_receive_after(
                        api,
                        block,
                        router,
                        storage,
                        original_packet,
                        next_packet,
                        sub_response.clone(),
                    ) {
                        InfallibleResult::Ok(ok) => InfallibleResult::Ok(PacketReceiveOk {
                            response: ok.response.try_merge(sub_response.clone()),
                            ack: ok.ack.merge_ack(sub_response),
                        }),
                        InfallibleResult::Err(err) => InfallibleResult::Err(PacketReceiveFailing {
                            error: err.error,
                            ack: err.ack.merge_ack(sub_response),
                        }),
                    }
                }
            },
            InfallibleResult::Err(err) => InfallibleResult::Err(err),
        }
    }

    fn packet_ack(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        msg: AckPacket,
    ) -> AppResult<AppResponse> {
        match self.mid_packet_ack_before(api, block, router, storage.clone(), msg.clone())? {
            MiddlewareResponse::Stop(response) => Ok(response),
            MiddlewareResponse::Continue(next_packet) => {
                let sub_response = self.get_inner().packet_ack(
                    api,
                    block,
                    router,
                    storage.clone(),
                    next_packet.clone(),
                )?;

                let res = self.mid_packet_ack_after(
                    api,
                    block,
                    router,
                    storage,
                    msg,
                    next_packet,
                    sub_response.clone(),
                )?;
                Ok(res.merge(sub_response))
            }
        }
    }

    fn packet_timeout(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        msg: TimeoutPacket,
    ) -> AppResult<AppResponse> {
        match self.mid_packet_timeout_before(api, block, router, storage.clone(), msg.clone())? {
            MiddlewareResponse::Stop(response) => Ok(response),
            MiddlewareResponse::Continue(next_packet) => {
                let sub_response = self.get_inner().packet_timeout(
                    api,
                    block,
                    router,
                    storage.clone(),
                    msg.clone(),
                )?;

                let res = self.mid_packet_timeout_after(
                    api,
                    block,
                    router,
                    storage,
                    msg,
                    next_packet,
                    sub_response.clone(),
                )?;
                Ok(res.merge(sub_response))
            }
        }
    }

    fn open_channel(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        msg: IbcChannelOpenMsg,
    ) -> AppResult<AppResponse> {
        match self.mid_open_channel_before(api, block, router, storage.clone(), msg.clone())? {
            MiddlewareResponse::Stop(response) => Ok(response),
            MiddlewareResponse::Continue(next_msg) => {
                let sub_response = self.get_inner().open_channel(
                    api,
                    block,
                    router,
                    storage.clone(),
                    msg.clone(),
                )?;

                let res = self.mid_open_channel_after(
                    api,
                    block,
                    router,
                    storage,
                    msg,
                    next_msg,
                    sub_response.clone(),
                )?;

                Ok(res.merge(sub_response))
            }
        }
    }

    fn channel_connect(
        &self,
        api: &dyn Api,
        block: &BlockInfo,
        router: &RouterWrapper,
        storage: Rc<RefCell<&mut dyn Storage>>,
        msg: IbcChannelConnectMsg,
    ) -> AppResult<AppResponse> {
        match self.mid_channel_connect_before(api, block, router, storage.clone(), msg.clone())? {
            MiddlewareResponse::Stop(response) => Ok(response),
            MiddlewareResponse::Continue(next_msg) => {
                let sub_response = self.get_inner().channel_connect(
                    api,
                    block,
                    router,
                    storage.clone(),
                    msg.clone(),
                )?;
                let res = self.mid_channel_connect_after(
                    api,
                    block,
                    router,
                    storage,
                    msg,
                    next_msg,
                    sub_response.clone(),
                )?;

                Ok(res.merge(sub_response))
            }
        }
    }
}

impl<T> StargateName for T
where
    T: Middleware,
{
    fn stargate_name(&self) -> String {
        self.get_inner().stargate_name()
    }
}

impl<T> StargateApplication for T
where
    T: Middleware + StargateUrls + 'static,
{
    fn stargate_msg(
        &self,
        api: &dyn Api,
        storage: Rc<RefCell<&mut dyn Storage>>,
        router: &RouterWrapper,
        block: &BlockInfo,
        sender: Addr,
        type_url: String,
        data: cosmwasm_std::Binary,
    ) -> AppResult<AppResponse> {
        let res = self.get_inner().stargate_msg(
            api,
            storage.clone(),
            router,
            block,
            sender,
            type_url,
            data,
        )?;

        Ok(res)
    }

    fn stargate_query(
        &self,
        api: &dyn Api,
        storage: &dyn Storage,
        querier: &dyn cosmwasm_std::Querier,
        block: &BlockInfo,
        request: cosmwasm_std::GrpcQuery,
    ) -> AppResult<cosmwasm_std::Binary> {
        self.get_inner()
            .stargate_query(api, storage, querier, block, request)
    }
}

impl<T> StargateUrls for T
where
    T: Middleware,
{
    fn is_query_type_url(&self, type_url: String) -> bool {
        self.get_inner().is_query_type_url(type_url)
    }

    fn is_msg_type_url(&self, type_url: String) -> bool {
        self.get_inner().is_msg_type_url(type_url)
    }

    fn type_urls(&self) -> Vec<String> {
        self.get_inner().type_urls()
    }
}

impl<T> IbcAndStargate for T where T: IbcApplication + StargateApplication {}

/// Define how the ack has to be handled
pub enum AckSetting {
    /// The returned value override the ack returned from inner application.
    Replace(Binary),
    /// Clear the ack returned from the inner application.
    Remove,
    /// Use the inner ack.
    UseChildren,
}

impl AckSetting {
    pub(crate) fn merge_ack(
        &self,
        response: InfallibleResult<PacketReceiveOk, PacketReceiveFailing>,
    ) -> Option<Binary> {
        let ack = match response {
            InfallibleResult::Ok(ok) => ok.ack,
            InfallibleResult::Err(err) => err.ack,
        };

        match self {
            AckSetting::Replace(replace) => Some(replace.clone()),
            AckSetting::Remove => None,
            AckSetting::UseChildren => ack,
        }
    }
}

/// [Middleware::mid_packet_receive_after] Ok Result type.
pub struct MidRecOk {
    /// Response, it will be merged with inner application Response.
    pub response: AppResponse,
    /// Specifiy how ack has to be managed.
    pub ack: AckSetting,
}

impl MidRecOk {
    /// Create a [`MidRecOk`] with [`MidRecOk::ack`] as [AckSetting::UseChildren]
    pub fn use_children(response: AppResponse) -> Self {
        Self {
            response,
            ack: AckSetting::UseChildren,
        }
    }
}

impl Default for MidRecOk {
    fn default() -> Self {
        Self {
            response: Default::default(),
            ack: AckSetting::UseChildren,
        }
    }
}

/// [Middleware::mid_packet_receive_after] Err Result type.
pub struct MidRecFailing {
    /// Stringed error.
    pub error: String,
    /// Specifiy how ack has to be managed.
    pub ack: AckSetting,
}

impl MidRecFailing {
    /// Constructor
    pub fn new(error: impl Into<String>, ack: Binary) -> Self {
        Self {
            error: error.into(),
            ack: AckSetting::Replace(ack),
        }
    }
}