Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions console_backend/src/settings_tab.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::time::Duration;
Expand Down Expand Up @@ -514,14 +515,26 @@ struct Settings {
default: SettingValue,
}

lazy_static! {
static ref SETTING_ORDERING: HashMap<(&'static str, &'static str), usize> = {
Setting::all()
.iter()
.enumerate()
.fold(HashMap::new(), |mut settings, (index, setting)| {
settings.insert((&setting.group, &setting.name), index);
settings
})
};
}

impl Settings {
fn new() -> Self {
Self {
// Keep the settings ordered in the same order as defined in the libsettings settings.yaml file
inner: Setting::all()
.iter()
.fold(IndexMap::new(), |mut settings, setting| {
(*settings.entry(setting.group.clone()).or_default())
.insert(setting.name.clone(), SettingsEntry::new(setting));
settings.insert(setting.group.clone(), IndexMap::new());
settings
}),
default: SettingValue::String("".into()),
Expand All @@ -530,7 +543,7 @@ impl Settings {

fn groups(&self) -> Vec<Vec<(&Setting, &SettingValue)>> {
self.inner.values().fold(Vec::new(), |mut groups, group| {
let group: Vec<_> = group
let mut group: Vec<_> = group
.values()
.map(|setting| {
setting.value.as_ref().map_or_else(
Expand All @@ -539,6 +552,18 @@ impl Settings {
)
})
.collect();

// Sort settings within a group by the order in which they're defined within the libsettings settings.yaml file
group.sort_by(|a, b| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a little puzzled as to how this is working with SETTING_ORDERING being a HashMap instead of IndexMap but testing it out passing the settings-yaml cli argument maintained correct group order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, SETTING_ORDERING is only holding the position in the YAML file of each setting, so it doesn't matter how its ordered.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see now that makes sense.

let a_index = SETTING_ORDERING
.get(&(&a.0.group, &a.0.name))
.unwrap_or(&usize::MAX);
let b_index = SETTING_ORDERING
.get(&(&b.0.group, &b.0.name))
.unwrap_or(&usize::MAX);
a_index.cmp(b_index)
});

if !group.is_empty() {
groups.push(group);
}
Expand Down Expand Up @@ -574,12 +599,3 @@ struct SettingsEntry {
setting: Cow<'static, Setting>,
value: Option<SettingValue>,
}

impl SettingsEntry {
fn new(setting: &'static Setting) -> Self {
Self {
setting: Cow::Borrowed(setting),
value: None,
}
}
}