From 7edd0e3648731b368203607e2ee00941b083c2e9 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 25 Nov 2023 20:02:56 +0000 Subject: [PATCH] Add context to Error::DecodeError Unfortunately esplugin doesn't expose similar context, so decode errors it reports are handled as generic plugin parsing errors. A future esplugin update will provide that context, at which point libloadorder can be updated to use it. --- src/enums.rs | 5 ++--- src/load_order/mutable.rs | 2 +- src/plugin.rs | 4 +--- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/enums.rs b/src/enums.rs index 92f42e6..f7f6b6b 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -17,7 +17,6 @@ * along with libloadorder. If not, see . */ -use std::borrow::Cow; use std::convert::From; use std::error; use std::ffi::OsString; @@ -81,7 +80,7 @@ pub enum Error { NoFilename(PathBuf), SystemTimeError(time::SystemTimeError), NotUtf8(Vec), - DecodeError(Cow<'static, str>), + DecodeError(Vec), EncodeError(String), PluginParsingError(PathBuf), PluginNotFound(String), @@ -147,7 +146,7 @@ impl fmt::Display for Error { write!(f, "The plugin path {path:?} has no filename part"), Error::SystemTimeError(error) => error.fmt(f), Error::NotUtf8(bytes) => write!(f, "Expected a UTF-8 string, got bytes {bytes:02X?}"), - Error::DecodeError(_) => write!(f, "Text could not be decoded from Windows-1252"), + Error::DecodeError(bytes) => write!(f, "String could not be decoded from Windows-1252, bytes are {bytes:02X?}"), Error::EncodeError(string) => write!(f, "The string \"{string}\" could not be encoded to Windows-1252"), Error::PluginParsingError(path) => { write!(f, "An error was encountered while parsing the plugin at {path:?}") diff --git a/src/load_order/mutable.rs b/src/load_order/mutable.rs index d9d5c7e..47142bc 100644 --- a/src/load_order/mutable.rs +++ b/src/load_order/mutable.rs @@ -188,7 +188,7 @@ where // they get mapped to C1 control characters. let decoded_content = WINDOWS_1252 .decode_without_bom_handling_and_without_replacement(&content) - .ok_or_else(|| Error::DecodeError("invalid sequence".into()))?; + .ok_or_else(|| Error::DecodeError(content.clone()))?; Ok(decoded_content.lines().filter_map(line_mapper).collect()) } diff --git a/src/plugin.rs b/src/plugin.rs index 4b98473..df7f4b0 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -208,9 +208,7 @@ fn file_error(file_path: &Path, error: esplugin::Error) -> Error { esplugin::Error::ParsingIncomplete | esplugin::Error::ParsingError(_, _) => { Error::PluginParsingError(file_path.to_path_buf()) } - esplugin::Error::DecodeError => { - Error::DecodeError("invalid byte sequence in plugin string".into()) - } + esplugin::Error::DecodeError => Error::PluginParsingError(file_path.to_path_buf()), } }