diff --git a/library/src/compute/data.rs b/library/src/compute/data.rs index 8dba62f54..fc81435ca 100644 --- a/library/src/compute/data.rs +++ b/library/src/compute/data.rs @@ -113,11 +113,11 @@ impl Default for Delimiter { } /// Format the user-facing CSV error message. -fn format_csv_error(error: csv::Error, line: usize) -> String { +fn format_csv_error(error: csv::Error, line: usize) -> EcoString { match error.kind() { csv::ErrorKind::Utf8 { .. } => "file is not valid utf-8".into(), csv::ErrorKind::UnequalLengths { expected_len, len, .. } => { - format!( + eco_format!( "failed to parse csv file: found {len} instead of {expected_len} fields in line {line}" ) } @@ -202,9 +202,9 @@ fn convert_json(value: serde_json::Value) -> Value { /// Format the user-facing JSON error message. #[track_caller] -fn format_json_error(error: serde_json::Error) -> String { +fn format_json_error(error: serde_json::Error) -> EcoString { assert!(error.is_syntax() || error.is_eof()); - format!("failed to parse json file: syntax error in line {}", error.line()) + eco_format!("failed to parse json file: syntax error in line {}", error.line()) } /// Read structured data from a YAML file. @@ -293,8 +293,8 @@ fn convert_yaml_key(key: serde_yaml::Value) -> Option { /// Format the user-facing YAML error message. #[track_caller] -fn format_yaml_error(error: serde_yaml::Error) -> String { - format!("failed to parse yaml file: {}", error.to_string().trim()) +fn format_yaml_error(error: serde_yaml::Error) -> EcoString { + eco_format!("failed to parse yaml file: {}", error.to_string().trim()) } /// Read structured data from an XML file. @@ -388,6 +388,6 @@ fn convert_xml(node: roxmltree::Node) -> Value { } /// Format the user-facing XML error message. -fn format_xml_error(error: roxmltree::Error) -> String { +fn format_xml_error(error: roxmltree::Error) -> EcoString { format_xml_like_error("xml file", error) } diff --git a/src/diag.rs b/src/diag.rs index c1a7c8346..88141062a 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -249,31 +249,31 @@ impl From for EcoString { } /// Format a user-facing error message for an XML-like file format. -pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> String { +pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> EcoString { match error { roxmltree::Error::UnexpectedCloseTag { expected, actual, pos } => { - format!( + eco_format!( "failed to parse {format}: found closing tag '{actual}' \ instead of '{expected}' in line {}", pos.row ) } roxmltree::Error::UnknownEntityReference(entity, pos) => { - format!( + eco_format!( "failed to parse {format}: unknown entity '{entity}' in line {}", pos.row ) } roxmltree::Error::DuplicatedAttribute(attr, pos) => { - format!( + eco_format!( "failed to parse {format}: duplicate attribute '{attr}' in line {}", pos.row ) } roxmltree::Error::NoRootNode => { - format!("failed to parse {format}: missing root node") + eco_format!("failed to parse {format}: missing root node") } roxmltree::Error::SizeLimit => "file is too large".into(), - _ => format!("failed to parse {format}"), + _ => eco_format!("failed to parse {format}"), } } diff --git a/src/image.rs b/src/image.rs index 44b00adb1..23ea60f58 100644 --- a/src/image.rs +++ b/src/image.rs @@ -3,6 +3,8 @@ use std::io; use std::sync::Arc; +use ecow::EcoString; + use crate::diag::{format_xml_like_error, StrResult}; use crate::util::Buffer; @@ -152,7 +154,7 @@ fn determine_size(data: &Buffer, format: ImageFormat) -> StrResult<(u32, u32)> { } /// Format the user-facing raster graphic decoding error message. -fn format_image_error(error: image::ImageError) -> String { +fn format_image_error(error: image::ImageError) -> EcoString { match error { image::ImageError::Limits(_) => "file is too large".into(), _ => "failed to decode image".into(), @@ -160,7 +162,7 @@ fn format_image_error(error: image::ImageError) -> String { } /// Format the user-facing SVG decoding error message. -fn format_usvg_error(error: usvg::Error) -> String { +fn format_usvg_error(error: usvg::Error) -> EcoString { match error { usvg::Error::NotAnUtf8Str => "file is not valid utf-8".into(), usvg::Error::MalformedGZip => "file is not compressed correctly".into(),