Make settings variant map use u64 instead of strings since no longer restricted by JSON

This commit is contained in:
NGnius (Graham) 2023-11-18 17:16:58 -05:00
parent 04a94c902f
commit d481f13144
6 changed files with 147 additions and 7 deletions

View file

@ -76,7 +76,7 @@ fn main() -> Result<(), ()> {
let mut loaded_settings =
persist::FileJson::open(utility::settings_dir().join(DEFAULT_SETTINGS_FILE))
.map(|mut file| file.variants.remove("0")
.map(|mut file| file.variants.remove(&0)
.map(|settings| settings::Settings::from_json(DEFAULT_SETTINGS_NAME.into(), settings, DEFAULT_SETTINGS_FILE.into()))
.unwrap_or_else(|| settings::Settings::system_default(
DEFAULT_SETTINGS_FILE.into(),

View file

@ -9,7 +9,7 @@ use super::SettingsJson;
pub struct FileJson {
pub version: u64,
pub name: String,
pub variants: HashMap<String, SettingsJson>,
pub variants: HashMap<u64, SettingsJson>,
}
impl FileJson {
@ -45,11 +45,11 @@ impl FileJson {
let file = if path.exists() {
let mut file = Self::open(path)?;
file.variants.insert(setting.variant.to_string(), setting);
file.variants.insert(setting.variant, setting);
file
} else {
let mut setting_variants = HashMap::with_capacity(1);
setting_variants.insert(setting.variant.to_string(), setting);
setting_variants.insert(setting.variant, setting);
Self {
version: 0,
name: given_name,

View file

@ -20,7 +20,7 @@ impl Default for SettingsJson {
fn default() -> Self {
Self {
version: 0,
name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
name: crate::consts::DEFAULT_SETTINGS_VARIANT_NAME.to_owned(),
variant: 0,
persistent: false,
cpus: Vec::with_capacity(8),

View file

@ -178,7 +178,7 @@ impl Settings {
}
pub fn get_variant<'a>(settings_file: &'a FileJson, variant_id: u64, variant_name: String) -> Result<&'a SettingsJson, SettingError> {
if let Some(variant) = settings_file.variants.get(&variant_id.to_string()) {
if let Some(variant) = settings_file.variants.get(&variant_id) {
Ok(variant)
} else {
Err(SettingError {

View file

@ -121,8 +121,40 @@ mod generate {
limits: limits_core::json_v2::GenericBatteryLimit::default_for(limits_core::json_v2::BatteryLimitType::SteamDeck),
},
};
let output_file = std::fs::File::create("../limits_override.ron").unwrap();
let output_file = std::fs::File::create(format!("../{}", crate::consts::LIMITS_OVERRIDE_FILE)).unwrap();
ron::ser::to_writer_pretty(output_file, &limits, crate::utility::ron_pretty_config()).unwrap();
}
#[test]
fn generate_default_minimal_save_file() {
let mut mini_variants = std::collections::HashMap::with_capacity(2);
mini_variants.insert(0, crate::persist::SettingsJson {
version: 0,
name: crate::consts::DEFAULT_SETTINGS_VARIANT_NAME.to_owned(),
variant: 0,
persistent: false,
cpus: vec![crate::persist::CpuJson::default(); 8],
gpu: crate::persist::GpuJson::default(),
battery: crate::persist::BatteryJson::default(),
provider: None,
});
mini_variants.insert(42, crate::persist::SettingsJson {
version: 0,
name: "FortySecondary".to_owned(),
variant: 42,
persistent: false,
cpus: vec![crate::persist::CpuJson::default(); 8],
gpu: crate::persist::GpuJson::default(),
battery: crate::persist::BatteryJson::default(),
provider: None,
});
let savefile = crate::persist::FileJson {
version: 0,
name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
variants: mini_variants,
};
let output_file = std::fs::File::create(format!("../{}", crate::consts::DEFAULT_SETTINGS_FILE)).unwrap();
ron::ser::to_writer_pretty(output_file, &savefile, crate::utility::ron_pretty_config()).unwrap();
}
}

108
default_settings.ron Normal file
View file

@ -0,0 +1,108 @@
FileJson(
version: 0,
name: "Main",
variants: {
0: SettingsJson(
version: 0,
name: "Primary",
variant: 0,
persistent: false,
cpus: [CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
)],
gpu: GpuJson(
fast_ppt: None,
slow_ppt: None,
clock_limits: None,
slow_memory: false,
),
battery: BatteryJson(
charge_rate: None,
charge_mode: None,
events: [],
),
provider: None,
),
42: SettingsJson(
version: 0,
name: "FortySecondary",
variant: 42,
persistent: false,
cpus: [CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
), CpuJson(
online: true,
clock_limits: None,
governor: "schedutil",
)],
gpu: GpuJson(
fast_ppt: None,
slow_ppt: None,
clock_limits: None,
slow_memory: false,
),
battery: BatteryJson(
charge_rate: None,
charge_mode: None,
events: [],
),
provider: None,
),
},
)