rusteval/
error.rs

1use core::fmt::{Display, Formatter};
2
3/// The result type of most interactive methods.
4pub type Result<'a, T> = core::result::Result<T, InteractiveError<'a>>;
5
6/// The main error type of this crate.
7#[allow(missing_docs)]
8#[non_exhaustive]
9#[derive(Debug, PartialEq, Eq, Clone)]
10pub enum InteractiveError<'a> {
11    InteractiveNotImplemented {
12        type_name: &'a str,
13    },
14    MethodsNotImplemented {
15        type_name: &'a str,
16    },
17    DebugNotImplemented {
18        type_name: &'static str,
19    },
20    FieldNotFound {
21        type_name: &'a str,
22        field_name: &'a str,
23    },
24    MethodNotFound {
25        type_name: &'a str,
26        method_name: &'a str,
27    },
28    FunctionNotFound {
29        function_name: &'a str,
30    },
31    WrongNumberOfArguments {
32        method_name: &'a str,
33        expected: usize,
34        found: usize,
35    },
36    ArgParseError {
37        method_name: &'a str,
38        error: ArgParseError<'a>,
39    },
40    SyntaxError,
41}
42
43impl Display for InteractiveError<'_> {
44    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
45        match self {
46            InteractiveError::InteractiveNotImplemented { type_name } => {
47                write!(f, "`{}` doesn't implement `Interactive`", type_name)
48            }
49            InteractiveError::MethodsNotImplemented { type_name } => {
50                write!(f, "`{}` doesn't implement `Methods`", type_name)
51            }
52            InteractiveError::DebugNotImplemented { type_name } => {
53                write!(f, "´{}´ doesn't implement ´Debug´", type_name)
54            }
55            InteractiveError::FieldNotFound {
56                type_name,
57                field_name,
58            } => write!(
59                f,
60                "No field `{}` found for type `{}`",
61                field_name, type_name
62            ),
63            InteractiveError::MethodNotFound {
64                method_name,
65                type_name,
66            } => write!(
67                f,
68                "No method named `{}` found for type `{}`",
69                method_name, type_name
70            ),
71            InteractiveError::FunctionNotFound { function_name } => {
72                write!(f, "No function named `{}` found", function_name)
73            }
74            InteractiveError::WrongNumberOfArguments {
75                method_name,
76                expected,
77                found,
78            } => {
79                let arguments_1 = if *expected == 1 {
80                    "argument"
81                } else {
82                    "arguments"
83                };
84                let arguments_2 = if *found == 1 { "argument" } else { "arguments" };
85                let was_were = if *found == 1 { "was" } else { "were" };
86                write!(
87                    f,
88                    "´{}´ takes {} {} but {} {} {} supplied",
89                    method_name, expected, arguments_1, found, arguments_2, was_were
90                )
91            }
92            InteractiveError::ArgParseError { error, .. } => write!(
93                f,
94                "Couldn't parse method/function argument(s)\n{:?}",
95                error // TODO improve message
96            ),
97            InteractiveError::SyntaxError => write!(f, "Syntax Error"),
98        }
99    }
100}
101/// Contains information about function or method argument parsing errors.
102///
103/// It is used inside the [`InteractiveError::ArgParseError`] variant.
104#[allow(missing_docs)]
105#[derive(Debug, Eq, PartialEq, Clone)]
106pub enum ArgParseError<'a> {
107    ParseIntError(core::num::ParseIntError),
108    ParseCharError(core::char::ParseCharError),
109    ParseFloatError(core::num::ParseFloatError),
110    ParseBoolError(core::str::ParseBoolError),
111
112    /// Produced when parsing string-like types.
113    UnescapeError(&'a str),
114}