use crate::{ ZipArchive, Read, File, HashMap, debug, info, Deserialize, Serialize, apply_mut, Mutex, Arc, }; #[derive(Debug, Deserialize, Serialize, Clone, Default)] #[serde(default)] pub struct Character { name: String, gender: String, eye_color: Colour, hair_color: Colour, skin_color: Colour, pronoun_subject: String, pronoun_object: String, pronoun_deppos: String, pronoun_indpos: String, pronoun_reflex: String, head_shape: String, hair_style: String, torso_shape: String, arm_shape: String, leg_shape: String, clothing: Clothing, } #[derive(Debug,Deserialize,Serialize,Clone,Default)] #[serde(default)] pub struct Clothing { top: String, bottom: String, shoes: String, } #[derive(Debug,Deserialize,Serialize,Clone,Default)] #[serde(default)] pub struct Colour { red: u8, green: u8, blue: u8, } impl Character { // 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(()) } } pub fn character_parse(archive: &mut ZipArchive) -> Result>>,String> { // Get the JSON file to a string let mut characters_file = archive.by_name("characters.json") .map_err (|err| format!("Unable to read character file: {err}"))?; let mut file_contents = String::new(); characters_file.read_to_string(&mut file_contents) .map_err (|err| format!("Unable to read character file to string: {err}"))?; // Serialise this to a HashMap let characters: HashMap = serde_json::from_str(&file_contents) .map_err (|err| format!("Invalid JSON in characters.json: {err}"))?; info!("Parsed characters from characters.json"); debug!("{characters:?}"); Ok(Arc::new(Mutex::new(characters))) }