1use miette::Diagnostic;
3use thiserror::Error;
4
5use crate::TestCaseInput;
6
7#[derive(Error, Diagnostic, Debug)]
8#[error("An error occurred while parsing testcases")]
9pub struct TestParseError {
11 #[related]
12 pub(crate) errors: Vec<TestErrorCase>,
13
14 #[source_code]
15 pub(crate) source_code: Option<TestCaseInput>,
16}
17
18impl From<kdl::KdlError> for TestParseError {
19 fn from(source: kdl::KdlError) -> Self {
20 TestParseError {
21 errors: vec![TestErrorCase::Kdl { source }],
22 source_code: None,
23 }
24 }
25}
26
27impl From<TestErrorCase> for TestParseError {
28 fn from(source: TestErrorCase) -> Self {
29 TestParseError {
30 errors: vec![source],
31 source_code: None,
32 }
33 }
34}
35
36#[derive(Error, Diagnostic, Debug)]
37pub enum TestErrorCase {
39 #[error("An error occurred while parsing the KDL data")]
41 Kdl {
42 #[source]
44 source: kdl::KdlError,
45 },
46
47 #[error("Not a valid test case")]
49 #[diagnostic(help("The outer items must all be `testcase`s"))]
50 NotTestcase {
51 #[label("Expected a `testcase`")]
53 span: miette::SourceSpan,
54 },
55
56 #[error("An argument was missing")]
58 MissingArgument {
59 #[label("This node is missing an argument")]
61 parent: miette::SourceSpan,
62
63 #[help]
65 missing: String,
66 },
67
68 #[error("An argument was of the wrong type")]
70 WrongArgumentType {
71 #[label("This node has an argument of a wrong kind")]
73 parent: miette::SourceSpan,
74
75 #[label("this one")]
77 argument: miette::SourceSpan,
78
79 #[help]
81 expected: String,
82 },
83
84 #[error("Could not find condition with this name")]
86 UnknownCondition {
87 #[label]
89 condition: miette::SourceSpan,
90 },
91
92 #[error("Could not find verb with this name")]
94 UnknownVerb {
95 #[label]
97 verb: miette::SourceSpan,
98 },
99
100 #[error("The condition is not valid in this position")]
102 InvalidCondition {
103 #[diagnostic_source]
105 error: miette::Error,
106 },
107}
108
109#[derive(Debug, Error, Diagnostic)]
110pub enum TestError {
112 #[error("An error occurred")]
114 Error {
115 #[diagnostic_source]
116 error: miette::Error,
118
119 #[label("in this node")]
120 span: miette::SourceSpan,
122 },
123
124 #[error("A panic occurred")]
126 Panic {
127 #[diagnostic_source]
128 error: miette::Error,
130
131 #[label("in this node")]
132 span: miette::SourceSpan,
134 },
135
136 #[error("The given condition failed")]
138 ConditionFailed {
139 #[label("in this node")]
140 span: miette::SourceSpan,
142 },
143}