Skip to content

Commit a5ef644

Browse files
SSR: Extract error code out to a separate module
This is to make reusing it outside of parsing easier in a subsequent change.
1 parent 57ed622 commit a5ef644

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

crates/ra_ssr/src/errors.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Code relating to errors produced by SSR.
2+
3+
/// Constructs an SsrError taking arguments like the format macro.
4+
macro_rules! _error {
5+
($fmt:expr) => {$crate::SsrError::new(format!($fmt))};
6+
($fmt:expr, $($arg:tt)+) => {$crate::SsrError::new(format!($fmt, $($arg)+))}
7+
}
8+
pub(crate) use _error as error;
9+
10+
/// Returns from the current function with an error, supplied by arguments as for format!
11+
macro_rules! _bail {
12+
($($tokens:tt)*) => {return Err(crate::errors::error!($($tokens)*))}
13+
}
14+
pub(crate) use _bail as bail;
15+
16+
#[derive(Debug, PartialEq)]
17+
pub struct SsrError(pub(crate) String);
18+
19+
impl std::fmt::Display for SsrError {
20+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21+
write!(f, "Parse error: {}", self.0)
22+
}
23+
}
24+
25+
impl SsrError {
26+
pub(crate) fn new(message: impl Into<String>) -> SsrError {
27+
SsrError(message.into())
28+
}
29+
}

crates/ra_ssr/src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
mod matching;
77
mod parsing;
88
mod replacing;
9+
#[macro_use]
10+
mod errors;
911
#[cfg(test)]
1012
mod tests;
1113

14+
pub use crate::errors::SsrError;
1215
pub use crate::matching::Match;
1316
use crate::matching::{record_match_fails_reasons_scope, MatchFailureReason};
1417
use hir::Semantics;
@@ -41,9 +44,6 @@ pub struct SsrPattern {
4144
pattern: Option<SyntaxNode>,
4245
}
4346

44-
#[derive(Debug, PartialEq)]
45-
pub struct SsrError(String);
46-
4747
#[derive(Debug, Default)]
4848
pub struct SsrMatches {
4949
pub matches: Vec<Match>,
@@ -216,12 +216,6 @@ pub struct MatchDebugInfo {
216216
matched: Result<Match, MatchFailureReason>,
217217
}
218218

219-
impl std::fmt::Display for SsrError {
220-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
221-
write!(f, "Parse error: {}", self.0)
222-
}
223-
}
224-
225219
impl std::fmt::Debug for MatchDebugInfo {
226220
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
227221
write!(f, "========= PATTERN ==========\n")?;

crates/ra_ssr/src/parsing.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
//! search patterns, we go further and parse the pattern as each kind of thing that we can match.
66
//! e.g. expressions, type references etc.
77
8+
use crate::errors::bail;
89
use crate::{SsrError, SsrPattern, SsrRule};
910
use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind, T};
1011
use rustc_hash::{FxHashMap, FxHashSet};
1112
use std::str::FromStr;
1213

13-
/// Returns from the current function with an error, supplied by arguments as for format!
14-
macro_rules! bail {
15-
($e:expr) => {return Err($crate::SsrError::new($e))};
16-
($fmt:expr, $($arg:tt)+) => {return Err($crate::SsrError::new(format!($fmt, $($arg)+)))}
17-
}
18-
1914
#[derive(Clone, Debug)]
2015
pub(crate) struct SsrTemplate {
2116
pub(crate) tokens: Vec<PatternElement>,
@@ -246,7 +241,7 @@ fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placehold
246241
}
247242
}
248243
_ => {
249-
bail!("Placeholders should either be $name or ${name:constraints}");
244+
bail!("Placeholders should either be $name or ${{name:constraints}}");
250245
}
251246
}
252247
}
@@ -289,7 +284,7 @@ fn expect_token(tokens: &mut std::vec::IntoIter<Token>, expected: &str) -> Resul
289284
}
290285
bail!("Expected {} found {}", expected, t.text);
291286
}
292-
bail!("Expected {} found end of stream");
287+
bail!("Expected {} found end of stream", expected);
293288
}
294289

295290
impl NodeKind {
@@ -307,12 +302,6 @@ impl Placeholder {
307302
}
308303
}
309304

310-
impl SsrError {
311-
fn new(message: impl Into<String>) -> SsrError {
312-
SsrError(message.into())
313-
}
314-
}
315-
316305
#[cfg(test)]
317306
mod tests {
318307
use super::*;

0 commit comments

Comments
 (0)