style(config): fix formatting

This commit is contained in:
Federico Terzi 2021-10-06 18:37:15 +02:00
parent 9202c1189c
commit be68a1f8ff
10 changed files with 153 additions and 91 deletions

View File

@ -20,4 +20,4 @@
pub(crate) const DEFAULT_CLIPBOARD_THRESHOLD: usize = 100;
pub(crate) const DEFAULT_PRE_PASTE_DELAY: usize = 100;
pub(crate) const DEFAULT_SHORTCUT_EVENT_DELAY: usize = 10;
pub(crate) const DEFAULT_RESTORE_CLIPBOARD_DELAY: usize = 300;
pub(crate) const DEFAULT_RESTORE_CLIPBOARD_DELAY: usize = 300;

View File

@ -82,4 +82,4 @@ impl ParsedConfig {
pub enum ParsedConfigError {
#[error("can't load config `{0}`")]
LoadFailed(#[from] anyhow::Error),
}
}

View File

@ -269,14 +269,15 @@ mod tests {
.unwrap();
let parsed_config: ParsedConfig = config.try_into().unwrap();
let keyboard_layout: BTreeMap<String, String> =
vec![
("rules".to_string(), "test_rule".to_string()),
("model".to_string(), "test_model".to_string()),
("layout".to_string(), "test_layout".to_string()),
("variant".to_string(), "test_variant".to_string()),
("options".to_string(), "test_options".to_string()),
].into_iter().collect();
let keyboard_layout: BTreeMap<String, String> = vec![
("rules".to_string(), "test_rule".to_string()),
("model".to_string(), "test_model".to_string()),
("layout".to_string(), "test_layout".to_string()),
("variant".to_string(), "test_variant".to_string()),
("options".to_string(), "test_options".to_string()),
]
.into_iter()
.collect();
assert_eq!(
parsed_config,

View File

@ -17,13 +17,22 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use super::{AppProperties, Backend, Config, RMLVOConfig, ToggleKey, default::{DEFAULT_CLIPBOARD_THRESHOLD, DEFAULT_PRE_PASTE_DELAY, DEFAULT_RESTORE_CLIPBOARD_DELAY, DEFAULT_SHORTCUT_EVENT_DELAY}, parse::ParsedConfig, path::calculate_paths, util::os_matches};
use super::{
default::{
DEFAULT_CLIPBOARD_THRESHOLD, DEFAULT_PRE_PASTE_DELAY, DEFAULT_RESTORE_CLIPBOARD_DELAY,
DEFAULT_SHORTCUT_EVENT_DELAY,
},
parse::ParsedConfig,
path::calculate_paths,
util::os_matches,
AppProperties, Backend, Config, RMLVOConfig, ToggleKey,
};
use crate::{counter::next_id, merge};
use anyhow::Result;
use log::error;
use regex::Regex;
use std::path::PathBuf;
use std::{collections::HashSet, path::Path};
use std::{path::PathBuf};
use thiserror::Error;
const STANDARD_INCLUDES: &[&str] = &["../match/**/*.yml"];
@ -149,7 +158,7 @@ impl Config for ResolvedConfig {
}
}
}
fn enable(&self) -> bool {
self.parsed.enable.unwrap_or(true)
}
@ -240,16 +249,18 @@ impl Config for ResolvedConfig {
}
fn word_separators(&self) -> Vec<String> {
self.parsed.word_separators.clone().unwrap_or_else(|| vec![
" ".to_string(),
",".to_string(),
".".to_string(),
"?".to_string(),
"!".to_string(),
"\r".to_string(),
"\n".to_string(),
(22u8 as char).to_string(),
])
self.parsed.word_separators.clone().unwrap_or_else(|| {
vec![
" ".to_string(),
",".to_string(),
".".to_string(),
"?".to_string(),
"!".to_string(),
"\r".to_string(),
"\n".to_string(),
(22u8 as char).to_string(),
]
})
}
fn backspace_limit(&self) -> usize {
@ -261,13 +272,17 @@ impl Config for ResolvedConfig {
}
fn keyboard_layout(&self) -> Option<RMLVOConfig> {
self.parsed.keyboard_layout.as_ref().map(|layout| RMLVOConfig {
rules: layout.get("rules").map(String::from),
model: layout.get("model").map(String::from),
layout: layout.get("layout").map(String::from),
variant: layout.get("variant").map(String::from),
options: layout.get("options").map(String::from),
})
self
.parsed
.keyboard_layout
.as_ref()
.map(|layout| RMLVOConfig {
rules: layout.get("rules").map(String::from),
model: layout.get("model").map(String::from),
layout: layout.get("layout").map(String::from),
variant: layout.get("variant").map(String::from),
options: layout.get("options").map(String::from),
})
}
fn search_trigger(&self) -> Option<String> {
@ -459,7 +474,10 @@ impl ResolvedConfig {
let exclude_paths = calculate_paths(base_dir, excludes.iter());
let include_paths = calculate_paths(base_dir, includes.iter());
include_paths.difference(&exclude_paths).cloned().collect::<HashSet<_>>()
include_paths
.difference(&exclude_paths)
.cloned()
.collect::<HashSet<_>>()
}
}
@ -481,7 +499,10 @@ mod tests {
ResolvedConfig::aggregate_includes(&ParsedConfig {
..Default::default()
}),
vec!["../match/**/*.yml".to_string(),].iter().cloned().collect::<HashSet<_>>()
vec!["../match/**/*.yml".to_string(),]
.iter()
.cloned()
.collect::<HashSet<_>>()
);
}
@ -504,8 +525,9 @@ mod tests {
..Default::default()
}),
vec!["../match/**/*.yml".to_string(), "custom/*.yml".to_string()]
.iter()
.cloned().collect::<HashSet<_>>()
.iter()
.cloned()
.collect::<HashSet<_>>()
);
}
@ -517,8 +539,9 @@ mod tests {
..Default::default()
}),
vec!["../match/**/*.yml".to_string(), "custom/*.yml".to_string()]
.iter()
.cloned().collect::<HashSet<_>>()
.iter()
.cloned()
.collect::<HashSet<_>>()
);
}
@ -531,12 +554,13 @@ mod tests {
..Default::default()
}),
vec![
"../match/**/*.yml".to_string(),
"custom/*.yml".to_string(),
"sub/*.yml".to_string()
]
.iter()
.cloned().collect::<HashSet<_>>()
"../match/**/*.yml".to_string(),
"custom/*.yml".to_string(),
"sub/*.yml".to_string()
]
.iter()
.cloned()
.collect::<HashSet<_>>()
);
}
@ -546,7 +570,10 @@ mod tests {
ResolvedConfig::aggregate_excludes(&ParsedConfig {
..Default::default()
}),
vec!["../match/**/_*.yml".to_string(),].iter().cloned().collect::<HashSet<_>>()
vec!["../match/**/_*.yml".to_string(),]
.iter()
.cloned()
.collect::<HashSet<_>>()
);
}
@ -569,8 +596,9 @@ mod tests {
..Default::default()
}),
vec!["../match/**/_*.yml".to_string(), "custom/*.yml".to_string()]
.iter()
.cloned().collect::<HashSet<_>>()
.iter()
.cloned()
.collect::<HashSet<_>>()
);
}
@ -582,8 +610,9 @@ mod tests {
..Default::default()
}),
vec!["../match/**/_*.yml".to_string(), "custom/*.yml".to_string()]
.iter()
.cloned().collect::<HashSet<_>>()
.iter()
.cloned()
.collect::<HashSet<_>>()
);
}
@ -596,12 +625,13 @@ mod tests {
..Default::default()
}),
vec![
"../match/**/_*.yml".to_string(),
"custom/*.yml".to_string(),
"sub/*.yml".to_string()
]
.iter()
.cloned().collect::<HashSet<_>>()
"../match/**/_*.yml".to_string(),
"custom/*.yml".to_string(),
"sub/*.yml".to_string()
]
.iter()
.cloned()
.collect::<HashSet<_>>()
);
}

