diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-09-25 18:20:39 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-09-25 18:20:39 +0200 |
| commit | f6adc45638409aaa0feb1f70883c11ed553efe4f (patch) | |
| tree | 3691cec70698a1952f2b315ed8b602912739acce /src/diag.rs | |
| parent | fffb55f79a3369fa2dcf39371091c48ff61f55a8 (diff) | |
XML reading
Diffstat (limited to 'src/diag.rs')
| -rw-r--r-- | src/diag.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/diag.rs b/src/diag.rs index 81bb7e51..0d016a21 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -3,6 +3,7 @@ use std::fmt::{self, Display, Formatter}; use std::io; use std::path::{Path, PathBuf}; +use std::str::Utf8Error; use std::string::FromUtf8Error; use comemo::Tracked; @@ -191,6 +192,12 @@ impl Display for FileError { } } +impl From<Utf8Error> for FileError { + fn from(_: Utf8Error) -> Self { + Self::InvalidUtf8 + } +} + impl From<FromUtf8Error> for FileError { fn from(_: FromUtf8Error) -> Self { Self::InvalidUtf8 @@ -202,3 +209,33 @@ impl From<FileError> for String { error.to_string() } } + +/// Format a user-facing error message for an XML-like file format. +pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> String { + match error { + roxmltree::Error::UnexpectedCloseTag { expected, actual, pos } => { + format!( + "failed to parse {format}: found closing tag '{actual}' \ + instead of '{expected}' in line {}", + pos.row + ) + } + roxmltree::Error::UnknownEntityReference(entity, pos) => { + format!( + "failed to parse {format}: unknown entity '{entity}' in line {}", + pos.row + ) + } + roxmltree::Error::DuplicatedAttribute(attr, pos) => { + format!( + "failed to parse {format}: duplicate attribute '{attr}' in line {}", + pos.row + ) + } + roxmltree::Error::NoRootNode => { + format!("failed to parse {format}: missing root node") + } + roxmltree::Error::SizeLimit => "file is too large".into(), + _ => format!("failed to parse {format}"), + } +} |
