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
use core::fmt::{Display, Formatter};

/// The result type of most interactive methods.
pub type Result<'a, T> = core::result::Result<T, InteractiveError<'a>>;

/// The main error type of this crate.
#[allow(missing_docs)]
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum InteractiveError<'a> {
    InteractiveNotImplemented {
        type_name: &'a str,
    },
    MethodsNotImplemented {
        type_name: &'a str,
    },
    DebugNotImplemented {
        type_name: &'static str,
    },
    FieldNotFound {
        type_name: &'a str,
        field_name: &'a str,
    },
    MethodNotFound {
        type_name: &'a str,
        method_name: &'a str,
    },
    FunctionNotFound {
        function_name: &'a str,
    },
    WrongNumberOfArguments {
        method_name: &'a str,
        expected: usize,
        found: usize,
    },
    ArgParseError {
        method_name: &'a str,
        error: ArgParseError<'a>,
    },
    SyntaxError,
}

impl Display for InteractiveError<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            InteractiveError::InteractiveNotImplemented { type_name } => {
                write!(f, "`{}` doesn't implement `Interactive`", type_name)
            }
            InteractiveError::MethodsNotImplemented { type_name } => {
                write!(f, "`{}` doesn't implement `Methods`", type_name)
            }
            InteractiveError::DebugNotImplemented { type_name } => {
                write!(f, "´{}´ doesn't implement ´Debug´", type_name)
            }
            InteractiveError::FieldNotFound {
                type_name,
                field_name,
            } => write!(
                f,
                "No field `{}` found for type `{}`",
                field_name, type_name
            ),
            InteractiveError::MethodNotFound {
                method_name,
                type_name,
            } => write!(
                f,
                "No method named `{}` found for type `{}`",
                method_name, type_name
            ),
            InteractiveError::FunctionNotFound { function_name } => {
                write!(f, "No function named `{}` found", function_name)
            }
            InteractiveError::WrongNumberOfArguments {
                method_name,
                expected,
                found,
            } => {
                let arguments_1 = if *expected == 1 {
                    "argument"
                } else {
                    "arguments"
                };
                let arguments_2 = if *found == 1 { "argument" } else { "arguments" };
                let was_were = if *found == 1 { "was" } else { "were" };
                write!(
                    f,
                    "´{}´ takes {} {} but {} {} {} supplied",
                    method_name, expected, arguments_1, found, arguments_2, was_were
                )
            }
            InteractiveError::ArgParseError { error, .. } => write!(
                f,
                "Couldn't parse method/function argument(s)\n{:?}",
                error // TODO improve message
            ),
            InteractiveError::SyntaxError => write!(f, "Syntax Error"),
        }
    }
}
/// Contains information about function or method argument parsing errors.
///
/// It is used inside the [`InteractiveError::ArgParseError`] variant.
#[allow(missing_docs)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum ArgParseError<'a> {
    ParseIntError(core::num::ParseIntError),
    ParseCharError(core::char::ParseCharError),
    ParseFloatError(core::num::ParseFloatError),
    ParseBoolError(core::str::ParseBoolError),

    /// Produced when parsing string-like types.
    UnescapeError(&'a str),
}