mirror of
https://github.com/loot/loot-condition-interpreter.git
synced 2026-07-27 14:16:09 -07:00
Improve content of I/O and PE parsing errors
This commit is contained in:
@@ -59,7 +59,8 @@ fn evaluate_file_regex(state: &State, parent_path: &Path, regex: &Regex) -> Resu
|
|||||||
};
|
};
|
||||||
|
|
||||||
for entry in dir_iterator {
|
for entry in dir_iterator {
|
||||||
if is_match(regex, &entry?.file_name()) {
|
let entry = entry.map_err(|e| Error::IoError(parent_path.to_path_buf(), e))?;
|
||||||
|
if is_match(regex, &entry.file_name()) {
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,7 +76,8 @@ fn evaluate_many(state: &State, parent_path: &Path, regex: &Regex) -> Result<boo
|
|||||||
|
|
||||||
let mut found_one = false;
|
let mut found_one = false;
|
||||||
for entry in dir_iterator {
|
for entry in dir_iterator {
|
||||||
if is_match(regex, &entry?.file_name()) {
|
let entry = entry.map_err(|e| Error::IoError(parent_path.to_path_buf(), e))?;
|
||||||
|
if is_match(regex, &entry.file_name()) {
|
||||||
if found_one {
|
if found_one {
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
} else {
|
} else {
|
||||||
@@ -132,11 +134,13 @@ fn evaluate_checksum(state: &State, file_path: &Path, crc: u32) -> Result<bool,
|
|||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
let reader = BufReader::new(File::open(path)?);
|
let file = File::open(path).map_err(|e| Error::IoError(file_path.to_path_buf(), e))?;
|
||||||
|
let reader = BufReader::new(file);
|
||||||
let mut digest = crc32::Digest::new(crc32::IEEE);
|
let mut digest = crc32::Digest::new(crc32::IEEE);
|
||||||
|
|
||||||
for byte in reader.bytes() {
|
for byte in reader.bytes() {
|
||||||
digest.write_u8(byte?);
|
let byte = byte.map_err(|e| Error::IoError(file_path.to_path_buf(), e))?;
|
||||||
|
digest.write_u8(byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
let calculated_crc = digest.sum32();
|
let calculated_crc = digest.sum32();
|
||||||
|
|||||||
+13
-12
@@ -28,8 +28,8 @@ use function::Function;
|
|||||||
pub enum Error {
|
pub enum Error {
|
||||||
ParsingIncomplete,
|
ParsingIncomplete,
|
||||||
ParsingError,
|
ParsingError,
|
||||||
PeParsingError,
|
PeParsingError(PathBuf, Box<error::Error>),
|
||||||
IoError(io::Error),
|
IoError(PathBuf, io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I> From<Err<I>> for Error {
|
impl<I> From<Err<I>> for Error {
|
||||||
@@ -41,12 +41,6 @@ impl<I> From<Err<I>> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<io::Error> for Error {
|
|
||||||
fn from(error: io::Error) -> Self {
|
|
||||||
Error::IoError(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
@@ -54,11 +48,18 @@ impl fmt::Display for Error {
|
|||||||
Error::ParsingError => {
|
Error::ParsingError => {
|
||||||
write!(f, "An error was encountered while parsing the expression")
|
write!(f, "An error was encountered while parsing the expression")
|
||||||
}
|
}
|
||||||
Error::PeParsingError => write!(
|
Error::PeParsingError(p, e) => write!(
|
||||||
f,
|
f,
|
||||||
"An error was encountered while reading the version of an executable"
|
"An error was encountered while reading the file version field of \"{}\": {}",
|
||||||
|
p.display(),
|
||||||
|
e
|
||||||
|
),
|
||||||
|
Error::IoError(p, e) => write!(
|
||||||
|
f,
|
||||||
|
"An error was encountered while accessing the path \"{}\": {}",
|
||||||
|
p.display(),
|
||||||
|
e
|
||||||
),
|
),
|
||||||
Error::IoError(e) => e.fmt(f),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,7 +67,7 @@ impl fmt::Display for Error {
|
|||||||
impl error::Error for Error {
|
impl error::Error for Error {
|
||||||
fn cause(&self) -> Option<&error::Error> {
|
fn cause(&self) -> Option<&error::Error> {
|
||||||
match self {
|
match self {
|
||||||
Error::IoError(e) => Some(e),
|
Error::IoError(_, e) => Some(e),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-3
@@ -29,9 +29,10 @@ pub struct Version {
|
|||||||
|
|
||||||
impl Version {
|
impl Version {
|
||||||
pub fn read_file_version(file_path: &Path) -> Result<Self, Error> {
|
pub fn read_file_version(file_path: &Path) -> Result<Self, Error> {
|
||||||
let file_map = FileMap::open(file_path)?;
|
let file_map =
|
||||||
let version_info =
|
FileMap::open(file_path).map_err(|e| Error::IoError(file_path.to_path_buf(), e))?;
|
||||||
get_pe_version_info(file_map.as_ref()).map_err(|_| Error::PeParsingError)?;
|
let version_info = get_pe_version_info(file_map.as_ref())
|
||||||
|
.map_err(|e| Error::PeParsingError(file_path.to_path_buf(), e.into()))?;
|
||||||
|
|
||||||
if let Some(fixed_file_info) = version_info.fixed() {
|
if let Some(fixed_file_info) = version_info.fixed() {
|
||||||
let version = format!(
|
let version = format!(
|
||||||
@@ -181,6 +182,24 @@ mod tests {
|
|||||||
assert!(version.pre_release_ids.is_empty());
|
assert!(version.pre_release_ids.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn version_read_file_version_should_error_with_path_if_path_does_not_exist() {
|
||||||
|
let error = Version::read_file_version(Path::new("missing")).unwrap_err();
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
error
|
||||||
|
.to_string()
|
||||||
|
.starts_with("An error was encountered while accessing the path \"missing\":")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn version_read_file_version_should_error_with_path_if_the_file_is_not_an_executable() {
|
||||||
|
let error = Version::read_file_version(Path::new("Cargo.toml")).unwrap_err();
|
||||||
|
|
||||||
|
assert_eq!("An error was encountered while reading the file version field of \"Cargo.toml\": bad magic", error.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn version_eq_an_empty_string_should_equal_an_empty_string() {
|
fn version_eq_an_empty_string_should_equal_an_empty_string() {
|
||||||
assert_eq!(Version::from(""), Version::from(""));
|
assert_eq!(Version::from(""), Version::from(""));
|
||||||
|
|||||||
Reference in New Issue
Block a user