summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-06-12 16:43:49 +0200
committerLaurenz <laurmaedje@gmail.com>2023-06-12 16:47:46 +0200
commitd3b4d7da9a801dac3af6a3cf52eb55af83adc5f5 (patch)
tree72ab79ed75dddfb7b97ddf02ba4d19c4efc9ea5a /src
parent378ebe5f5601f11c3f428c17bed492012feb251e (diff)
More `bail!` usage
Diffstat (limited to 'src')
-rw-r--r--src/diag.rs9
-rw-r--r--src/eval/mod.rs2
-rw-r--r--src/eval/ops.rs4
-rw-r--r--src/eval/scope.rs4
-rw-r--r--src/eval/str.rs6
-rw-r--r--src/eval/symbol.rs4
-rw-r--r--src/geom/align.rs4
-rw-r--r--src/geom/axes.rs2
-rw-r--r--src/geom/mod.rs2
-rw-r--r--src/model/introspect.rs4
-rw-r--r--src/model/selector.rs8
11 files changed, 29 insertions, 20 deletions
diff --git a/src/diag.rs b/src/diag.rs
index d9f88024..4fb9ecc0 100644
--- a/src/diag.rs
+++ b/src/diag.rs
@@ -13,6 +13,15 @@ use crate::syntax::{ErrorPos, Span, Spanned};
use crate::World;
/// Early-return with a [`StrResult`] or [`SourceResult`].
+///
+/// If called with just a string and format args, returns with a
+/// `StrResult`. If called with a span, a string and format args, returns
+/// a `SourceResult`.
+///
+/// ```
+/// bail!("bailing with a {}", "string result");
+/// bail!(span, "bailing with a {}", "source result");
+/// ```
#[macro_export]
#[doc(hidden)]
macro_rules! __bail {
diff --git a/src/eval/mod.rs b/src/eval/mod.rs
index fe11606a..93a73ea4 100644
--- a/src/eval/mod.rs
+++ b/src/eval/mod.rs
@@ -256,7 +256,7 @@ impl<'a> Vm<'a> {
}
}
- Err("cannot access file system from here".into())
+ bail!("cannot access file system from here")
}
}
diff --git a/src/eval/ops.rs b/src/eval/ops.rs
index bea2ea82..0880a87e 100644
--- a/src/eval/ops.rs
+++ b/src/eval/ops.rs
@@ -6,7 +6,7 @@ use std::fmt::Debug;
use ecow::eco_format;
use super::{format_str, Regex, Value};
-use crate::diag::StrResult;
+use crate::diag::{bail, StrResult};
use crate::geom::{Axes, Axis, GenAlign, Length, Numeric, PartialStroke, Rel, Smart};
use Value::*;
@@ -219,7 +219,7 @@ pub fn mul(lhs: Value, rhs: Value) -> StrResult<Value> {
/// Compute the quotient of two values.
pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> {
if is_zero(&rhs) {
- Err("cannot divide by zero")?;
+ bail!("cannot divide by zero");
}
Ok(match (lhs, rhs) {
diff --git a/src/eval/scope.rs b/src/eval/scope.rs
index f2aa26fb..f3e13715 100644
--- a/src/eval/scope.rs
+++ b/src/eval/scope.rs
@@ -5,7 +5,7 @@ use std::hash::Hash;
use ecow::{eco_format, EcoString};
use super::{IntoValue, Library, Value};
-use crate::diag::StrResult;
+use crate::diag::{bail, StrResult};
/// A stack of scopes.
#[derive(Debug, Default, Clone)]
@@ -171,7 +171,7 @@ impl Slot {
match self.kind {
Kind::Normal => Ok(&mut self.value),
Kind::Captured => {
- Err("variables from outside the function are read-only and cannot be modified")?
+ bail!("variables from outside the function are read-only and cannot be modified")
}
}
}
diff --git a/src/eval/str.rs b/src/eval/str.rs
index e3e83f3a..f5e5ab00 100644
--- a/src/eval/str.rs
+++ b/src/eval/str.rs
@@ -7,7 +7,7 @@ use ecow::EcoString;
use unicode_segmentation::UnicodeSegmentation;
use super::{cast, dict, Args, Array, Dict, Func, IntoValue, Value, Vm};
-use crate::diag::{At, SourceResult, StrResult};
+use crate::diag::{bail, At, SourceResult, StrResult};
use crate::geom::GenAlign;
/// Create a new [`Str`] from a format string.
@@ -507,7 +507,7 @@ cast! {
let mut chars = string.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => c,
- _ => Err("expected exactly one character")?,
+ _ => bail!("expected exactly one character"),
}
},
}
@@ -600,7 +600,7 @@ cast! {
align: GenAlign => match align {
GenAlign::Start => Self::Start,
GenAlign::End => Self::End,
- _ => Err("expected either `start` or `end`")?,
+ _ => bail!("expected either `start` or `end`"),
},
}
diff --git a/src/eval/symbol.rs b/src/eval/symbol.rs
index 8f4c93f8..0925202e 100644
--- a/src/eval/symbol.rs
+++ b/src/eval/symbol.rs
@@ -5,7 +5,7 @@ use std::sync::Arc;
use ecow::EcoString;
-use crate::diag::StrResult;
+use crate::diag::{bail, StrResult};
/// A symbol, possibly with variants.
#[derive(Clone, Eq, PartialEq, Hash)]
@@ -72,7 +72,7 @@ impl Symbol {
}
}
- Err("unknown symbol modifier".into())
+ bail!("unknown symbol modifier")
}
/// The characters that are covered by this symbol.
diff --git a/src/geom/align.rs b/src/geom/align.rs
index dca35891..47acd3a6 100644
--- a/src/geom/align.rs
+++ b/src/geom/align.rs
@@ -216,7 +216,7 @@ cast! {
self => self.0.into_value(),
align: GenAlign => {
if align.axis() != Axis::X {
- Err("alignment must be horizontal")?;
+ bail!("alignment must be horizontal");
}
Self(align)
},
@@ -232,7 +232,7 @@ cast! {
self => self.0.into_value(),
align: GenAlign => {
if align.axis() != Axis::Y {
- Err("alignment must be vertical")?;
+ bail!("alignment must be vertical");
}
Self(align)
},
diff --git a/src/geom/axes.rs b/src/geom/axes.rs
index 35c94129..059d3bb2 100644
--- a/src/geom/axes.rs
+++ b/src/geom/axes.rs
@@ -280,7 +280,7 @@ cast! {
let mut iter = array.into_iter();
match (iter.next(), iter.next(), iter.next()) {
(Some(a), Some(b), None) => Axes::new(a.cast()?, b.cast()?),
- _ => Err("point array must contain exactly two entries")?,
+ _ => bail!("point array must contain exactly two entries"),
}
},
}
diff --git a/src/geom/mod.rs b/src/geom/mod.rs
index 4a9ecfe1..b7a7ff40 100644
--- a/src/geom/mod.rs
+++ b/src/geom/mod.rs
@@ -61,7 +61,7 @@ use std::hash::{Hash, Hasher};
use std::iter::Sum;
use std::ops::*;
-use crate::diag::StrResult;
+use crate::diag::{bail, StrResult};
use crate::eval::{array, cast, Array, Dict, Value};
use crate::model::{Fold, Resolve, StyleChain};
diff --git a/src/model/introspect.rs b/src/model/introspect.rs
index b150fabf..42c1a9e1 100644
--- a/src/model/introspect.rs
+++ b/src/model/introspect.rs
@@ -9,7 +9,7 @@ use ecow::EcoVec;
use indexmap::IndexMap;
use super::{Content, Selector};
-use crate::diag::StrResult;
+use crate::diag::{bail, StrResult};
use crate::doc::{Frame, FrameItem, Meta, Position};
use crate::eval::{cast, Value};
use crate::geom::{Point, Transform};
@@ -313,7 +313,7 @@ impl Introspector {
let mut found = None;
for elem in self.all().filter(|elem| elem.label() == Some(label)) {
if found.is_some() {
- return Err("label occurs multiple times in the document".into());
+ bail!("label occurs multiple times in the document");
}
found = Some(elem.clone());
}
diff --git a/src/model/selector.rs b/src/model/selector.rs
index 5a2b11cb..9723ee4f 100644
--- a/src/model/selector.rs
+++ b/src/model/selector.rs
@@ -5,7 +5,7 @@ use std::sync::Arc;
use ecow::{eco_format, EcoString, EcoVec};
use super::{Content, ElemFunc, Label, Location};
-use crate::diag::StrResult;
+use crate::diag::{bail, StrResult};
use crate::eval::{
cast, CastInfo, Dict, FromValue, Func, IntoValue, Reflect, Regex, Value,
};
@@ -201,8 +201,8 @@ impl FromValue for LocatableSelector {
}
Selector::Location(_) => {}
Selector::Label(_) => {}
- Selector::Regex(_) => Err("text is not locatable")?,
- Selector::Can(_) => Err("capability is not locatable")?,
+ Selector::Regex(_) => bail!("text is not locatable"),
+ Selector::Can(_) => bail!("capability is not locatable"),
Selector::Or(list) | Selector::And(list) => {
for selector in list {
validate(selector)?;
@@ -279,7 +279,7 @@ impl FromValue for ShowableSelector {
| Selector::Can(_)
| Selector::Before { .. }
| Selector::After { .. } => {
- Err("this selector cannot be used with show")?
+ bail!("this selector cannot be used with show")
}
}
Ok(())