From f72b1505bebf8d2fe1a60d386a3a3c3b67d4f903 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 4 Dec 2019 20:20:02 +0100 Subject: =?UTF-8?q?Unify=20error=20types=20=E2=99=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/func/args.rs | 10 +++++----- src/func/macros.rs | 23 ++++++++--------------- src/func/mod.rs | 5 ++--- 3 files changed, 15 insertions(+), 23 deletions(-) (limited to 'src/func') diff --git a/src/func/args.rs b/src/func/args.rs index 2e9e80cc..d1d49b6a 100644 --- a/src/func/args.rs +++ b/src/func/args.rs @@ -64,14 +64,14 @@ impl<'a> ArgParser<'a> { if self.positional_index == self.args.positional.len() { Ok(()) } else { - pr!("unexpected argument"); + error!(unexpected_argument); } } /// Covert an option to a result with an error on `None`. fn expected(val: Option>) -> ParseResult> where T: Argument<'a> { - val.ok_or_else(|| pr!(@"expected {}", T::ERROR_MESSAGE)) + val.ok_or_else(|| error!(@"expected {}", T::ERROR_MESSAGE)) } } @@ -93,10 +93,10 @@ macro_rules! arg { const ERROR_MESSAGE: &'static str = $err; fn from_expr(expr: &'a Spanned) -> ParseResult> { - #[allow(unreachable_patterns)] + #[allow(unreachable_code)] match &expr.val { $wanted => Ok(Spanned::new($converted, expr.span)), - _ => pr!("expected {}", $err), + _ => error!("expected {}", $err), } } } @@ -179,7 +179,7 @@ impl AlignmentKey { Right if horizontal => axes.right(), Top if !horizontal => axes.top(), Bottom if !horizontal => axes.bottom(), - _ => lr!( + _ => error!( "invalid alignment `{}` for {} axis", format!("{:?}", self).to_lowercase(), format!("{:?}", axis).to_lowercase() diff --git a/src/func/macros.rs b/src/func/macros.rs index 78cf1f56..0ffdc857 100644 --- a/src/func/macros.rs +++ b/src/func/macros.rs @@ -111,7 +111,7 @@ macro_rules! function { macro_rules! parse { (forbidden: $body:expr) => { if $body.is_some() { - pr!("unexpected body"); + error!("unexpected body"); } }; @@ -127,23 +127,16 @@ macro_rules! parse { if let Some(body) = $body { $crate::syntax::parse(body, $ctx)? } else { - pr!("expected body"); + error!("expected body"); } ) } -/// Early-return with a formatted parsing error or yield -/// an error expression without returning when prefixed with `@`. +/// Early-return with a formatted typesetting error or construct an error +/// expression without returning when prefixed with `@`. #[macro_export] -macro_rules! pr { - (@$($tts:tt)*) => ($crate::syntax::ParseError::new(format!($($tts)*))); - ($($tts:tt)*) => (return Err(pr!(@$($tts)*));); -} - -/// Early-return with a formatted layouting error or yield -/// an error expression without returning when prefixed with `@`. -#[macro_export] -macro_rules! lr { - (@$($tts:tt)*) => ($crate::layout::LayoutError::new(format!($($tts)*))); - ($($tts:tt)*) => (return Err(lr!(@$($tts)*));); +macro_rules! error { + (@unexpected_argument) => (error!(@"unexpected argument")); + (@$($tts:tt)*) => ($crate::TypesetError::with_message(format!($($tts)*))); + ($($tts:tt)*) => (return Err(error!(@$($tts)*));); } diff --git a/src/func/mod.rs b/src/func/mod.rs index b16eecb8..31e31592 100644 --- a/src/func/mod.rs +++ b/src/func/mod.rs @@ -16,7 +16,7 @@ pub mod prelude { pub use super::args::*; pub use super::{Scope, ParseFunc, LayoutFunc, Command, Commands}; pub use crate::syntax::{SyntaxTree, FuncHeader, FuncArgs, Expression, Spanned, Span}; - pub use crate::syntax::{parse, ParseContext, ParseError, ParseResult}; + pub use crate::syntax::{parse, ParseContext, ParseResult}; pub use crate::size::{Size, Size2D, SizeBox}; pub use crate::style::{PageStyle, TextStyle}; pub use crate::layout::{ @@ -24,8 +24,7 @@ pub mod prelude { LayoutContext, LayoutSpace, LayoutSpaces, LayoutAxes, Axis, GenericAxisKind, SpecificAxisKind, LayoutAlignment, Alignment, - SpacingKind, - LayoutError, LayoutResult, + SpacingKind, LayoutResult, }; } -- cgit v1.2.3