From 9b1557276e7973cdc1ed6c3ff4a2855815c6bae4 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Tue, 26 Jul 2022 16:46:19 +0200 Subject: [PATCH] chore(types): update macros --- server/src/middleware/validate_json.rs | 4 +--- types/src/config/types.rs | 15 ++++----------- types/src/custom_option.rs | 13 +++++++++++++ types/src/lib.rs | 3 +-- types/src/macros.rs | 7 ------- 5 files changed, 19 insertions(+), 23 deletions(-) create mode 100644 types/src/custom_option.rs delete mode 100644 types/src/macros.rs diff --git a/server/src/middleware/validate_json.rs b/server/src/middleware/validate_json.rs index c19db64..08368ca 100644 --- a/server/src/middleware/validate_json.rs +++ b/server/src/middleware/validate_json.rs @@ -2,9 +2,7 @@ use axum::{extract::rejection::JsonRejection, Json}; use homedisk_types::errors::ServerError; /// Validate json request -pub fn validate_json( - payload: Result, JsonRejection>, -) -> Result, ServerError> { +pub fn validate_json(payload: Result, JsonRejection>) -> Result, ServerError> { match payload { // if success return payload Ok(payload) => Ok(payload), diff --git a/types/src/config/types.rs b/types/src/config/types.rs index ec3caf1..b897958 100644 --- a/types/src/config/types.rs +++ b/types/src/config/types.rs @@ -1,12 +1,7 @@ -//! Configuration file types - use std::fs; use serde::{Deserialize, Serialize}; -use crate::option_return; - -/// Config type #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { /// Configure HTTP settings @@ -17,7 +12,6 @@ pub struct Config { pub storage: ConfigStorage, } -/// HTTP config #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConfigHTTP { /// HTTP Host @@ -28,7 +22,6 @@ pub struct ConfigHTTP { pub cors: Vec, } -/// Json Web Token config #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConfigJWT { /// JWT Secret string (used to sign tokens) @@ -37,7 +30,6 @@ pub struct ConfigJWT { pub expires: i64, } -/// Storage config #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ConfigStorage { /// Directory where user files will be stored @@ -54,9 +46,10 @@ impl Config { /// let config = Config::parse().unwrap(); /// ``` pub fn parse() -> anyhow::Result { - // get file path of config file - let config_dir = option_return!(dirs::config_dir(), "find config dir")?; - let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy()); + // get path to the user's config directory + let sys_config_dir = dirs::config_dir().unwrap(); + // path to the homedisk config file + let config_path = format!("{}/homedisk/config.toml", sys_config_dir.to_string_lossy()); // read file content to string let config = fs::read_to_string(config_path)?; diff --git a/types/src/custom_option.rs b/types/src/custom_option.rs new file mode 100644 index 0000000..9ad2503 --- /dev/null +++ b/types/src/custom_option.rs @@ -0,0 +1,13 @@ +use std::io; + +/// Custom functions implemented for Option +pub trait OptionOkOrErr { + /// If the value is some return it, if not return Error + fn ok_or_err(self, desc: &str) -> Result; +} + +impl OptionOkOrErr for Option { + fn ok_or_err(self, desc: &str) -> Result { + self.ok_or(io::Error::new(io::ErrorKind::Other, desc)) + } +} diff --git a/types/src/lib.rs b/types/src/lib.rs index d0a1ea4..6444212 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -2,9 +2,8 @@ pub mod auth; pub mod config; +pub mod custom_option; #[cfg(feature = "database")] pub mod database; pub mod errors; pub mod fs; - -mod macros; diff --git a/types/src/macros.rs b/types/src/macros.rs deleted file mode 100644 index 4885351..0000000 --- a/types/src/macros.rs +++ /dev/null @@ -1,7 +0,0 @@ -/// Return value or error (if None) from Some(T) -#[macro_export] -macro_rules! option_return { - ($variable:expr,$err_desc:expr) => { - $variable.ok_or(std::io::Error::new(std::io::ErrorKind::Other, $err_desc)) - }; -}