Use anyhow for Result type

This commit is contained in:
2025-12-12 03:40:33 -06:00
parent 44e5997847
commit 60f1868334
5 changed files with 24 additions and 25 deletions
Generated
+7
View File
@@ -63,6 +63,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04" checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04"
[[package]]
name = "anyhow"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]] [[package]]
name = "arboard" name = "arboard"
version = "3.6.1" version = "3.6.1"
@@ -820,6 +826,7 @@ dependencies = [
name = "gta-tools" name = "gta-tools"
version = "0.11.0" version = "0.11.0"
dependencies = [ dependencies = [
"anyhow",
"catppuccin-egui", "catppuccin-egui",
"eframe", "eframe",
"egui_extras", "egui_extras",
+1
View File
@@ -7,6 +7,7 @@ authors = ["futile <git@futile.eu>"]
description = "A toolset of convenient things for GTA V Online." description = "A toolset of convenient things for GTA V Online."
[dependencies] [dependencies]
anyhow = "1.0.100"
catppuccin-egui = { version = "5.6.0", default-features = false, features = [ catppuccin-egui = { version = "5.6.0", default-features = false, features = [
"egui32", "egui32",
] } ] }
+5 -7
View File
@@ -3,10 +3,8 @@ use crate::util::{
firewall::{Firewall, RuleDirection, RuleMode, RuleProtocol}, firewall::{Firewall, RuleDirection, RuleMode, RuleProtocol},
system_info::SystemInfo, system_info::SystemInfo,
}; };
use std::{ use anyhow::Result;
error::Error, use std::time::{Duration, Instant};
time::{Duration, Instant},
};
const FILTER_NAME_EMPTY_SESSION_IN: &str = "[GTA Tools] Block inbound UDP traffic for all of GTA V"; const FILTER_NAME_EMPTY_SESSION_IN: &str = "[GTA Tools] Block inbound UDP traffic for all of GTA V";
const FILTER_NAME_EMPTY_SESSION_OUT: &str = const FILTER_NAME_EMPTY_SESSION_OUT: &str =
@@ -32,7 +30,7 @@ impl Default for EmptySession {
} }
impl EmptySession { impl EmptySession {
pub fn run_timers(&mut self, firewall: &Firewall) -> Result<(), Box<dyn Error>> { pub fn run_timers(&mut self, firewall: &Firewall) -> Result<()> {
if self.disabled { if self.disabled {
self.countdown.count(); self.countdown.count();
} else { } else {
@@ -46,7 +44,7 @@ impl EmptySession {
} }
} }
pub fn activate(system_info: &mut SystemInfo, firewall: &Firewall) -> Result<bool, Box<dyn Error>> { pub fn activate(system_info: &mut SystemInfo, firewall: &Firewall) -> Result<bool> {
let Some(exe_path) = system_info.get_game_exe_path() else { let Some(exe_path) = system_info.get_game_exe_path() else {
log::info!("wasn't able to find game exe"); log::info!("wasn't able to find game exe");
return Ok(false); return Ok(false);
@@ -66,7 +64,7 @@ pub fn activate(system_info: &mut SystemInfo, firewall: &Firewall) -> Result<boo
Ok(true) Ok(true)
} }
pub fn deactivate(firewall: &Firewall) -> Result<(), Box<dyn Error>> { pub fn deactivate(firewall: &Firewall) -> Result<()> {
firewall.remove(FILTER_NAME_EMPTY_SESSION_IN)?; firewall.remove(FILTER_NAME_EMPTY_SESSION_IN)?;
firewall.remove(FILTER_NAME_EMPTY_SESSION_OUT)?; firewall.remove(FILTER_NAME_EMPTY_SESSION_OUT)?;
Ok(()) Ok(())
+6 -14
View File
@@ -5,7 +5,7 @@ use crate::{
system_info::SystemInfo, system_info::SystemInfo,
}, },
}; };
use std::error::Error; use anyhow::Result;
use strum::{Display, EnumIter}; use strum::{Display, EnumIter};
const FILTER_NAME_EXE: &str = "[GTA Tools] Block outbound traffic for all of GTA V"; const FILTER_NAME_EXE: &str = "[GTA Tools] Block outbound traffic for all of GTA V";
@@ -40,11 +40,7 @@ impl Default for GameNetworking {
} }
impl GameNetworking { impl GameNetworking {
pub fn block_exe( pub fn block_exe(&mut self, system_info: &mut SystemInfo, firewall: &Firewall) -> Result<()> {
&mut self,
system_info: &mut SystemInfo,
firewall: &Firewall,
) -> Result<(), Box<dyn Error>> {
let Some(exe_path) = system_info.get_game_exe_path() else { let Some(exe_path) = system_info.get_game_exe_path() else {
log::warn!("Unable to find game executable path."); log::warn!("Unable to find game executable path.");
return Ok(()); return Ok(());
@@ -59,17 +55,13 @@ impl GameNetworking {
.inspect(|_| self.blocked = BlockedStatus::ExeBlocked) .inspect(|_| self.blocked = BlockedStatus::ExeBlocked)
} }
pub fn unblock_exe(&mut self, firewall: &Firewall) -> Result<(), Box<dyn Error>> { pub fn unblock_exe(&mut self, firewall: &Firewall) -> Result<()> {
firewall firewall
.remove(FILTER_NAME_EXE) .remove(FILTER_NAME_EXE)
.inspect(|_| self.blocked = BlockedStatus::NotBlocked) .inspect(|_| self.blocked = BlockedStatus::NotBlocked)
} }
pub fn block_save_server( pub fn block_save_server(&mut self, save_server_ip: &str, firewall: &Firewall) -> Result<()> {
&mut self,
save_server_ip: &str,
firewall: &Firewall,
) -> Result<(), Box<dyn Error>> {
firewall firewall
.add( .add(
FILTER_NAME_SAVE_SERVER, FILTER_NAME_SAVE_SERVER,
@@ -80,7 +72,7 @@ impl GameNetworking {
.inspect(|_| self.blocked = BlockedStatus::ServerBlocked) .inspect(|_| self.blocked = BlockedStatus::ServerBlocked)
} }
pub fn unblock_save_server(&mut self, firewall: &Firewall) -> Result<(), Box<dyn Error>> { pub fn unblock_save_server(&mut self, firewall: &Firewall) -> Result<()> {
firewall firewall
.remove(FILTER_NAME_SAVE_SERVER) .remove(FILTER_NAME_SAVE_SERVER)
.inspect(|_| self.blocked = BlockedStatus::NotBlocked) .inspect(|_| self.blocked = BlockedStatus::NotBlocked)
@@ -90,7 +82,7 @@ impl GameNetworking {
&mut self, &mut self,
block_method: BlockMethod, block_method: BlockMethod,
firewall: &Firewall, firewall: &Firewall,
) -> Result<(), Box<dyn Error>> { ) -> Result<()> {
match block_method { match block_method {
BlockMethod::EntireGame => { BlockMethod::EntireGame => {
if self.blocked == BlockedStatus::ServerBlocked { if self.blocked == BlockedStatus::ServerBlocked {
+5 -4
View File
@@ -1,4 +1,5 @@
use std::{error::Error, path::PathBuf}; use anyhow::Result;
use std::path::PathBuf;
use windows::{ use windows::{
Win32::{ Win32::{
NetworkManagement::WindowsFirewall::{ NetworkManagement::WindowsFirewall::{
@@ -31,7 +32,7 @@ impl Firewall {
mode: RuleMode, mode: RuleMode,
direction: RuleDirection, direction: RuleDirection,
protocol: RuleProtocol, protocol: RuleProtocol,
) -> Result<(), Box<dyn Error>> { ) -> Result<()> {
let add_rule = || { let add_rule = || {
let rules = unsafe { self.policy.Rules() }?; let rules = unsafe { self.policy.Rules() }?;
unsafe { rules.Remove(&BSTR::from(name)) }?; unsafe { rules.Remove(&BSTR::from(name)) }?;
@@ -61,7 +62,7 @@ impl Firewall {
add_rule().inspect_err(|e| log::warn!("Failed to add rule '{name}': {e}")) add_rule().inspect_err(|e| log::warn!("Failed to add rule '{name}': {e}"))
} }
pub fn remove(&self, name: &str) -> Result<(), Box<dyn Error>> { pub fn remove(&self, name: &str) -> Result<()> {
let remove_rule = || { let remove_rule = || {
let rules = unsafe { self.policy.Rules() }?; let rules = unsafe { self.policy.Rules() }?;
unsafe { rules.Remove(&BSTR::from(name)) }?; unsafe { rules.Remove(&BSTR::from(name)) }?;
@@ -70,7 +71,7 @@ impl Firewall {
remove_rule().inspect_err(|e| log::warn!("Failed to remove rule '{name}': {e}")) remove_rule().inspect_err(|e| log::warn!("Failed to remove rule '{name}': {e}"))
} }
pub fn is_blocked(&self, name: &str) -> Result<bool, Box<dyn Error>> { pub fn is_blocked(&self, name: &str) -> Result<bool> {
let rules = unsafe { self.policy.Rules() }?; let rules = unsafe { self.policy.Rules() }?;
let rule_exists = unsafe { rules.Item(&BSTR::from(name)) }.is_ok(); let rule_exists = unsafe { rules.Item(&BSTR::from(name)) }.is_ok();
Ok(rule_exists) Ok(rule_exists)