diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-27 00:49:02 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-27 01:00:57 +0100 |
| commit | 7caf98fe42797eab59a39ef71071030c9790245a (patch) | |
| tree | cadf45a53aad29f11bf30a164f646b5a73bb5f16 /src/model | |
| parent | 6bafc6391061d4b589dea835705a08b25a4df9f8 (diff) | |
Switch `StrResult` to `EcoString`
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/array.rs | 10 | ||||
| -rw-r--r-- | src/model/cast.rs | 12 | ||||
| -rw-r--r-- | src/model/dict.rs | 6 | ||||
| -rw-r--r-- | src/model/ops.rs | 5 | ||||
| -rw-r--r-- | src/model/str.rs | 4 | ||||
| -rw-r--r-- | src/model/value.rs | 2 |
6 files changed, 22 insertions, 17 deletions
diff --git a/src/model/array.rs b/src/model/array.rs index 5a2f8672..06d1b588 100644 --- a/src/model/array.rs +++ b/src/model/array.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use super::{ops, Args, Func, Value, Vm}; use crate::diag::{At, SourceResult, StrResult}; use crate::syntax::Spanned; -use crate::util::ArcExt; +use crate::util::{format_eco, ArcExt, EcoString}; /// Create a new [`Array`] from values. #[macro_export] @@ -253,7 +253,7 @@ impl Array { vec.sort_by(|a, b| { a.partial_cmp(b).unwrap_or_else(|| { if result.is_ok() { - result = Err(format!( + result = Err(format_eco!( "cannot order {} and {}", a.type_name(), b.type_name(), @@ -294,13 +294,13 @@ impl Array { /// The out of bounds access error message. #[cold] -fn out_of_bounds(index: i64, len: i64) -> String { - format!("array index out of bounds (index: {}, len: {})", index, len) +fn out_of_bounds(index: i64, len: i64) -> EcoString { + format_eco!("array index out of bounds (index: {}, len: {})", index, len) } /// The error message when the array is empty. #[cold] -fn array_is_empty() -> String { +fn array_is_empty() -> EcoString { "array is empty".into() } diff --git a/src/model/cast.rs b/src/model/cast.rs index a4a3fe4e..df3c8c81 100644 --- a/src/model/cast.rs +++ b/src/model/cast.rs @@ -9,7 +9,7 @@ use crate::geom::{ Axes, Corners, Dir, GenAlign, Get, Length, Paint, PartialStroke, Point, Rel, Sides, }; use crate::syntax::Spanned; -use crate::util::EcoString; +use crate::util::{format_eco, EcoString}; /// Cast from a value to a specific type. pub trait Cast<V = Value>: Sized { @@ -94,7 +94,11 @@ macro_rules! __castable { v => v.type_name(), }; - Err(format!("expected {}, found {}", $expected, found)) + Err($crate::util::format_eco!( + "expected {}, found {}", + $expected, + found, + )) } } }; @@ -426,7 +430,7 @@ where }; if let Some((key, _)) = dict.iter().next() { - return Err(format!("unexpected key {key:?}")); + return Err(format_eco!("unexpected key {key:?}")); } Ok(sides.map(Option::unwrap_or_default)) @@ -468,7 +472,7 @@ where }; if let Some((key, _)) = dict.iter().next() { - return Err(format!("unexpected key {key:?}")); + return Err(format_eco!("unexpected key {key:?}")); } Ok(corners.map(Option::unwrap_or_default)) diff --git a/src/model/dict.rs b/src/model/dict.rs index 1c299795..d54a0e82 100644 --- a/src/model/dict.rs +++ b/src/model/dict.rs @@ -7,7 +7,7 @@ use super::{Args, Array, Func, Str, Value, Vm}; use crate::diag::{SourceResult, StrResult}; use crate::syntax::is_ident; use crate::syntax::Spanned; -use crate::util::ArcExt; +use crate::util::{format_eco, ArcExt, EcoString}; /// Create a new [`Dict`] from key-value pairs. #[macro_export] @@ -122,8 +122,8 @@ impl Dict { /// The missing key access error message. #[cold] -fn missing_key(key: &str) -> String { - format!("dictionary does not contain key {:?}", Str::from(key)) +fn missing_key(key: &str) -> EcoString { + format_eco!("dictionary does not contain key {:?}", Str::from(key)) } impl Debug for Dict { diff --git a/src/model/ops.rs b/src/model/ops.rs index 9a731d65..60b1c449 100644 --- a/src/model/ops.rs +++ b/src/model/ops.rs @@ -3,13 +3,14 @@ use super::{Regex, Smart, Value}; use crate::diag::StrResult; use crate::geom::{Axes, Axis, GenAlign, Length, Numeric, PartialStroke, Rel}; +use crate::util::format_eco; use std::cmp::Ordering; use Value::*; /// Bail with a type mismatch error. macro_rules! mismatch { ($fmt:expr, $($value:expr),* $(,)?) => { - return Err(format!($fmt, $($value.type_name()),*)) + return Err(format_eco!($fmt, $($value.type_name()),*)) }; } @@ -104,7 +105,7 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> { (a.downcast::<GenAlign>(), b.downcast::<GenAlign>()) { if a.axis() == b.axis() { - return Err(format!("cannot add two {:?} alignments", a.axis())); + return Err(format_eco!("cannot add two {:?} alignments", a.axis())); } return Ok(Value::dynamic(match a.axis() { diff --git a/src/model/str.rs b/src/model/str.rs index 1fcf7075..0c288d9b 100644 --- a/src/model/str.rs +++ b/src/model/str.rs @@ -8,7 +8,7 @@ use unicode_segmentation::UnicodeSegmentation; use super::{castable, dict, Array, Dict, Value}; use crate::diag::StrResult; use crate::geom::GenAlign; -use crate::util::EcoString; +use crate::util::{format_eco, EcoString}; /// Create a new [`Str`] from a format string. #[macro_export] @@ -401,7 +401,7 @@ pub struct Regex(regex::Regex); impl Regex { /// Create a new regular expression. pub fn new(re: &str) -> StrResult<Self> { - regex::Regex::new(re).map(Self).map_err(|err| err.to_string()) + regex::Regex::new(re).map(Self).map_err(|err| format_eco!("{err}")) } } diff --git a/src/model/value.rs b/src/model/value.rs index 043fde34..59dac720 100644 --- a/src/model/value.rs +++ b/src/model/value.rs @@ -344,7 +344,7 @@ macro_rules! primitive { match value { Value::$variant(v) => Ok(v), $(Value::$other$(($binding))? => Ok($out),)* - v => Err(format!( + v => Err(format_eco!( "expected {}, found {}", Self::TYPE_NAME, v.type_name(), |
