From 2e511c13276b7109e6774263d37eafce8df0a434 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 3 Mar 2023 12:43:34 +0800 Subject: chore: optimize config path --- src/config.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 3c2c633..7f21027 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,10 +7,10 @@ use std::{ use anyhow::{anyhow, Result}; use serde::Deserialize; -pub const CONFIG_FILE_NAME: &str = "config.yaml"; -pub const ROLES_FILE_NAME: &str = "roles.yaml"; -pub const HISTORY_FILE_NAME: &str = "history.txt"; -pub const MESSAGE_FILE_NAME: &str = "messages.md"; +const CONFIG_FILE_NAME: &str = "config.yaml"; +const ROLES_FILE_NAME: &str = "roles.yaml"; +const HISTORY_FILE_NAME: &str = "history.txt"; +const MESSAGE_FILE_NAME: &str = "messages.md"; #[derive(Debug, Clone, Deserialize)] pub struct Config { @@ -40,6 +40,7 @@ impl Config { config.load_roles()?; Ok(config) } + pub fn local_file(name: &str) -> Result { let env_name = format!( "{}_CONFIG_DIR", @@ -58,8 +59,25 @@ impl Config { path.push(name); Ok(path) } + + pub fn config_file() -> Result { + Self::local_file(CONFIG_FILE_NAME) + } + + pub fn roles_file() -> Result { + Self::local_file(ROLES_FILE_NAME) + } + + pub fn history_file() -> Result { + Self::local_file(HISTORY_FILE_NAME) + } + + pub fn messages_file() -> Result { + Self::local_file(MESSAGE_FILE_NAME) + } + fn load_roles(&mut self) -> Result<()> { - let path = Self::local_file(ROLES_FILE_NAME)?; + let path = Self::roles_file()?; if !path.exists() { return Ok(()); } -- cgit v1.2.3