make Error enum variants clearer

This commit is contained in:
2024-12-31 08:42:28 +00:00
parent e3f6613c6d
commit 84ada5576f
5 changed files with 14 additions and 14 deletions
+4 -4
View File
@@ -3,13 +3,13 @@ use thiserror::Error;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
#[error("failed to obtain handle")] #[error("failed to obtain handle")]
HandleError(String), ObtainHandleError(String),
#[error("failed to access process memory")] #[error("failed to access process memory")]
MemoryError(String), AccessMemoryError(String),
#[error("failed to create process")] #[error("failed to create process")]
ProcessError(String), CreateProcessError(String),
#[error("failed to create snapshot")] #[error("failed to create snapshot")]
SnapshotError(String), CreateSnapshotError(String),
#[error("failed to convert integer")] #[error("failed to convert integer")]
ConvertIntegerError(#[from] std::num::TryFromIntError), ConvertIntegerError(#[from] std::num::TryFromIntError),
} }
+2 -2
View File
@@ -35,13 +35,13 @@ impl Handle {
OpenProcess(PROCESS_ALL_ACCESS, false, pid) OpenProcess(PROCESS_ALL_ACCESS, false, pid)
.or_else(|_| OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid)) .or_else(|_| OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid))
.map_err(|why| { .map_err(|why| {
crate::Error::HandleError(format!( crate::Error::ObtainHandleError(format!(
"failed to open process with needed access: {why}", "failed to open process with needed access: {why}",
)) ))
})? })?
}; };
if handle == INVALID_HANDLE_VALUE { if handle == INVALID_HANDLE_VALUE {
return Err(crate::Error::HandleError( return Err(crate::Error::ObtainHandleError(
"failed to get a valid handle".to_string(), "failed to get a valid handle".to_string(),
)); ));
} }
+2 -2
View File
@@ -14,7 +14,7 @@ pub fn read<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), cra
None, None,
) { ) {
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(why) => Err(crate::Error::MemoryError(format!( Err(why) => Err(crate::Error::AccessMemoryError(format!(
"failed to read memory: {why}" "failed to read memory: {why}"
))), ))),
} }
@@ -31,7 +31,7 @@ pub fn write<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), cr
None, None,
) { ) {
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(why) => Err(crate::Error::MemoryError(format!( Err(why) => Err(crate::Error::AccessMemoryError(format!(
"failed to write memory: {why}" "failed to write memory: {why}"
))), ))),
} }
+2 -2
View File
@@ -42,7 +42,7 @@ impl Process {
.into_iter() .into_iter()
.find(|snapshot| snapshot.name == *name), .find(|snapshot| snapshot.name == *name),
}) else { }) else {
return Err(crate::Error::ProcessError(format!( return Err(crate::Error::CreateProcessError(format!(
"failed to find a process with identifier `{identifier}`", "failed to find a process with identifier `{identifier}`",
))); )));
}; };
@@ -61,7 +61,7 @@ impl Process {
.into_iter() .into_iter()
.find(|snapshot| snapshot.name == name) .find(|snapshot| snapshot.name == name)
else { else {
return Err(crate::Error::ProcessError(format!( return Err(crate::Error::CreateProcessError(format!(
"failed to find a module with identifier `{name}`", "failed to find a module with identifier `{name}`",
))); )));
}; };
+4 -4
View File
@@ -17,7 +17,7 @@ impl ProcessSnapshot {
match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) { match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) {
Ok(snapshot) => snapshot, Ok(snapshot) => snapshot,
Err(why) => { Err(why) => {
return Err(crate::Error::SnapshotError(format!( return Err(crate::Error::CreateSnapshotError(format!(
"failed to create process snapshot: {why}" "failed to create process snapshot: {why}"
))) )))
} }
@@ -31,7 +31,7 @@ impl ProcessSnapshot {
match Process32FirstW(snapshot, &mut process_entry_32_w) { match Process32FirstW(snapshot, &mut process_entry_32_w) {
Ok(()) => {} Ok(()) => {}
Err(why) => { Err(why) => {
return Err(crate::Error::SnapshotError(format!( return Err(crate::Error::CreateSnapshotError(format!(
"failed to get first process from snapshot: {why}" "failed to get first process from snapshot: {why}"
))) )))
} }
@@ -71,7 +71,7 @@ impl ModuleSnapshot {
match CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid) { match CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid) {
Ok(snapshot) => snapshot, Ok(snapshot) => snapshot,
Err(why) => { Err(why) => {
return Err(crate::Error::SnapshotError(format!( return Err(crate::Error::CreateSnapshotError(format!(
"failed to create module snapshot: {why}" "failed to create module snapshot: {why}"
))) )))
} }
@@ -85,7 +85,7 @@ impl ModuleSnapshot {
match Module32FirstW(snapshot, &mut module_entry_32_w) { match Module32FirstW(snapshot, &mut module_entry_32_w) {
Ok(()) => {} Ok(()) => {}
Err(why) => { Err(why) => {
return Err(crate::Error::SnapshotError(format!( return Err(crate::Error::CreateSnapshotError(format!(
"failed to get first module from snapshot: {why}" "failed to get first module from snapshot: {why}"
))) )))
} }