diff options
| author | MaxVerevkin <maxxverrr@gmail.com> | 2023-08-21 16:12:23 +0300 |
|---|---|---|
| committer | MaxVerevkin <maxxverrr@gmail.com> | 2023-08-21 16:12:23 +0300 |
| commit | d1fe8e5999a5b16cfed7519f80b9710a7fbe1411 (patch) | |
| tree | 5e13c09e6a11398fe5d2f936ad3a33863b9c77f5 /src/config.rs | |
| parent | f4fe586d35dee16ff13930451e79b23a0079129c (diff) | |
| download | i3bar-mango-d1fe8e5999a5b16cfed7519f80b9710a7fbe1411.tar.gz | |
allow setting config path via cli args
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/src/config.rs b/src/config.rs index 5b657d0..f6ff47d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,7 +6,7 @@ use serde::{de, Deserialize}; use std::collections::HashMap; use std::fs::read_to_string; use std::ops::Deref; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::{env, fmt}; #[derive(Deserialize, Debug)] @@ -94,19 +94,21 @@ impl Default for Config { } impl Config { - pub fn new() -> Result<Self> { - Ok(match config_dir() { - Some(mut config_path) => { - config_path.push("i3bar-river"); - config_path.push("config.toml"); - if config_path.exists() { - let config = - read_to_string(config_path).context("Failed to read configuration")?; - toml::from_str(&config).context("Failed to deserialize configuration")? - } else { - eprintln!("Using default configuration"); - Self::default() - } + pub fn new(path: Option<&Path>) -> Result<Self> { + let buf; + + let path = match path { + Some(path) => Some(path), + None => { + buf = config_path(); + buf.as_deref() + } + }; + + Ok(match path { + Some(config_path) => { + let config = read_to_string(config_path).context("Failed to read configuration")?; + toml::from_str(&config).context("Failed to deserialize configuration")? } None => { eprintln!("Could not find the configuration path"); @@ -130,6 +132,13 @@ fn config_dir() -> Option<PathBuf> { .or_else(|| Some(PathBuf::from(env::var_os("HOME")?).join(".config"))) } +fn config_path() -> Option<PathBuf> { + let mut path = config_dir()?; + path.push("i3bar-river"); + path.push("config.toml"); + path.exists().then_some(path) +} + #[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)] #[serde(rename_all = "lowercase")] pub enum Position { |