View File

@ -17,8 +17,8 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::path::{Path, PathBuf};
use anyhow::Error;
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct NonFatalErrorSet {
@ -68,4 +68,4 @@ impl ErrorRecord {
pub enum ErrorLevel {
Error,
Warning,
}
}

View File

@ -976,8 +976,7 @@ mod tests {
#[test]
fn test_config_set_load_fail_bad_directory() {
let config_set =
LegacyConfigSet::load(Path::new("invalid/path"), Path::new("invalid/path"));
let config_set = LegacyConfigSet::load(Path::new("invalid/path"), Path::new("invalid/path"));
assert!(config_set.is_err());
assert_eq!(
config_set.unwrap_err(),
@ -1606,9 +1605,10 @@ mod tests {
#[test]
fn test_list_has_conflict_no_conflict() {
assert!(
!LegacyConfigSet::list_has_conflicts(&[":ab".to_owned(), ":bc".to_owned()])
);
assert!(!LegacyConfigSet::list_has_conflicts(&[
":ab".to_owned(),
":bc".to_owned()
]));
}
#[test]
@ -1643,9 +1643,10 @@ mod tests {
);
let config_set = LegacyConfigSet::load(data_dir.path(), package_dir.path()).unwrap();
assert!(
!LegacyConfigSet::has_conflicts(&config_set.default, &config_set.specific),
);
assert!(!LegacyConfigSet::has_conflicts(
&config_set.default,
&config_set.specific
),);
}
#[test]
@ -1675,9 +1676,10 @@ mod tests {
);
let config_set = LegacyConfigSet::load(data_dir.path(), package_dir.path()).unwrap();
assert!(
LegacyConfigSet::has_conflicts(&config_set.default, &config_set.specific),
);
assert!(LegacyConfigSet::has_conflicts(
&config_set.default,
&config_set.specific
),);
}
#[test]
@ -1705,9 +1707,10 @@ mod tests {
);
let config_set = LegacyConfigSet::load(data_dir.path(), package_dir.path()).unwrap();
assert!(
LegacyConfigSet::has_conflicts(&config_set.default, &config_set.specific),
);
assert!(LegacyConfigSet::has_conflicts(
&config_set.default,
&config_set.specific
),);
}
#[test]
@ -1746,9 +1749,10 @@ mod tests {
);
let config_set = LegacyConfigSet::load(data_dir.path(), package_dir.path()).unwrap();
assert!(
!LegacyConfigSet::has_conflicts(&config_set.default, &config_set.specific),
);
assert!(!LegacyConfigSet::has_conflicts(
&config_set.default,
&config_set.specific
),);
}
#[test]

View File

@ -360,11 +360,11 @@ impl Config for LegacyInteropConfig {
}
fn search_trigger(&self) -> Option<String> {
self.config.search_trigger.clone()
self.config.search_trigger.clone()
}
fn search_shortcut(&self) -> Option<String> {
self.config.search_shortcut.clone()
self.config.search_shortcut.clone()
}
fn undo_backspace(&self) -> bool {
@ -390,7 +390,7 @@ impl Config for LegacyInteropConfig {
fn win32_exclude_orphan_events(&self) -> bool {
true
}
fn evdev_modifier_delay(&self) -> Option<usize> {
Some(10)
}

View File

@ -34,7 +34,13 @@ pub mod matches;
mod util;
#[allow(clippy::type_complexity)]
pub fn load(base_path: &Path) -> Result<(Box<dyn ConfigStore>, Box<dyn MatchStore>, Vec<error::NonFatalErrorSet>)> {
pub fn load(
base_path: &Path,
) -> Result<(
Box<dyn ConfigStore>,
Box<dyn MatchStore>,
Vec<error::NonFatalErrorSet>,
)> {
let config_dir = base_path.join("config");
if !config_dir.exists() || !config_dir.is_dir() {
return Err(ConfigError::MissingConfigDir().into());
@ -43,16 +49,24 @@ pub fn load(base_path: &Path) -> Result<(Box<dyn ConfigStore>, Box<dyn MatchStor
let (config_store, non_fatal_config_errors) = config::load_store(&config_dir)?;
let root_paths = config_store.get_all_match_paths();
let (match_store, non_fatal_match_errors) = matches::store::load(&root_paths.into_iter().collect::<Vec<String>>());
let (match_store, non_fatal_match_errors) =
matches::store::load(&root_paths.into_iter().collect::<Vec<String>>());
let mut non_fatal_errors = Vec::new();
non_fatal_errors.extend(non_fatal_config_errors.into_iter());
non_fatal_errors.extend(non_fatal_match_errors.into_iter());
Ok((Box::new(config_store), Box::new(match_store), non_fatal_errors))
Ok((
Box::new(config_store),
Box::new(match_store),
non_fatal_errors,
))
}
pub fn load_legacy(config_dir: &Path, package_dir: &Path) -> Result<(Box<dyn ConfigStore>, Box<dyn MatchStore>)> {
pub fn load_legacy(
config_dir: &Path,
package_dir: &Path,
) -> Result<(Box<dyn ConfigStore>, Box<dyn MatchStore>)> {
legacy::load(config_dir, package_dir)
}
@ -70,7 +84,7 @@ pub enum ConfigError {
mod tests {
use super::*;
use crate::util::tests::use_test_directory;
use config::{AppProperties};
use config::AppProperties;
#[test]
fn load_works_correctly() {
@ -222,12 +236,12 @@ mod tests {
.unwrap();
let (config_store, match_store, errors) = load(base).unwrap();
assert_eq!(errors.len(), 3);
// It shouldn't have loaded the "config.yml" one because of the YAML error
assert_eq!(config_store.configs().len(), 1);
// It shouldn't load "base.yml" and "_sub.yml" due to YAML errors
assert_eq!(match_store.loaded_paths().len(), 1);
assert_eq!(match_store.loaded_paths().len(), 1);
});
}
@ -250,7 +264,7 @@ mod tests {
std::fs::write(&config_file, r#""#).unwrap();
let (config_store, match_store, errors) = load(base).unwrap();
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].file, base_file);
assert_eq!(errors[0].errors.len(), 1);
@ -280,13 +294,17 @@ mod tests {
.unwrap();
let config_file = config_dir.join("default.yml");
std::fs::write(&config_file, r#"
std::fs::write(
&config_file,
r#"
invalid
"
"#).unwrap();
"#,
)
.unwrap();
// A syntax error in the default.yml file cannot be handled gracefully
// A syntax error in the default.yml file cannot be handled gracefully
assert!(load(base).is_err());
});
}

View File

@ -17,11 +17,11 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::{path::Path};
use std::path::Path;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_yaml::{Mapping};
use serde_yaml::Mapping;
use crate::util::is_yaml_empty;

View File

@ -140,17 +140,26 @@ mod tests {
#[test]
fn convert_value_params() {
let mut mapping = serde_yaml::Mapping::new();
mapping.insert(serde_yaml::Value::String("test".to_string()), serde_yaml::Value::Null);
mapping.insert(
serde_yaml::Value::String("test".to_string()),
serde_yaml::Value::Null,
);
let mut expected = Params::new();
expected.insert("test".to_string(), Value::Null);
assert_eq!(convert_value(serde_yaml::Value::Mapping(mapping)).unwrap(), Value::Object(expected));
assert_eq!(
convert_value(serde_yaml::Value::Mapping(mapping)).unwrap(),
Value::Object(expected)
);
}
#[test]
fn convert_params_works_correctly() {
let mut mapping = serde_yaml::Mapping::new();
mapping.insert(serde_yaml::Value::String("test".to_string()), serde_yaml::Value::Null);
mapping.insert(
serde_yaml::Value::String("test".to_string()),
serde_yaml::Value::Null,
);
let mut expected = Params::new();
expected.insert("test".to_string(), Value::Null);