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
//! # Cli helpers
//!
//! a variety of useful tools

// module with cli tools
pub mod cli {

    use std::fmt::{Debug, Display};
    use std::io::{self};
    use std::io::{stdin, Stdin};

    pub trait Reader {
        fn read_string(&self) -> Result<String, io::Error>;
    }

    #[derive(Debug, PartialEq)]
    pub enum ErrorKind {
        IoError,
        AttemptsExceedError,
        InputRequirementError,
    }

    #[derive(Debug)]
    pub struct InputReadError {
        msg: String,
        kind: ErrorKind,
    }

    impl InputReadError {
        pub fn kind(&self) -> &ErrorKind {
            &self.kind
        }
    }

    impl Display for InputReadError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{}", self.msg)
        }
    }

    impl From<io::Error> for InputReadError {
        fn from(value: io::Error) -> Self {
            InputReadError {
                msg: value.to_string(),
                kind: ErrorKind::IoError,
            }
        }
    }

    pub trait Claimy<F, Ft, Fe>
    where
        F: Fn(&str) -> Result<Ft, Fe>,
        Fe: Display + Debug,
    {
        fn demand(&self, claim: F) -> Result<String, InputReadError>;

        fn demand_until(&self, claim: F, attempts: Option<u8>) -> Result<String, InputReadError>;
    }

    pub struct Input<T>
    where
        T: Reader,
    {
        reader: T,
    }

    impl Reader for Stdin {
        fn read_string(&self) -> Result<String, io::Error> {
            let mut buf = String::new();
            self.read_line(&mut buf)?;
            Ok(buf)
        }
    }

    impl<T> Input<T>
    where
        T: Reader,
    {
        pub fn new(reader: T) -> Input<T> {
            Input { reader }
        }

        pub fn read(&self) -> Result<String, InputReadError> {
            let inp = self.reader.read_string()?;
            Ok(inp)
        }

        pub fn reader(&self) -> &T
        where
            T: Reader,
        {
            &self.reader
        }
    }

    impl<R, F, Ft, Fe> Claimy<F, Ft, Fe> for Input<R>
    where
        R: Reader,
        F: Fn(&str) -> Result<Ft, Fe>,
        Fe: Display + Debug,
    {
        fn demand(&self, claim: F) -> Result<String, InputReadError> {
            let inp = self.reader.read_string()?;
            match claim(&inp) {
                Ok(_) => Ok(inp),
                Err(err) => Err(InputReadError {
                    msg: format!("wrong input. {}", err),
                    kind: ErrorKind::InputRequirementError,
                }),
            }
        }

        /// read and check
        ///
        /// reads an input and passes it to the predicate
        /// until a predicate isn't positive
        ///
        fn demand_until(&self, claim: F, attempts: Option<u8>) -> Result<String, InputReadError> {
            let attempts = if let Some(attempts) = attempts {
                attempts
            } else {
                3u8
            };
            let mut last_error_msg = "".to_string();
            for attempt in 0..attempts {
                let input_str = self.read()?;
                let claim_res = claim(&input_str);
                if claim_res.is_ok() {
                    return Ok(input_str);
                } else if attempt == attempts - 1 {
                    last_error_msg = claim_res.err().unwrap().to_string();
                }
            }
            Err(InputReadError {
                msg: format!("attempts to read input were failed. {}", last_error_msg),
                kind: ErrorKind::AttemptsExceedError,
            })
        }
    }

    impl Default for Input<Stdin> {
        fn default() -> Self {
            Input { reader: stdin() }
        }
    }
}

#[cfg(test)]
mod tests;