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
use core::marker::PhantomData;

use crate::{
    api::{EndpointArgumentApi, EndpointArgumentApiImpl, ErrorApi, ErrorApiImpl, ManagedTypeApi},
    err_msg, ArgDecodeInput, DynArgInput,
};

#[derive(Default)]
pub struct EndpointDynArgLoader<AA>
where
    AA: ManagedTypeApi + ErrorApi + EndpointArgumentApi,
{
    _phantom: PhantomData<AA>,
    current_index: i32,
    num_arguments: i32,
}

impl<AA> EndpointDynArgLoader<AA>
where
    AA: ManagedTypeApi + ErrorApi + EndpointArgumentApi,
{
    pub fn new() -> Self {
        let num_arguments = AA::argument_api_impl().get_num_arguments();
        EndpointDynArgLoader {
            _phantom: PhantomData,
            current_index: 0,
            num_arguments,
        }
    }
}

impl<AA> DynArgInput for EndpointDynArgLoader<AA>
where
    AA: ManagedTypeApi + ErrorApi + EndpointArgumentApi,
{
    type ItemInput = ArgDecodeInput<AA>;

    type ManagedTypeErrorApi = AA;

    fn has_next(&self) -> bool {
        self.current_index < self.num_arguments
    }

    fn next_arg_input(&mut self) -> ArgDecodeInput<AA> {
        if self.current_index >= self.num_arguments {
            AA::error_api_impl().signal_error(err_msg::ARG_WRONG_NUMBER)
        } else {
            let arg_input = ArgDecodeInput::new(self.current_index);
            self.current_index += 1;
            arg_input
        }
    }

    fn flush_ignore(&mut self) {
        self.current_index = self.num_arguments;
    }
}