move Config into its own module
cargo-fmt
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
//! config module implements all the utilities to properly create and validate a router config
|
||||
|
||||
use configparser::ini::Ini;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Config {
|
||||
pub jwt_exp_time: u64,
|
||||
pub jwt_issuer: String,
|
||||
pub jwt_priv_key: String,
|
||||
pub jwt_pub_key: String,
|
||||
pub filestore_path: String,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
jwt_exp_time: 0,
|
||||
jwt_issuer: "".to_string(),
|
||||
jwt_priv_key: "".to_string(),
|
||||
jwt_pub_key: "".to_string(),
|
||||
filestore_path: "".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Ini> for Config {
|
||||
type Error = String;
|
||||
fn try_from(config: Ini) -> Result<Self, Self::Error> {
|
||||
let exp_time = config
|
||||
.get("jwt", "expiration_time")
|
||||
.unwrap_or("".to_string());
|
||||
let jwt_exp_time = {
|
||||
match u64::from_str(&exp_time) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("unable to convert JWT expiration time into u64 err={}", e);
|
||||
0
|
||||
}
|
||||
}
|
||||
};
|
||||
let config = Config {
|
||||
jwt_exp_time,
|
||||
jwt_issuer: config.get("jwt", "issuer").unwrap_or("".to_string()),
|
||||
jwt_pub_key: config.get("jwt", "public_key").unwrap_or("".to_string()),
|
||||
jwt_priv_key: config.get("jwt", "private_key").unwrap_or("".to_string()),
|
||||
filestore_path: config.get("store", "path").unwrap_or("".to_string()),
|
||||
};
|
||||
|
||||
if !config.validate() {
|
||||
return Err("ini file configuration validation failed".to_string());
|
||||
}
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// validates config ini file
|
||||
fn validate(&self) -> bool {
|
||||
if self.jwt_exp_time <= 0 {
|
||||
eprintln!("invalid config parameter: JWT expiration time is negative or equals to 0");
|
||||
return false;
|
||||
}
|
||||
|
||||
if self.jwt_issuer == "" {
|
||||
eprintln!("invalid config parameter: JWT issuer is empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: check if the file exists and rights are ok
|
||||
if self.jwt_pub_key == "" {
|
||||
eprintln!("invalid config parameter: JWT public key file path is empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: check if the file exists and rights are ok
|
||||
if self.jwt_priv_key == "" {
|
||||
eprintln!("invalid config parameter: JWT private key file path is empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
if self.filestore_path == "" {
|
||||
eprintln!("invalid config parameter: filestore path is empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config() {
|
||||
use std::env;
|
||||
|
||||
let root_path = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
|
||||
let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "config.ini");
|
||||
let mut config = Ini::new();
|
||||
let _r = config.load(config_path);
|
||||
|
||||
let router_config = Config::try_from(config);
|
||||
assert!(router_config.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bad_config() {
|
||||
use std::env;
|
||||
|
||||
let root_path = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
|
||||
let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "bad_config.ini");
|
||||
let mut config = Ini::new();
|
||||
let _r = config.load(config_path);
|
||||
|
||||
let router_config = Config::try_from(config);
|
||||
assert!(router_config.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bad_config_path() {
|
||||
use std::env;
|
||||
|
||||
let root_path = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
|
||||
let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "con.ini");
|
||||
let mut config = Ini::new();
|
||||
|
||||
let result = config.load(config_path);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
Reference in New Issue
Block a user