use crate::{
platform::prelude::*,
util::{
ordered_map::{Iter, Map},
PopulateString,
},
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CustomVariable {
pub value: String,
pub is_permanent: bool,
}
impl CustomVariable {
pub fn permanent(&mut self) -> &mut Self {
self.is_permanent = true;
self
}
pub fn set_value<S>(&mut self, value: S)
where
S: PopulateString,
{
value.populate(&mut self.value);
}
pub fn clear_value(&mut self) {
self.value.clear();
}
}
#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RunMetadata {
pub run_id: String,
pub platform_name: String,
pub uses_emulator: bool,
pub region_name: String,
pub speedrun_com_variables: Map<String>,
pub custom_variables: Map<CustomVariable>,
}
impl RunMetadata {
#[inline]
pub fn new() -> Self {
Default::default()
}
#[inline]
pub fn run_id(&self) -> &str {
&self.run_id
}
#[inline]
pub fn set_run_id<S>(&mut self, id: S)
where
S: PopulateString,
{
id.populate(&mut self.run_id);
}
#[inline]
pub fn platform_name(&self) -> &str {
&self.platform_name
}
#[inline]
pub fn set_platform_name<S>(&mut self, name: S)
where
S: PopulateString,
{
name.populate(&mut self.platform_name);
}
#[inline]
pub const fn uses_emulator(&self) -> bool {
self.uses_emulator
}
#[inline]
pub fn set_emulator_usage(&mut self, uses_emulator: bool) {
self.uses_emulator = uses_emulator;
}
#[inline]
pub fn region_name(&self) -> &str {
&self.region_name
}
#[inline]
pub fn set_region_name<S>(&mut self, region_name: S)
where
S: PopulateString,
{
region_name.populate(&mut self.region_name);
}
pub fn set_speedrun_com_variable<N, V>(&mut self, name: N, value: V)
where
N: PopulateString,
V: PopulateString,
{
let entry = self.speedrun_com_variables.entry(name).or_default();
value.populate(entry);
}
pub fn remove_speedrun_com_variable(&mut self, name: &str) {
self.speedrun_com_variables.shift_remove(name);
}
pub fn speedrun_com_variables(&self) -> Iter<'_, String> {
self.speedrun_com_variables.iter()
}
pub fn custom_variable(&self, name: &str) -> Option<&CustomVariable> {
self.custom_variables.get(name)
}
pub fn custom_variable_value(&self, name: &str) -> Option<&str> {
Some(&self.custom_variable(name)?.value)
}
pub fn custom_variable_mut<S>(&mut self, name: S) -> &mut CustomVariable
where
S: PopulateString,
{
self.custom_variables.entry(name).or_default()
}
pub fn remove_custom_variable(&mut self, name: &str) {
self.custom_variables.shift_remove(name);
}
pub fn custom_variables(&self) -> Iter<'_, CustomVariable> {
self.custom_variables.iter()
}
pub fn clear(&mut self) {
self.run_id.clear();
self.platform_name.clear();
self.region_name.clear();
self.uses_emulator = false;
self.speedrun_com_variables.clear();
self.custom_variables.clear();
}
}