livesplit_auto_splitting/
settings.rs

1use std::collections::HashMap;
2
3/// A setting that is meant to be shown to and modified by the user.
4#[non_exhaustive]
5pub struct UserSetting {
6    /// A unique identifier for this setting. This is not meant to be shown to
7    /// the user and is only used to keep track of the setting.
8    pub key: Box<str>,
9    /// The name of the setting that is shown to the user.
10    pub description: Box<str>,
11    /// The default value of the setting. This also specifies the type of the
12    /// setting.
13    pub default_value: SettingValue,
14}
15
16/// A value that a setting can have.
17#[non_exhaustive]
18#[derive(Clone, Debug)]
19pub enum SettingValue {
20    /// A boolean value.
21    Bool(bool),
22}
23
24/// Stores all the settings of an auto splitter. Currently this only stores
25/// values that are modified. So there may be settings that are registered as
26/// user settings, but because the user didn't modify them, they are not stored
27/// here yet.
28#[derive(Clone, Default)]
29pub struct SettingsStore {
30    values: HashMap<Box<str>, SettingValue>,
31}
32
33impl SettingsStore {
34    /// Creates a new empty settings store.
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    /// Sets a setting to the new value. If the key of the setting doesn't exist
40    /// yet it will be stored as a new value. Otherwise the value will be
41    /// updated.
42    pub fn set(&mut self, key: Box<str>, value: SettingValue) {
43        self.values.insert(key, value);
44    }
45
46    /// Accesses the value of a setting by its key. While the setting may exist
47    /// as part of the user settings, it may not have been stored into the
48    /// settings store yet, so it may not exist, despite being registered.
49    pub fn get(&self, key: &str) -> Option<&SettingValue> {
50        self.values.get(key)
51    }
52
53    /// Iterates over all the setting keys and their values in the settings
54    /// store.
55    pub fn iter(&self) -> impl Iterator<Item = (&str, &SettingValue)> {
56        self.values.iter().map(|(k, v)| (k.as_ref(), v))
57    }
58}