Advertisement
Guest User

Untitled

a guest
Sep 21st, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. use configparser::ini::Ini;
  2.  
  3. pub enum LoggingType {
  4. TERMINAL,
  5. FILE
  6. }
  7. pub struct Config {
  8. pub hostname: String,
  9. pub port: u16,
  10. pub redis_addr: String,
  11. pub log_type: Option<LoggingType>,
  12. pub on: bool,
  13. }
  14.  
  15. #[derive(Debug)]
  16. pub struct ConfigError {
  17. pub message: String,
  18. pub category: String,
  19. pub field: String
  20. }
  21.  
  22. /// Load config from config.conf file located in root directory
  23. /// Returns config struct
  24. pub fn load_config() -> Result<Config, ConfigError> {
  25. let mut config = Ini::new();
  26. config.load(std::path::Path::new("./config.conf"))
  27. .expect(
  28. "Malformed config, try copying config from the
  29. repo's readme or create a new issue on github."
  30. );
  31.  
  32. Ok(Config {
  33. redis_addr: config.get("cache", "redis_addr").unwrap(),
  34. hostname: config.get("host", "hostname").unwrap(),
  35. port: config.get("host", "port")
  36. .unwrap()
  37. .parse::<u16>()
  38. .unwrap(),
  39. on: config.get("logging", "on")
  40. .unwrap()
  41. .eq("yes")
  42. .then(|| true)
  43. .or_else(|| enum_primitive::Option::Some(false))
  44. .unwrap(),
  45. log_type: enum_primitive::Option::Some((|| {
  46. let lt = config.get("logging", "type")
  47. .unwrap();
  48.  
  49. if lt == "file" {
  50. LoggingType::FILE
  51. } else {
  52. LoggingType::TERMINAL
  53. }
  54. })())
  55. })
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement