mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-01 18:51:04 +02:00
Merge pull request #45 from JakeStanger/feat/config-loading-improvements
Feat/config loading improvements
This commit is contained in:
commit
b2afe78c07
3 changed files with 37 additions and 7 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -1135,9 +1135,9 @@ checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libcorn"
|
name = "libcorn"
|
||||||
version = "0.6.0"
|
version = "0.6.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5684465f57a903e77cd58cc0bd9fcdc75b2b1a361731966183bf1f1743da4553"
|
checksum = "ec3c43083c921e27422ac8b8e96b75b55156cc1f6e176d9dbed0f02d57a86832"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"pest",
|
"pest",
|
||||||
|
|
|
@ -20,7 +20,7 @@ serde = { version = "1.0.141", features = ["derive"] }
|
||||||
serde_json = "1.0.82"
|
serde_json = "1.0.82"
|
||||||
serde_yaml = "0.9.4"
|
serde_yaml = "0.9.4"
|
||||||
toml = "0.5.9"
|
toml = "0.5.9"
|
||||||
libcorn = "0.6.0"
|
libcorn = "0.6.1"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
async_once = "0.2.6"
|
async_once = "0.2.6"
|
||||||
indexmap = "1.9.1"
|
indexmap = "1.9.1"
|
||||||
|
|
|
@ -13,7 +13,7 @@ use color_eyre::{eyre, Help, Report};
|
||||||
use dirs::config_dir;
|
use dirs::config_dir;
|
||||||
use eyre::Result;
|
use eyre::Result;
|
||||||
use gtk::Orientation;
|
use gtk::Orientation;
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Deserializer};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
|
@ -40,13 +40,44 @@ pub enum ModuleConfig {
|
||||||
Custom(CustomModule),
|
Custom(CustomModule),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[serde(untagged)]
|
|
||||||
pub enum MonitorConfig {
|
pub enum MonitorConfig {
|
||||||
Single(Config),
|
Single(Config),
|
||||||
Multiple(Vec<Config>),
|
Multiple(Vec<Config>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Manually implement for better untagged enum error handling:
|
||||||
|
// currently open pr: https://github.com/serde-rs/serde/pull/1544
|
||||||
|
impl<'de> Deserialize<'de> for MonitorConfig {
|
||||||
|
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let content =
|
||||||
|
<serde::__private::de::Content as serde::Deserialize>::deserialize(deserializer)?;
|
||||||
|
|
||||||
|
match <Config as serde::Deserialize>::deserialize(
|
||||||
|
serde::__private::de::ContentRefDeserializer::<D::Error>::new(&content),
|
||||||
|
) {
|
||||||
|
Ok(config) => Ok(Self::Single(config)),
|
||||||
|
Err(outer) => match <Vec<Config> as serde::Deserialize>::deserialize(
|
||||||
|
serde::__private::de::ContentRefDeserializer::<D::Error>::new(&content),
|
||||||
|
) {
|
||||||
|
Ok(config) => Ok(Self::Multiple(config)),
|
||||||
|
Err(inner) => {
|
||||||
|
let report = Report::msg(format!(" multi-bar (c): {inner}").replace("An error occurred when deserializing: ", ""))
|
||||||
|
.wrap_err(format!("single-bar (b): {outer}").replace("An error occurred when deserializing: ", ""))
|
||||||
|
.wrap_err("An invalid config was found. The following errors were encountered:")
|
||||||
|
.note("Both the single-bar (type b / error 1) and multi-bar (type c / error 2) config variants were tried. You can likely ignore whichever of these is not relevant to you.")
|
||||||
|
.suggestion("Please see https://github.com/JakeStanger/ironbar/wiki/configuration-guide#2-pick-your-use-case for more info on the above");
|
||||||
|
|
||||||
|
Err(serde::de::Error::custom(format!("{report:?}")))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Deserialize, Copy, Clone, PartialEq, Eq)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum BarPosition {
|
pub enum BarPosition {
|
||||||
|
@ -111,7 +142,6 @@ const fn default_bar_height() -> i32 {
|
||||||
impl Config {
|
impl Config {
|
||||||
/// Attempts to load the config file from file,
|
/// Attempts to load the config file from file,
|
||||||
/// parse it and return a new instance of `Self`.
|
/// parse it and return a new instance of `Self`.
|
||||||
#[instrument]
|
|
||||||
pub fn load() -> Result<Self> {
|
pub fn load() -> Result<Self> {
|
||||||
let config_path = env::var("IRONBAR_CONFIG").map_or_else(
|
let config_path = env::var("IRONBAR_CONFIG").map_or_else(
|
||||||
|_| Self::try_find_config(),
|
|_| Self::try_find_config(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue