|
| 1 | +use core::fmt; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use std::{ |
| 4 | + fmt::{Display, Formatter}, |
| 5 | + fs, io, |
| 6 | + path::PathBuf, |
| 7 | +}; |
| 8 | +use thiserror::Error; |
| 9 | + |
| 10 | +#[derive(Debug, Error)] |
| 11 | +pub enum ConfigPathError { |
| 12 | + #[error("Unable to get OS config directory")] |
| 13 | + UnknownBasePath, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Error)] |
| 17 | +pub enum ConfigInitError { |
| 18 | + #[error("Unable to get config path: {0}")] |
| 19 | + Path(#[from] ConfigPathError), |
| 20 | + #[error("I/O error: {0}")] |
| 21 | + IO(#[from] io::Error), |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Error)] |
| 25 | +pub enum ConfigParseError { |
| 26 | + #[error("Unable to get config path: {0}")] |
| 27 | + Path(#[from] ConfigPathError), |
| 28 | + #[error("Unable to initialize config: {0}")] |
| 29 | + Init(#[from] ConfigInitError), |
| 30 | + #[error("Unable to serialize or deserialize config: {0}")] |
| 31 | + Serde(#[from] serde_json::Error), |
| 32 | + #[error("I/O error: {0}")] |
| 33 | + IO(#[from] io::Error), |
| 34 | +} |
| 35 | + |
| 36 | +fn discord_token_default() -> String { |
| 37 | + String::from("Please provide a token") |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Debug, PartialEq, PartialOrd, Serialize, Deserialize, Clone)] |
| 41 | +pub struct Config { |
| 42 | + #[serde(rename = "discordToken", default = "discord_token_default")] |
| 43 | + pub discord_token: String, |
| 44 | +} |
| 45 | + |
| 46 | +impl Default for Config { |
| 47 | + fn default() -> Self { |
| 48 | + Config { |
| 49 | + discord_token: discord_token_default(), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl Display for Config { |
| 55 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 56 | + let content = match serde_json::to_string(self) { |
| 57 | + Ok(content) => content, |
| 58 | + Err(error) => { |
| 59 | + return write!(f, "Unable to serialize config: {}", error); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + write!(f, "{}", content) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[derive(Debug)] |
| 68 | +pub struct ConfigHandler { |
| 69 | + pub app_name: String, |
| 70 | +} |
| 71 | + |
| 72 | +impl ConfigHandler { |
| 73 | + pub fn new(app_name: &str) -> Self { |
| 74 | + ConfigHandler { |
| 75 | + app_name: app_name.to_string(), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn get_config_dir_path(&self) -> Result<PathBuf, ConfigPathError> { |
| 80 | + let mut path = match dirs::config_dir() { |
| 81 | + Some(path) => path, |
| 82 | + None => return Err(ConfigPathError::UnknownBasePath), |
| 83 | + }; |
| 84 | + |
| 85 | + path.push(&self.app_name); |
| 86 | + Ok(path) |
| 87 | + } |
| 88 | + |
| 89 | + pub fn create_config_dir_path(&self) -> Result<(), ConfigInitError> { |
| 90 | + let path = self.get_config_dir_path()?; |
| 91 | + fs::create_dir_all(path)?; |
| 92 | + Ok(()) |
| 93 | + } |
| 94 | + |
| 95 | + pub fn get_config_file_path(&self) -> Result<PathBuf, ConfigPathError> { |
| 96 | + let mut path = self.get_config_dir_path()?; |
| 97 | + path.push("config.json"); |
| 98 | + Ok(path) |
| 99 | + } |
| 100 | + |
| 101 | + pub fn save_config(&self, config: &Config) -> Result<(), ConfigParseError> { |
| 102 | + let path = self.get_config_file_path()?; |
| 103 | + |
| 104 | + if !path.exists() { |
| 105 | + self.create_config_dir_path()?; |
| 106 | + } |
| 107 | + |
| 108 | + let config_json = serde_json::to_string_pretty(config)?; |
| 109 | + |
| 110 | + fs::write(path, config_json)?; |
| 111 | + |
| 112 | + Ok(()) |
| 113 | + } |
| 114 | + |
| 115 | + pub fn load_config(&self) -> Result<Config, ConfigParseError> { |
| 116 | + let path = self.get_config_file_path()?; |
| 117 | + if !path.exists() { |
| 118 | + self.create_config_dir_path()?; |
| 119 | + fs::write(&path, "{}")?; |
| 120 | + } |
| 121 | + |
| 122 | + let config_json = fs::read_to_string(path)?; |
| 123 | + let config: Config = serde_json::from_str(&config_json)?; |
| 124 | + |
| 125 | + self.save_config(&config)?; // In case the config file was missing some fields which serde used the defaults for |
| 126 | + |
| 127 | + Ok(config) |
| 128 | + } |
| 129 | +} |
0 commit comments