chore(types): update macros
This commit is contained in:
parent
e616fa8c79
commit
9b1557276e
|
@ -2,9 +2,7 @@ use axum::{extract::rejection::JsonRejection, Json};
|
||||||
use homedisk_types::errors::ServerError;
|
use homedisk_types::errors::ServerError;
|
||||||
|
|
||||||
/// Validate json request
|
/// Validate json request
|
||||||
pub fn validate_json<T>(
|
pub fn validate_json<T>(payload: Result<Json<T>, JsonRejection>) -> Result<Json<T>, ServerError> {
|
||||||
payload: Result<Json<T>, JsonRejection>,
|
|
||||||
) -> Result<Json<T>, ServerError> {
|
|
||||||
match payload {
|
match payload {
|
||||||
// if success return payload
|
// if success return payload
|
||||||
Ok(payload) => Ok(payload),
|
Ok(payload) => Ok(payload),
|
||||||
|
|
|
@ -1,12 +1,7 @@
|
||||||
//! Configuration file types
|
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::option_return;
|
|
||||||
|
|
||||||
/// Config type
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Configure HTTP settings
|
/// Configure HTTP settings
|
||||||
|
@ -17,7 +12,6 @@ pub struct Config {
|
||||||
pub storage: ConfigStorage,
|
pub storage: ConfigStorage,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// HTTP config
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ConfigHTTP {
|
pub struct ConfigHTTP {
|
||||||
/// HTTP Host
|
/// HTTP Host
|
||||||
|
@ -28,7 +22,6 @@ pub struct ConfigHTTP {
|
||||||
pub cors: Vec<String>,
|
pub cors: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Json Web Token config
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ConfigJWT {
|
pub struct ConfigJWT {
|
||||||
/// JWT Secret string (used to sign tokens)
|
/// JWT Secret string (used to sign tokens)
|
||||||
|
@ -37,7 +30,6 @@ pub struct ConfigJWT {
|
||||||
pub expires: i64,
|
pub expires: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Storage config
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ConfigStorage {
|
pub struct ConfigStorage {
|
||||||
/// Directory where user files will be stored
|
/// Directory where user files will be stored
|
||||||
|
@ -54,9 +46,10 @@ impl Config {
|
||||||
/// let config = Config::parse().unwrap();
|
/// let config = Config::parse().unwrap();
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parse() -> anyhow::Result<Config> {
|
pub fn parse() -> anyhow::Result<Config> {
|
||||||
// get file path of config file
|
// get path to the user's config directory
|
||||||
let config_dir = option_return!(dirs::config_dir(), "find config dir")?;
|
let sys_config_dir = dirs::config_dir().unwrap();
|
||||||
let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy());
|
// path to the homedisk config file
|
||||||
|
let config_path = format!("{}/homedisk/config.toml", sys_config_dir.to_string_lossy());
|
||||||
|
|
||||||
// read file content to string
|
// read file content to string
|
||||||
let config = fs::read_to_string(config_path)?;
|
let config = fs::read_to_string(config_path)?;
|
||||||
|
|
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,9 +2,8 @@
|
||||||
|
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
pub mod custom_option;
|
||||||
#[cfg(feature = "database")]
|
#[cfg(feature = "database")]
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
|
|
||||||
mod macros;
|
|
||||||
|
|
|
@ -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))
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
Reference in New Issue