Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use configparser::ini::Ini;
- pub enum LoggingType {
- TERMINAL,
- FILE
- }
- pub struct Config {
- pub hostname: String,
- pub port: u16,
- pub redis_addr: String,
- pub log_type: Option<LoggingType>,
- pub on: bool,
- }
- #[derive(Debug)]
- pub struct ConfigError {
- pub message: String,
- pub category: String,
- pub field: String
- }
- /// Load config from config.conf file located in root directory
- /// Returns config struct
- pub fn load_config() -> Result<Config, ConfigError> {
- let mut config = Ini::new();
- config.load(std::path::Path::new("./config.conf"))
- .expect(
- "Malformed config, try copying config from the
- repo's readme or create a new issue on github."
- );
- Ok(Config {
- redis_addr: config.get("cache", "redis_addr").unwrap(),
- hostname: config.get("host", "hostname").unwrap(),
- port: config.get("host", "port")
- .unwrap()
- .parse::<u16>()
- .unwrap(),
- on: config.get("logging", "on")
- .unwrap()
- .eq("yes")
- .then(|| true)
- .or_else(|| enum_primitive::Option::Some(false))
- .unwrap(),
- log_type: enum_primitive::Option::Some((|| {
- let lt = config.get("logging", "type")
- .unwrap();
- if lt == "file" {
- LoggingType::FILE
- } else {
- LoggingType::TERMINAL
- }
- })())
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement