diff --git a/cli/src/main.rs b/cli/src/main.rs index 5463c3ecf..cdbd59263 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -641,8 +641,7 @@ impl FontSearcher { } } - /// Search for all fonts in a directory. - /// recursively. + /// Search for all fonts in a directory recursively. fn search_dir(&mut self, path: impl AsRef) { for entry in WalkDir::new(path) .follow_links(true) diff --git a/library/src/layout/page.rs b/library/src/layout/page.rs index 9d5d4f561..42db02c39 100644 --- a/library/src/layout/page.rs +++ b/library/src/layout/page.rs @@ -196,7 +196,7 @@ impl Cast> for Marginal { Value::Str(v) => Ok(Self::Content(TextNode::packed(v))), Value::Content(v) => Ok(Self::Content(v)), Value::Func(v) => Ok(Self::Func(v, value.span)), - v => Err(format!( + v => Err(format_eco!( "expected none, content or function, found {}", v.type_name(), )), diff --git a/library/src/structure/list.rs b/library/src/structure/list.rs index 462e0c32e..6bfddd2e8 100644 --- a/library/src/structure/list.rs +++ b/library/src/structure/list.rs @@ -263,7 +263,7 @@ impl Cast> for Label { Value::Str(v) => Ok(Self::Pattern(v.parse()?)), Value::Content(v) => Ok(Self::Content(v)), Value::Func(v) => Ok(Self::Func(v, value.span)), - v => Err(format!( + v => Err(format_eco!( "expected string, content or function, found {}", v.type_name(), )), diff --git a/src/diag.rs b/src/diag.rs index 58eebf8bc..29e43b918 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -154,13 +154,13 @@ impl Trace for SourceResult { } /// A result type with a string error message. -pub type StrResult = Result; +pub type StrResult = Result; /// Transform `expected X, found Y` into `expected X or A, found Y`. -pub fn with_alternative(msg: String, alt: &str) -> String { +pub fn with_alternative(msg: EcoString, alt: &str) -> EcoString { let mut parts = msg.split(", found "); if let (Some(a), Some(b)) = (parts.next(), parts.next()) { - format!("{} or {}, found {}", a, alt, b) + format_eco!("{} or {}, found {}", a, alt, b) } else { msg } diff --git a/src/export/pdf/mod.rs b/src/export/pdf/mod.rs index 7a530f041..7e5a3c064 100644 --- a/src/export/pdf/mod.rs +++ b/src/export/pdf/mod.rs @@ -21,10 +21,6 @@ use crate::image::Image; /// Export a document into a PDF file. /// -/// This creates one page per frame. In addition to the frames, you need to pass -/// in the context used during compilation so that fonts and images can be -/// included in the PDF. -/// /// Returns the raw bytes making up the PDF file. pub fn pdf(document: &Document) -> Vec { let mut ctx = PdfContext::new(&document.metadata); diff --git a/src/export/render.rs b/src/export/render.rs index 14654b9b8..a61ff1be3 100644 --- a/src/export/render.rs +++ b/src/export/render.rs @@ -16,11 +16,8 @@ use crate::image::{DecodedImage, Image}; /// Export a frame into a raster image. /// -/// This renders the frame at the given number of pixels per printer's point and -/// returns the resulting `tiny-skia` pixel buffer. -/// -/// In addition to the frame, you need to pass in the context used during -/// compilation so that fonts and images can be rendered. +/// This renders the frame at the given number of pixels per point and returns +/// the resulting `tiny-skia` pixel buffer. pub fn render(frame: &Frame, pixel_per_pt: f32) -> sk::Pixmap { let size = frame.size(); let pxw = (pixel_per_pt * size.x.to_f32()).round().max(1.0) as u32; diff --git a/src/model/array.rs b/src/model/array.rs index 5a2f86729..06d1b5885 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 a4a3fe4ec..df3c8c81d 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: 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 1c2997958..d54a0e824 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 9a731d655..60b1c4498 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 { (a.downcast::(), b.downcast::()) { 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 1fcf7075d..0c288d9b7 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 { - 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 043fde343..59dac7201 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(),