inkling/error/parse/
condition.rs1use std::{error::Error, fmt};
4
5use crate::error::parse::{expression::ExpressionError, variable::VariableError};
6
7#[derive(Debug)]
8pub struct ConditionError {
10 pub content: String,
12 pub kind: ConditionErrorKind,
14}
15
16#[derive(Debug)]
17pub enum ConditionErrorKind {
19 BadLink,
26 BadValue,
28 CouldNotParse,
30 InvalidExpression(ExpressionError),
32 InvalidVariable(VariableError),
34 MultipleElseStatements,
36 NoCondition,
38 UnmatchedParenthesis,
40}
41
42impl Error for ConditionError {
43 fn source(&self) -> Option<&(dyn Error + 'static)> {
44 Some(&self.kind)
45 }
46}
47
48impl Error for ConditionErrorKind {
49 fn source(&self) -> Option<&(dyn Error + 'static)> {
50 match &self {
51 ConditionErrorKind::InvalidVariable(err) => Some(err),
52 _ => None,
53 }
54 }
55}
56
57impl_from_error![
58 ConditionErrorKind;
59 [InvalidExpression, ExpressionError],
60 [InvalidVariable, VariableError]
61];
62
63impl fmt::Display for ConditionError {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 write!(f, "{} (condition string: '{}')", &self.kind, &self.content)
66 }
67}
68
69impl fmt::Display for ConditionErrorKind {
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 use ConditionErrorKind::*;
72
73 match &self {
74 BadLink => write!(
75 f,
76 "internal error: did not correctly partition conditions into parts separated \
77 by `and`/`or` markers"
78 ),
79 BadValue => write!(f, "could not parse a number from the condition value"),
80 CouldNotParse => write!(f, "incorrectly formatted condition"),
81 InvalidExpression(err) => write!(
82 f,
83 "could not parse left or right hand side expression for a comparison: {}",
84 err
85 ),
86 InvalidVariable(err) => write!(f, "could not parse variable in condition: {}", err),
87 MultipleElseStatements => write!(f, "found multiple else statements in condition"),
88 NoCondition => write!(f, "condition string was empty"),
89 UnmatchedParenthesis => write!(f, "contained unmatched parenthesis"),
90 }
91 }
92}
93
94impl ConditionError {
95 pub(crate) fn from_kind<T: Into<String>>(content: T, kind: ConditionErrorKind) -> Self {
97 ConditionError {
98 content: content.into(),
99 kind,
100 }
101 }
102}