Remove intermediate strings in error message

This commit is contained in:
Laurenz 2023-04-18 14:31:09 +02:00
parent 4613739748
commit bce83d330f
3 changed files with 17 additions and 15 deletions

View File

@ -113,11 +113,11 @@ impl Default for Delimiter {
} }
/// Format the user-facing CSV error message. /// 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() { match error.kind() {
csv::ErrorKind::Utf8 { .. } => "file is not valid utf-8".into(), csv::ErrorKind::Utf8 { .. } => "file is not valid utf-8".into(),
csv::ErrorKind::UnequalLengths { expected_len, len, .. } => { csv::ErrorKind::UnequalLengths { expected_len, len, .. } => {
format!( eco_format!(
"failed to parse csv file: found {len} instead of {expected_len} fields in line {line}" "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. /// Format the user-facing JSON error message.
#[track_caller] #[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()); 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. /// Read structured data from a YAML file.
@ -293,8 +293,8 @@ fn convert_yaml_key(key: serde_yaml::Value) -> Option<Str> {
/// Format the user-facing YAML error message. /// Format the user-facing YAML error message.
#[track_caller] #[track_caller]
fn format_yaml_error(error: serde_yaml::Error) -> String { fn format_yaml_error(error: serde_yaml::Error) -> EcoString {
format!("failed to parse yaml file: {}", error.to_string().trim()) eco_format!("failed to parse yaml file: {}", error.to_string().trim())
} }
/// Read structured data from an XML file. /// 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. /// 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) format_xml_like_error("xml file", error)
} }

View File

@ -249,31 +249,31 @@ impl From<FileError> for EcoString {
} }
/// Format a user-facing error message for an XML-like file format. /// 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 { match error {
roxmltree::Error::UnexpectedCloseTag { expected, actual, pos } => { roxmltree::Error::UnexpectedCloseTag { expected, actual, pos } => {
format!( eco_format!(
"failed to parse {format}: found closing tag '{actual}' \ "failed to parse {format}: found closing tag '{actual}' \
instead of '{expected}' in line {}", instead of '{expected}' in line {}",
pos.row pos.row
) )
} }
roxmltree::Error::UnknownEntityReference(entity, pos) => { roxmltree::Error::UnknownEntityReference(entity, pos) => {
format!( eco_format!(
"failed to parse {format}: unknown entity '{entity}' in line {}", "failed to parse {format}: unknown entity '{entity}' in line {}",
pos.row pos.row
) )
} }
roxmltree::Error::DuplicatedAttribute(attr, pos) => { roxmltree::Error::DuplicatedAttribute(attr, pos) => {
format!( eco_format!(
"failed to parse {format}: duplicate attribute '{attr}' in line {}", "failed to parse {format}: duplicate attribute '{attr}' in line {}",
pos.row pos.row
) )
} }
roxmltree::Error::NoRootNode => { 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(), roxmltree::Error::SizeLimit => "file is too large".into(),
_ => format!("failed to parse {format}"), _ => eco_format!("failed to parse {format}"),
} }
} }

View File

@ -3,6 +3,8 @@
use std::io; use std::io;
use std::sync::Arc; use std::sync::Arc;
use ecow::EcoString;
use crate::diag::{format_xml_like_error, StrResult}; use crate::diag::{format_xml_like_error, StrResult};
use crate::util::Buffer; 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. /// 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 { match error {
image::ImageError::Limits(_) => "file is too large".into(), image::ImageError::Limits(_) => "file is too large".into(),
_ => "failed to decode image".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. /// 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 { match error {
usvg::Error::NotAnUtf8Str => "file is not valid utf-8".into(), usvg::Error::NotAnUtf8Str => "file is not valid utf-8".into(),
usvg::Error::MalformedGZip => "file is not compressed correctly".into(), usvg::Error::MalformedGZip => "file is not compressed correctly".into(),