Cleaned code up a bit

TODD:
make colour an enum
make unwrap_or_exit return the error
This commit is contained in:
2026-05-17 15:44:01 +01:00
parent 13049309b2
commit dd04399784
11 changed files with 54 additions and 71 deletions
+7 -25
View File
@@ -7,6 +7,7 @@ use crate::{
info,
Deserialize,
Serialize,
apply_mut,
Mutex,
Arc,
};
@@ -27,7 +28,7 @@ pub struct Character
torso_shape: String,
arm_shape: String,
leg_shape: String,
hair_color: String,
hair_color: String, // TODO RGB enum
clothing: Clothing,
}
#[derive(Debug,Deserialize,Serialize,Clone,Default)]
@@ -39,30 +40,11 @@ pub struct Clothing
shoes: String,
}
impl Character {
// Big ass ugly function because rust doesn't support referencing struct entries by string
pub fn set_field(&mut self, field: &str, value: &str) -> Result<(), ()> {
match field {
"name" => self.name = value.to_string(),
"gender" => self.gender = value.to_string(),
"eye_color" => self.eye_color = value.to_string(),
"pronoun_subject" => self.pronoun_subject = value.to_string(),
"pronoun_object" => self.pronoun_object = value.to_string(),
"pronoun_deppos" => self.pronoun_deppos = value.to_string(),
"pronoun_indpos" => self.pronoun_indpos = value.to_string(),
"pronoun_reflex" => self.pronoun_reflex = value.to_string(),
"head_shape" => self.head_shape = value.to_string(),
"hair_style" => self.hair_style = value.to_string(),
"torso_shape" => self.torso_shape = value.to_string(),
"arm_shape" => self.arm_shape = value.to_string(),
"leg_shape" => self.leg_shape = value.to_string(),
"hair_color" => self.hair_color = value.to_string(),
"clothing.top" => self.clothing.top = value.to_string(),
"clothing.bottom" => self.clothing.bottom = value.to_string(),
"clothing.shoes" => self.clothing.shoes = value.to_string(),
_ => return Err(()),
}
// Big ass ugly match case
pub fn set_field(&mut self, field: &str, value: &str) -> Result<(), String> {
let patch = format!("{{ \"{field}\": \"{value}\" }}");
apply_mut(self, patch)
.map_err(|_| "Invalid field".to_string())?;
Ok(())
}
}