A tool to read animebox backup files and export the data in alternate formats.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

4 years ago
4 years ago
  1. use std::error::Error;
  2. use std::ffi::OsString;
  3. use std::fs::File;
  4. use std::io::BufReader;
  5. use std::path::Path;
  6. use std::process::exit;
  7. use serde::{Deserialize, Serialize};
  8. use serde_json::from_reader;
  9. #[derive(Serialize, Deserialize, Debug)]
  10. #[serde(deny_unknown_fields)]
  11. pub struct Config {
  12. pub save_directory: Option<String>,
  13. }
  14. pub struct ConfigManager {
  15. config_path: OsString,
  16. pub config: Config,
  17. }
  18. fn read_config<P: AsRef<Path>>(path: P) -> Result<Config, Box<dyn Error>> {
  19. if path.as_ref().exists() {
  20. let file = File::open(path)?;
  21. let reader = BufReader::new(file);
  22. let result: Config = from_reader(reader)?;
  23. Ok(result)
  24. } else {
  25. Ok(Config {
  26. save_directory: Some(String::from("tmp")),
  27. })
  28. }
  29. }
  30. impl ConfigManager {
  31. pub fn new(config_path: &Path) -> ConfigManager {
  32. let mut config = ConfigManager {
  33. config_path: OsString::from(config_path.as_os_str()),
  34. config: Config {
  35. save_directory: None,
  36. },
  37. };
  38. config.load();
  39. config
  40. }
  41. fn load(&mut self) {
  42. match read_config(&self.config_path) {
  43. Err(e) => {
  44. println!("Failed to read config file {:#?}: {}", &self.config_path, e);
  45. exit(1);
  46. }
  47. Ok(config) => self.config = config,
  48. }
  49. }
  50. }