Skip to content

Commit

Permalink
refactor: Avoid checking the string message of a fatal error
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed Jun 14, 2022
1 parent 9b544a0 commit 62fdaa8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
29 changes: 13 additions & 16 deletions libflux/flux-core/src/ast/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ pub fn check(node: walk::Node) -> Result<(), Errors<Error>> {
let errors = &mut self.errors;

if self.depth > MAX_DEPTH {
errors.push(located(
n.base().location.clone(),
ErrorKind {
message: String::from("Program is nested to deep"),
},
));
errors.push(located(n.base().location.clone(), ErrorKind::NestedToDeep));

return false;
}
Expand All @@ -40,7 +35,7 @@ pub fn check(node: walk::Node) -> Result<(), Errors<Error>> {
for err in n.base().errors.iter() {
errors.push(located(
n.base().location.clone(),
ErrorKind {
ErrorKind::Message {
message: err.clone(),
},
));
Expand All @@ -49,13 +44,13 @@ pub fn check(node: walk::Node) -> Result<(), Errors<Error>> {
match n {
walk::Node::BadStmt(n) => errors.push(located(
n.base.location.clone(),
ErrorKind {
ErrorKind::Message {
message: format!("invalid statement: {}", n.text),
},
)),
walk::Node::BadExpr(n) if !n.text.is_empty() => errors.push(located(
n.base.location.clone(),
ErrorKind {
ErrorKind::Message {
message: format!("invalid expression: {}", n.text),
},
)),
Expand All @@ -70,7 +65,7 @@ pub fn check(node: walk::Node) -> Result<(), Errors<Error>> {
if let PropertyKey::StringLit(s) = &p.key {
errors.push(located(
n.base.location.clone(),
ErrorKind {
ErrorKind::Message {
message: format!(
"string literal key {} must have a value",
s.value
Expand All @@ -88,7 +83,7 @@ pub fn check(node: walk::Node) -> Result<(), Errors<Error>> {
if has_implicit && has_explicit {
errors.push(located(
n.base.location.clone(),
ErrorKind {
ErrorKind::Message {
message: String::from(
"cannot mix implicit and explicit properties",
),
Expand Down Expand Up @@ -123,15 +118,17 @@ pub type Error = Located<ErrorKind>;

/// An error that can be returned while checking the AST.
#[derive(Error, Debug, PartialEq)]
#[error("{}", message)]
pub struct ErrorKind {
/// Error message.
pub message: String,
#[allow(missing_docs)]
pub enum ErrorKind {
#[error("Program is nested to deep")]
NestedToDeep,
#[error("{message}")]
Message { message: String },
}

impl ErrorKind {
pub(crate) fn is_fatal(&self) -> bool {
self.message.contains("Program is nested to deep")
matches!(self, Self::NestedToDeep)
}
}

Expand Down
8 changes: 4 additions & 4 deletions libflux/flux-core/src/ast/check/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn test_object_check() {
},
source: Some(String::from("{c: 2, a}")),
},
ErrorKind {
ErrorKind::Message {
message: String::from("cannot mix implicit and explicit properties"),
},
)]));
Expand All @@ -39,7 +39,7 @@ fn test_bad_stmt() {
end: Position { line: 3, column: 3 },
source: Some(String::from("=")),
},
ErrorKind {
ErrorKind::Message {
message: String::from("invalid statement: ="),
},
)]));
Expand All @@ -60,7 +60,7 @@ fn test_bad_expr() {
},
source: Some(String::from("/")),
},
ErrorKind {
ErrorKind::Message {
message: String::from("invalid expression: invalid token for primary expression: DIV"),
},
)]));
Expand Down Expand Up @@ -131,6 +131,6 @@ fn test_check_collect_existing_error() {
let got = check(walk::Node::File(&file)).unwrap_err();
assert_eq!(3, got.len());
for (i, err) in got.iter().enumerate() {
assert_eq!(err.error.message, format!("error {}", i + 1));
assert_eq!(err.error.to_string(), format!("error {}", i + 1));
}
}

0 comments on commit 62fdaa8

Please sign in to comment.