chore(types): update macros

This commit is contained in:
MedzikUser 2022-07-26 16:46:19 +02:00
parent e616fa8c79
commit 9b1557276e
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
5 changed files with 19 additions and 23 deletions

View File

@ -2,9 +2,7 @@ use axum::{extract::rejection::JsonRejection, Json};
use homedisk_types::errors::ServerError;
/// Validate json request
pub fn validate_json<T>(
payload: Result<Json<T>, JsonRejection>,
) -> Result<Json<T>, ServerError> {
pub fn validate_json<T>(payload: Result<Json<T>, JsonRejection>) -> Result<Json<T>, ServerError> {
match payload {
// if success return payload
Ok(payload) => Ok(payload),

View File

@ -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<String>,
}
/// 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<Config> {
// 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)?;

View File

@ -0,0 +1,13 @@
use std::io;
/// Custom functions implemented for Option<T>
pub trait OptionOkOrErr<T> {
/// If the value is some return it, if not return Error
fn ok_or_err(self, desc: &str) -> Result<T, io::Error>;
}
impl<T> OptionOkOrErr<T> for Option<T> {
fn ok_or_err(self, desc: &str) -> Result<T, io::Error> {
self.ok_or(io::Error::new(io::ErrorKind::Other, desc))
}
}

View File

@ -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;

View File

@ -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))
};
}