Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: merge config as TOML value #1534

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 37 additions & 52 deletions tools/twix/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
pub mod keybind_plugin;
pub mod keys;
pub mod merge;

use std::path::{Path, PathBuf};

use merge::Merge;
use serde::Deserialize;
use toml::{map::Entry, Value};

const DEFAULT_CONFIG: &str = include_str!("../config_default.toml");

Expand Down Expand Up @@ -38,31 +37,6 @@ pub struct NaoConfig {
pub highest: u8,
}

#[derive(Debug, Deserialize)]
pub struct RawNaoConfig {
pub lowest: Option<u8>,
pub highest: Option<u8>,
}

impl Merge<RawNaoConfig> for NaoConfig {
fn merge(&mut self, other: RawNaoConfig) {
self.lowest.merge(other.lowest);
self.highest.merge(other.highest);
}
}

impl Merge<NaoConfig> for NaoConfig {
fn merge(&mut self, other: NaoConfig) {
*self = other;
}
}

#[derive(Debug, Deserialize)]
pub struct RawConfiguration {
pub keys: Option<keys::Keybinds>,
pub naos: Option<RawNaoConfig>,
}

impl Configuration {
pub fn load() -> Result<Self, Error> {
Self::load_from_file(config_path())
Expand All @@ -71,12 +45,12 @@ impl Configuration {
pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
match std::fs::read_to_string(&path) {
Ok(config_file) => {
let mut configuration = Self::default();
let user_configuration: RawConfiguration = toml::from_str(&config_file)?;
let mut configuration: Value = Self::default();
let user_configuration: Value = toml::from_str(&config_file)?;

configuration.merge(user_configuration);
configuration.update(user_configuration);

Ok(configuration)
Ok(configuration.try_into()?)
}
Err(error) => {
log::info!(
Expand All @@ -88,33 +62,43 @@ impl Configuration {
}
}
}
}

impl Merge<RawConfiguration> for Configuration {
fn merge(&mut self, other: RawConfiguration) {
self.keys.merge(other.keys);
self.naos.merge(other.naos);
fn default<T: for<'de> Deserialize<'de>>() -> T {
toml::from_str(DEFAULT_CONFIG).unwrap()
}
}

impl Merge<Configuration> for Configuration {
fn merge(&mut self, other: Configuration) {
self.keys.merge(other.keys);
self.naos.merge(other.naos);
}
trait Update {
fn update(&mut self, other: Self);
}

impl Default for Configuration {
fn default() -> Self {
toml::from_str(DEFAULT_CONFIG).unwrap()
impl Update for Value {
fn update(&mut self, other: Self) {
match (self, other) {
(Value::Table(self_table), Value::Table(other_table)) => {
for (key, value) in other_table {
match self_table.entry(key) {
Entry::Vacant(entry) => {
entry.insert(value);
}
Entry::Occupied(entry) => {
entry.into_mut().update(value);
}
}
}
}
(other_self, other) => {
*other_self = other;
}
}
}
}

#[cfg(test)]
mod tests {
use crate::configuration::merge::Merge;
use crate::configuration::Update;

use super::{Configuration, RawConfiguration, DEFAULT_CONFIG};
use super::{Configuration, DEFAULT_CONFIG};

#[test]
fn parse_default_config() {
Expand All @@ -123,7 +107,7 @@ mod tests {

#[test]
fn merge_configs() {
let mut config_1: Configuration = toml::from_str(
let mut config_1: toml::Value = toml::from_str(
r#"
[keys]
C-a = "focus_left"
Expand All @@ -136,7 +120,7 @@ mod tests {
)
.unwrap();

let config_2: Configuration = toml::from_str(
let config_2: toml::Value = toml::from_str(
r#"
[keys]
C-b = "focus_left"
Expand All @@ -149,14 +133,15 @@ mod tests {
)
.unwrap();

config_1.merge(config_2);
config_1.update(config_2);

assert_eq!(
config_1,
toml::from_str(
r#"
[keys]
C-a = "focus_left"
C-S-a = "reconnect"
C-A = "focus_right"
C-b = "focus_left"

Expand All @@ -171,7 +156,7 @@ mod tests {

#[test]
fn merge_partial_config() {
let mut default_config: Configuration = toml::from_str(
let mut default_config: toml::Value = toml::from_str(
r#"
[keys]
C-a = "focus_left"
Expand All @@ -184,15 +169,15 @@ mod tests {
)
.unwrap();

let user_config: RawConfiguration = toml::from_str(
let user_config: toml::Value = toml::from_str(
r#"
[keys]
C-a = "focus_right"
"#,
)
.unwrap();

default_config.merge(user_config);
default_config.update(user_config);

assert_eq!(
default_config,
Expand Down
8 changes: 0 additions & 8 deletions tools/twix/src/configuration/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use serde::{
};
use thiserror::Error;

use super::merge::Merge;

#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug, Error)]
pub enum Error {
Expand Down Expand Up @@ -151,12 +149,6 @@ impl Keybinds {
}
}

impl Merge<Self> for Keybinds {
fn merge(&mut self, other: Self) {
self.keybinds.merge(other.keybinds);
}
}

impl<'de> Deserialize<'de> for Keybinds {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
50 changes: 0 additions & 50 deletions tools/twix/src/configuration/merge.rs

This file was deleted.

Loading