binding_rust: Improve implementation of fmt::Display for QueryError trait, avoid multiple format!() calls

This commit is contained in:
Andrew Hlynskyi 2021-04-23 06:44:16 +03:00
parent e7b1d84a83
commit 3c0a49289c

View file

@ -1917,20 +1917,20 @@ impl fmt::Display for LanguageError {
impl fmt::Display for QueryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match self.kind {
QueryErrorKind::Capture => format!("Invalid capture name {}", self.message),
QueryErrorKind::Field => format!("Invalid field name {}", self.message),
QueryErrorKind::NodeType => format!("Invalid node type {}", self.message),
QueryErrorKind::Syntax => format!("Invalid syntax:\n{}", self.message),
QueryErrorKind::Structure => format!("Impossible pattern:\n{}", self.message),
QueryErrorKind::Predicate => format!("Invalid predicate: {}", self.message),
};
write!(
f,
"Query error at {}:{}. {}",
"Query error at {}:{}. {}{}",
self.row + 1,
self.column + 1,
msg
match self.kind {
QueryErrorKind::Field => "Invalid field name ",
QueryErrorKind::NodeType => "Invalid node type ",
QueryErrorKind::Capture => "Invalid capture name ",
QueryErrorKind::Predicate => "Invalid predicate: ",
QueryErrorKind::Structure => "Impossible pattern:\n",
QueryErrorKind::Syntax => "Invalid syntax:\n",
},
self.message
)
}
}