From f9517182411f04951904f586291b27bafd6dd20a Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Sun, 29 Oct 2023 22:20:32 +0100 Subject: [PATCH] Start using anyhow --- Cargo.lock | 1 + Cargo.toml | 1 + src/config_manager.rs | 8 +++++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 365c23e..6255e30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -688,6 +688,7 @@ dependencies = [ name = "nfsense" version = "0.1.0" dependencies = [ + "anyhow", "async-trait", "axum", "ipnet", diff --git a/Cargo.toml b/Cargo.toml index bc0920b..42101f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.75" async-trait = "0.1.74" axum = "0.6.20" ipnet = { version = "2.8.0", features = ["serde"] } diff --git a/src/config_manager.rs b/src/config_manager.rs index 04a55e9..958df99 100644 --- a/src/config_manager.rs +++ b/src/config_manager.rs @@ -3,9 +3,10 @@ use validator::Validate; use crate::api::ApiError; use super::definitions::config::Config; +use anyhow::{Context, Result}; use std::fs; +use std::io; use std::sync::{Arc, Mutex, MutexGuard}; -use std::{io, result::Result}; use pwhash::sha512_crypt; @@ -74,11 +75,12 @@ impl ConfigManager { self.shared_data.lock().unwrap().pending_config.clone() } - pub fn apply_pending_changes(&mut self) -> Result<(), ConfigError> { + pub fn apply_pending_changes(&mut self) -> Result<()> { let mut data = self.shared_data.lock().unwrap(); // TODO run Apply functions // TODO Revert on Apply Failure and Return - write_config_to_file(CURRENT_CONFIG_PATH, data.pending_config.clone())?; + write_config_to_file(CURRENT_CONFIG_PATH, data.pending_config.clone()) + .context("Writing Config to file".to_string())?; // TODO revert if config save fails // TODO Remove Pending Config File data.current_config = data.pending_config.clone();