feat(core): complete work on patch system structure

This commit is contained in:
Federico Terzi 2021-07-31 16:58:54 +02:00
parent bc19b412ae
commit 0112603ff7
6 changed files with 176 additions and 196 deletions

View File

@ -18,7 +18,11 @@
*/ */
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use espanso_config::{config::ConfigStore, error::{ErrorLevel, NonFatalErrorSet}, matches::store::MatchStore}; use espanso_config::{
config::ConfigStore,
error::{ErrorLevel, NonFatalErrorSet},
matches::store::MatchStore,
};
use log::{error, info, warn}; use log::{error, info, warn};
use std::path::Path; use std::path::Path;
@ -80,7 +84,8 @@ pub fn load_config(config_path: &Path, packages_path: &Path) -> Result<ConfigLoa
.context("unable to load legacy config")?; .context("unable to load legacy config")?;
Ok(ConfigLoadResult { Ok(ConfigLoadResult {
config_store, // Apply the built-in patches
config_store: crate::patch::patch_store(config_store),
match_store, match_store,
is_legacy_config: true, is_legacy_config: true,
non_fatal_errors: Vec::new(), non_fatal_errors: Vec::new(),
@ -93,7 +98,10 @@ pub fn load_config(config_path: &Path, packages_path: &Path) -> Result<ConfigLoa
if !non_fatal_errors.is_empty() { if !non_fatal_errors.is_empty() {
warn!("------- detected some errors in the configuration: -------"); warn!("------- detected some errors in the configuration: -------");
for non_fatal_error_set in &non_fatal_errors { for non_fatal_error_set in &non_fatal_errors {
warn!(">>> {}", non_fatal_error_set.file.to_string_lossy().to_string()); warn!(
">>> {}",
non_fatal_error_set.file.to_string_lossy().to_string()
);
for record in &non_fatal_error_set.errors { for record in &non_fatal_error_set.errors {
if record.level == ErrorLevel::Error { if record.level == ErrorLevel::Error {
error!("{:?}", record.error); error!("{:?}", record.error);
@ -106,7 +114,8 @@ pub fn load_config(config_path: &Path, packages_path: &Path) -> Result<ConfigLoa
} }
Ok(ConfigLoadResult { Ok(ConfigLoadResult {
config_store, // Apply the built-in patches
config_store: crate::patch::patch_store(config_store),
match_store, match_store,
is_legacy_config: false, is_legacy_config: false,
non_fatal_errors, non_fatal_errors,

View File

@ -19,7 +19,7 @@
use std::sync::Arc; use std::sync::Arc;
use espanso_config::config::{ConfigStore, Config}; use espanso_config::config::{Config, ConfigStore};
use log::debug; use log::debug;
use super::PatchDefinition; use super::PatchDefinition;
@ -34,19 +34,25 @@ impl PatchedConfigStore {
Self::from_store_with_patches(config_store, super::get_builtin_patches()) Self::from_store_with_patches(config_store, super::get_builtin_patches())
} }
pub fn from_store_with_patches(config_store: Box<dyn ConfigStore>, patches: Vec<PatchDefinition>) -> Self { pub fn from_store_with_patches(
config_store: Box<dyn ConfigStore>,
patches: Vec<PatchDefinition>,
) -> Self {
// Only keep the patches that should be active in the current system // Only keep the patches that should be active in the current system
let active_patches = patches.into_iter().filter(|patch| { let active_patches = patches
let should_be_activated = (patch.should_be_activated)(); .into_iter()
.filter(|patch| {
let is_enabled = (patch.is_enabled)();
if should_be_activated { if is_enabled {
debug!("activating '{}' patch", patch.name); debug!("enabled '{}' patch", patch.name);
} else { } else {
debug!("skipping '{}' patch", patch.name); debug!("skipping '{}' patch", patch.name);
} }
should_be_activated is_enabled
}).collect(); })
.collect();
Self { Self {
config_store, config_store,
@ -60,15 +66,19 @@ impl ConfigStore for PatchedConfigStore {
self.config_store.default() self.config_store.default()
} }
fn active<'f>( fn active<'f>(&'f self, app: &espanso_config::config::AppProperties) -> Arc<dyn Config> {
&'f self, let active_config = self.config_store.active(app);
app: &espanso_config::config::AppProperties,
) -> Arc<dyn Config>{ // Check if a patch should be applied
todo!() if let Some(patch) = self.patches.iter().find(|patch| (patch.should_patch)(app)) {
(patch.apply)(active_config)
} else {
active_config
}
} }
fn configs(&self) -> Vec<Arc<dyn Config>> { fn configs(&self) -> Vec<Arc<dyn Config>> {
todo!() self.config_store.configs()
} }
fn get_all_match_paths(&self) -> std::collections::HashSet<String> { fn get_all_match_paths(&self) -> std::collections::HashSet<String> {

View File

@ -19,107 +19,34 @@
use std::sync::Arc; use std::sync::Arc;
use espanso_config::config::{AppProperties, Backend, Config, ToggleKey}; use espanso_config::config::{AppProperties, Config, ConfigStore};
mod config_store; mod config_store;
mod patches; mod patches;
pub fn get_builtin_patches() -> Vec<PatchDefinition> { pub fn patch_store(store: Box<dyn ConfigStore>) -> Box<dyn ConfigStore> {
// TODO Box::new(config_store::PatchedConfigStore::from_store(store))
vec![
patches::win::onenote_for_windows_10::patch(),
]
} }
// TODO: fix visibility levels (pub/pub(crate)) fn get_builtin_patches() -> Vec<PatchDefinition> {
#[cfg(target_os = "windows")]
return vec![
patches::win::onenote_for_windows_10::patch(),
];
// TODO: patches should be "merged" at runtime with the active config, unless a config option (like "apply_patch") #[cfg(target_os = "macos")]
// is set. This is needed because we still want to allow the user to define user-specific configs without return vec![];
// losing the patch-specific changes
#[cfg(target_os = "linux")]
return vec![
// TODO: all the terminals registered in the legacy version + libre office
// + firefox
];
}
pub struct PatchDefinition { pub struct PatchDefinition {
pub name: &'static str, pub name: &'static str,
pub should_be_activated: fn() -> bool, pub is_enabled: fn() -> bool,
pub patch_config: fn(config: Arc<dyn Config>) -> Arc<dyn PatchedConfig>, pub should_patch: fn(app: &AppProperties) -> bool,
} pub apply: fn(config: Arc<dyn Config>) -> Arc<dyn Config>,
pub struct DefaultPatchedConfig {
base: Arc<dyn Config>,
}
pub trait PatchedConfig: Config {
// TODO: can we pass a simple reference here?
fn get_base(&self) -> Arc<dyn Config>;
fn id(&self) -> i32 {
self.get_base().id()
}
fn label(&self) -> &str {
self.get_base().label()
}
fn match_paths(&self) -> &[String] {
self.get_base().match_paths()
}
fn backend(&self) -> Backend {
self.get_base().backend()
}
fn clipboard_threshold(&self) -> usize {
self.get_base().clipboard_threshold()
}
fn pre_paste_delay(&self) -> usize {
self.get_base().pre_paste_delay()
}
fn paste_shortcut_event_delay(&self) -> usize {
self.get_base().paste_shortcut_event_delay()
}
fn paste_shortcut(&self) -> Option<String> {
self.get_base().paste_shortcut()
}
fn disable_x11_fast_inject(&self) -> bool {
self.get_base().disable_x11_fast_inject()
}
fn toggle_key(&self) -> Option<ToggleKey> {
self.get_base().toggle_key()
}
fn auto_restart(&self) -> bool {
self.get_base().auto_restart()
}
fn preserve_clipboard(&self) -> bool {
self.get_base().preserve_clipboard()
}
fn restore_clipboard_delay(&self) -> usize {
self.get_base().restore_clipboard_delay()
}
fn inject_delay(&self) -> Option<usize> {
self.get_base().inject_delay()
}
fn key_delay(&self) -> Option<usize> {
self.get_base().key_delay()
}
fn word_separators(&self) -> Vec<String> {
self.get_base().word_separators()
}
fn backspace_limit(&self) -> usize {
self.get_base().backspace_limit()
}
fn is_match<'a>(&self, app: &AppProperties<'a>) -> bool {
self.get_base().is_match(app)
}
} }

View File

@ -0,0 +1,76 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* espanso is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
#[macro_export]
macro_rules! generate_patchable_config {
( $struct_name:ident, $( $config_field:ident ->$config_type:ty ),* ) => {
use std::sync::Arc;
use espanso_config::config::{AppProperties, Config};
pub struct $struct_name {
base: Arc<dyn Config>,
patch: Patches,
}
impl $struct_name {
#[allow(dead_code)]
pub fn patch(base: Arc<dyn Config>, patch: Patches) -> Self {
Self {
base,
patch,
}
}
}
#[derive(Default)]
pub struct Patches {
$(
pub $config_field: Option<$config_type>,
)*
}
impl Config for $struct_name {
$(
fn $config_field(&self) -> $config_type {
if let Some(patched) = self.patch.$config_field.clone() {
return patched;
}
self.base.$config_field()
}
)*
fn id(&self) -> i32 {
self.base.id()
}
fn label(&self) -> &str {
self.base.label()
}
fn match_paths(&self) -> &[String] {
self.base.match_paths()
}
fn is_match<'b>(&self, app: &AppProperties<'b>) -> bool {
self.base.is_match(app)
}
}
};
}

View File

@ -17,80 +17,28 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use espanso_config::config::{Backend, ToggleKey};
#[cfg(target_os = "windows")]
pub mod win; pub mod win;
#[macro_use]
mod macros;
generate_patchable_config!(
// #[macro_export] PatchedConfig,
// macro_rules! create_patch { backend -> Backend,
// // TODO: add function body clipboard_threshold -> usize,
// ( $should_be_activated:expr, $( $config_field:ident ->$config_type:ty ),* ) => { pre_paste_delay -> usize,
// { paste_shortcut_event_delay -> usize,
// $( paste_shortcut -> Option<String>,
// if $child.$x.is_none() { disable_x11_fast_inject -> bool,
// $child.$x = $parent.$x.clone(); toggle_key -> Option<ToggleKey>,
// } auto_restart -> bool,
// )* preserve_clipboard -> bool,
restore_clipboard_delay -> usize,
// // Build a temporary object to verify that all fields inject_delay -> Option<usize>,
// // are being used at compile time key_delay -> Option<usize>,
// $t { word_separators -> Vec<String>,
// $( backspace_limit -> usize
// $x: None, );
// )*
// };
// }
// {
// use crate::patch::{ConfigProxy, Patch};
// pub struct PatchImpl<'a> {
// patched_config: PatchedConfig<'a>,
// }
// impl<'a> PatchImpl<'a> {
// pub fn new(default: &'a dyn Config) -> Self {
// Self {
// patched_config: PatchedConfig::new(default),
// }
// }
// }
// impl<'a> Patch<'a> for PatchImpl<'a> {
// fn should_be_activated(&self) -> bool {
// $should_be_activated
// }
// fn patch_name(&self) -> &'static str {
// todo!()
// }
// fn config(&self) -> &'a dyn Config {
// &self.patched_config
// }
// }
// pub struct PatchedConfig<'a> {
// default: &'a dyn Config,
// }
// impl<'a> PatchedConfig<'a> {
// pub fn new(default: &'a dyn Config) -> Self {
// Self { default }
// }
// }
// impl<'a> ConfigProxy<'a> for PatchedConfig<'a> {
// fn get_default(&self) -> &'a dyn espanso_config::config::Config {
// self.default
// }
// $(
// fn $config_field(&self) -> $config_type {
// todo!()
// }
// )*
// }
// }
// };
// }

View File

@ -17,14 +17,24 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::sync::Arc;
use crate::patch::patches::{PatchedConfig, Patches};
use crate::patch::PatchDefinition; use crate::patch::PatchDefinition;
pub fn patch() -> PatchDefinition { pub fn patch() -> PatchDefinition {
PatchDefinition { PatchDefinition {
name: file!(), name: module_path!().split(":").last().unwrap_or("unknown"),
should_be_activated: || cfg!(target_os = "windows"), is_enabled: || cfg!(target_os = "windows"),
patch_config: |base| { should_patch: |app| app.title.unwrap_or_default().contains("OneNote"),
todo!() apply: |base| {
Arc::new(PatchedConfig::patch(
base,
Patches {
key_delay: Some(Some(10)),
..Default::default()
},
))
}, },
} }
} }