From a5ef644a16f6d187fb6b612d30f19bf4f20fc6c4 Mon Sep 17 00:00:00 2001 From: David Lattimore Date: Tue, 23 Jun 2020 22:03:39 +1000 Subject: SSR: Extract error code out to a separate module This is to make reusing it outside of parsing easier in a subsequent change. --- crates/ra_ssr/src/errors.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 crates/ra_ssr/src/errors.rs (limited to 'crates/ra_ssr/src/errors.rs') diff --git a/crates/ra_ssr/src/errors.rs b/crates/ra_ssr/src/errors.rs new file mode 100644 index 000000000..c02bacae6 --- /dev/null +++ b/crates/ra_ssr/src/errors.rs @@ -0,0 +1,29 @@ +//! Code relating to errors produced by SSR. + +/// Constructs an SsrError taking arguments like the format macro. +macro_rules! _error { + ($fmt:expr) => {$crate::SsrError::new(format!($fmt))}; + ($fmt:expr, $($arg:tt)+) => {$crate::SsrError::new(format!($fmt, $($arg)+))} +} +pub(crate) use _error as error; + +/// Returns from the current function with an error, supplied by arguments as for format! +macro_rules! _bail { + ($($tokens:tt)*) => {return Err(crate::errors::error!($($tokens)*))} +} +pub(crate) use _bail as bail; + +#[derive(Debug, PartialEq)] +pub struct SsrError(pub(crate) String); + +impl std::fmt::Display for SsrError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "Parse error: {}", self.0) + } +} + +impl SsrError { + pub(crate) fn new(message: impl Into) -> SsrError { + SsrError(message.into()) + } +} -- cgit v1.2.3