Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
//! Declarative plugin settings, rendered generically by the client (the native
//! equivalent of Vencord's `definePluginSettings`).
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SettingValue {
Bool(bool),
Number(f64),
Text(String),
}
#[derive(Clone, Debug)]
pub enum SettingKind {
Bool {
default: bool,
},
Text {
default: &'static str,
multiline: bool,
placeholder: &'static str,
},
Number {
default: f64,
min: f64,
max: f64,
},
/// Stored as the option's value string.
Select {
default: &'static str,
options: &'static [(&'static str, &'static str)],
},
}
impl SettingKind {
pub fn default_value(&self) -> SettingValue {
match self {
SettingKind::Bool { default } => SettingValue::Bool(*default),
SettingKind::Text { default, .. } => SettingValue::Text(default.to_string()),
SettingKind::Number { default, .. } => SettingValue::Number(*default),
SettingKind::Select { default, .. } => SettingValue::Text(default.to_string()),
}
}
}
#[derive(Clone, Debug)]
pub struct SettingDef {
pub key: &'static str,
pub label: &'static str,
pub description: &'static str,
pub kind: SettingKind,
}
impl SettingDef {
pub fn bool(key: &'static str, label: &'static str, description: &'static str, default: bool) -> Self {
Self { key, label, description, kind: SettingKind::Bool { default } }
}
pub fn text(key: &'static str, label: &'static str, description: &'static str, placeholder: &'static str, multiline: bool) -> Self {
Self {
key,
label,
description,
kind: SettingKind::Text { default: "", multiline, placeholder },
}
}
pub fn select(
key: &'static str,
label: &'static str,
description: &'static str,
default: &'static str,
options: &'static [(&'static str, &'static str)],
) -> Self {
Self { key, label, description, kind: SettingKind::Select { default, options } }
}
}
/// A plugin's current settings: stored values with fallbacks to the defaults.
#[derive(Clone, Debug, Default)]
pub struct PluginSettings {
defs: Vec<SettingDef>,
values: BTreeMap<String, SettingValue>,
}
impl PluginSettings {
pub fn new(defs: Vec<SettingDef>, values: BTreeMap<String, SettingValue>) -> Self {
Self { defs, values }
}
pub fn defs(&self) -> &[SettingDef] {
&self.defs
}
pub fn values(&self) -> &BTreeMap<String, SettingValue> {
&self.values
}
pub fn get(&self, key: &str) -> SettingValue {
self.values
.get(key)
.cloned()
.or_else(|| {
self.defs
.iter()
.find(|d| d.key == key)
.map(|d| d.kind.default_value())
})
.unwrap_or(SettingValue::Bool(false))
}
pub fn bool(&self, key: &str) -> bool {
matches!(self.get(key), SettingValue::Bool(true))
}
pub fn number(&self, key: &str) -> f64 {
match self.get(key) {
SettingValue::Number(n) => n,
SettingValue::Text(t) => t.parse().unwrap_or(0.0),
SettingValue::Bool(b) => b as u8 as f64,
}
}
pub fn text(&self, key: &str) -> String {
match self.get(key) {
SettingValue::Text(t) => t,
SettingValue::Number(n) => n.to_string(),
SettingValue::Bool(b) => b.to_string(),
}
}
/// Comma / whitespace / newline separated list.
pub fn list(&self, key: &str) -> Vec<String> {
self.text(key)
.split(|c: char| c == ',' || c.is_whitespace())
.map(str::trim)
.filter(|s| !s.is_empty())
.map(String::from)
.collect()
}
pub fn set(&mut self, key: &str, value: SettingValue) {
self.values.insert(key.to_string(), value);
}
}