From e7e6007e260a7fc16f2478fd1b8b20dd54fe4e22 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 20 Oct 2021 21:39:01 +0200 Subject: [PATCH] feat(service): Add `isolate_with` plugin query config This query parameter will isolate search results when the specific regex query is matched. --- service/src/lib.rs | 14 ++++++++++++++ service/src/plugins/config.rs | 7 +++++++ service/src/plugins/help.rs | 1 + service/src/plugins/mod.rs | 4 ++++ 4 files changed, 26 insertions(+) diff --git a/service/src/lib.rs b/service/src/lib.rs index 508a9b4..64b8917 100644 --- a/service/src/lib.rs +++ b/service/src/lib.rs @@ -211,9 +211,16 @@ impl + Unpin> Service { let init = std::sync::Arc::new(init); + let isolate_with = config + .query + .isolate_with + .as_ref() + .and_then(|expr| Regex::new(&*expr).ok()); + entry.insert(PluginConnector::new( config, regex, + isolate_with, Box::new(move || { let (request_tx, request_rx) = mpsc::channel(8); @@ -359,6 +366,13 @@ impl + Unpin> Service { break; } + if let Some(regex) = plugin.isolate_regex.as_ref() { + if regex.is_match(query) { + isolated = Some(key); + break; + } + } + query_queue.push(key); } diff --git a/service/src/plugins/config.rs b/service/src/plugins/config.rs index 860eb7a..102bbf7 100644 --- a/service/src/plugins/config.rs +++ b/service/src/plugins/config.rs @@ -49,6 +49,13 @@ pub struct PluginQuery { #[serde(default)] pub isolate: bool, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "::serde_with::rust::unwrap_or_skip" + )] + pub isolate_with: Option>, + #[serde(default)] pub no_sort: bool, diff --git a/service/src/plugins/help.rs b/service/src/plugins/help.rs index 461995e..5f1d4ff 100644 --- a/service/src/plugins/help.rs +++ b/service/src/plugins/help.rs @@ -16,6 +16,7 @@ pub const CONFIG: PluginConfig = PluginConfig { query: PluginQuery { help: None, isolate: true, + isolate_with: None, no_sort: true, persistent: false, priority: PluginPriority::Default, diff --git a/service/src/plugins/mod.rs b/service/src/plugins/mod.rs index a078e14..747bc99 100644 --- a/service/src/plugins/mod.rs +++ b/service/src/plugins/mod.rs @@ -79,6 +79,8 @@ pub struct PluginConnector { /// this plugin to spawn as a background service pub init: Box Sender>, + pub isolate_regex: Option, + /// A compiled regular expression that a query must match /// for the launcher service to justify spawning and sending /// queries to this plugin @@ -93,11 +95,13 @@ impl PluginConnector { pub fn new( config: PluginConfig, regex: Option, + isolate_regex: Option, init: Box Sender + Send>, ) -> Self { Self { config, init, + isolate_regex, regex, sender: None, }