feat: introduce rules (#10)

* feat: introduce rules

* docs: rules example
This commit is contained in:
Jozef Steinhübl 2024-08-18 20:26:57 +02:00 committed by GitHub
parent 3c74a0fe4f
commit affb5df12b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 81 additions and 4 deletions

View file

@ -34,6 +34,15 @@ You can configure state, details and git integration by changing Discord Presenc
// URL for small image // URL for small image
"small_image": "{base_icons_url}/zed.png", "small_image": "{base_icons_url}/zed.png",
"small_text": "Zed", "small_text": "Zed",
// Rules - disable presence in some workspaces
"rules": {
"mode": "blacklist" // or whitelist
"paths": [
"absolute path"
]
},
"git_integration": true "git_integration": true
} }
} }
@ -41,4 +50,4 @@ You can configure state, details and git integration by changing Discord Presenc
} }
``` ```
You can also use `null` to unset the option. Possible for everything except `base_icons_url` and `git_integration` You can also use `null` to unset the option. Possible for everything except `base_icons_url`, `rules` and `git_integration`

View file

@ -19,6 +19,39 @@
use serde_json::Value; use serde_json::Value;
#[derive(Debug, PartialEq)]
pub enum RulesMode {
Whitelist,
Blacklist,
}
#[derive(Debug)]
pub struct Rules {
pub mode: RulesMode,
pub paths: Vec<String>,
}
impl Default for Rules {
fn default() -> Self {
Rules {
mode: RulesMode::Blacklist,
paths: Vec::new(),
}
}
}
impl Rules {
pub fn suitable(&self, path: &str) -> bool {
let contains = self.paths.contains(&path.to_string());
if self.mode == RulesMode::Blacklist {
!contains
} else {
contains
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Configuration { pub struct Configuration {
pub base_icons_url: String, pub base_icons_url: String,
@ -31,6 +64,8 @@ pub struct Configuration {
pub small_image: Option<String>, pub small_image: Option<String>,
pub small_text: Option<String>, pub small_text: Option<String>,
pub rules: Rules,
pub git_integration: bool, pub git_integration: bool,
} }
@ -66,6 +101,7 @@ impl Configuration {
large_text: Some(String::from("{language:u}")), large_text: Some(String::from("{language:u}")),
small_image: Some(String::from("{base_icons_url}/zed.png")), small_image: Some(String::from("{base_icons_url}/zed.png")),
small_text: Some(String::from("Zed")), small_text: Some(String::from("Zed")),
rules: Rules::default(),
git_integration: true, git_integration: true,
} }
} }
@ -80,6 +116,28 @@ impl Configuration {
set_option!(self, options, small_image, "small_image"); set_option!(self, options, small_image, "small_image");
set_option!(self, options, small_text, "small_text"); set_option!(self, options, small_text, "small_text");
if let Some(rules) = options.get("rules") {
self.rules.mode = rules.get("mode").and_then(|m| m.as_str()).map_or(
RulesMode::Blacklist,
|mode| match mode {
"whitelist" => RulesMode::Whitelist,
"blacklist" => RulesMode::Blacklist,
_ => RulesMode::Blacklist,
},
);
self.rules.paths =
rules
.get("paths")
.and_then(|p| p.as_array())
.map_or(Vec::new(), |paths| {
paths
.iter()
.filter_map(|p| p.as_str().map(|s| s.to_string()))
.collect()
});
}
if let Some(git_integration) = options.get("git_integration") { if let Some(git_integration) = options.get("git_integration") {
self.git_integration = git_integration.as_bool().unwrap_or(true); self.git_integration = git_integration.as_bool().unwrap_or(true);
} }

View file

@ -21,6 +21,7 @@ use std::ffi::OsStr;
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::Deref; use std::ops::Deref;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::exit;
use std::sync::{Mutex, MutexGuard}; use std::sync::{Mutex, MutexGuard};
use configuration::Configuration; use configuration::Configuration;
@ -155,9 +156,6 @@ impl Backend {
#[tower_lsp::async_trait] #[tower_lsp::async_trait]
impl LanguageServer for Backend { impl LanguageServer for Backend {
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> { async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
// Connect discord client
self.discord.connect();
// Set workspace name // Set workspace name
let root_uri = params.root_uri.expect("Failed to get root uri"); let root_uri = params.root_uri.expect("Failed to get root uri");
let workspace_path = Path::new(root_uri.path()); let workspace_path = Path::new(root_uri.path());
@ -178,6 +176,18 @@ impl LanguageServer for Backend {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
config.set(params.initialization_options); config.set(params.initialization_options);
if config.rules.suitable(
workspace_path
.to_str()
.expect("Failed to transform workspace path to str"),
) {
// Connect discord client
self.discord.connect();
} else {
// Exit LSP
exit(0);
}
Ok(InitializeResult { Ok(InitializeResult {
server_info: Some(ServerInfo { server_info: Some(ServerInfo {
name: env!("CARGO_PKG_NAME").into(), name: env!("CARGO_PKG_NAME").into(),