From 96584db0a3d51be5f9dd260e3372fad8f04d8f84 Mon Sep 17 00:00:00 2001 From: hut Date: Wed, 20 Mar 2024 04:54:39 +0100 Subject: [PATCH] populate ParserState struct --- src/world.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/src/world.rs b/src/world.rs index d23bb65..0d98f84 100644 --- a/src/world.rs +++ b/src/world.rs @@ -314,12 +314,39 @@ pub fn setup( }); } +struct ParserState { + chat: String, + name: String, + delay: f64, + text: String, + level: String, + label: String, + goto: String, + is_choice: bool, +} +impl Default for ParserState { + fn default() -> Self { + Self { + chat: "".to_string(), + name: "".to_string(), + delay: 0.0, + text: "".to_string(), + level: "".to_string(), + label: "".to_string(), + goto: "".to_string(), + is_choice: false, + } + } +} + pub fn load_defs( ) { let re1 = Regex::new(r"^\s*([a-z]+)\s+(.*)$").unwrap(); let re2 = Regex::new("\"([^\"]*)\"|([0-9]+(?:\\.[0-9]+)?)").unwrap(); let defs_string = include_str!("defs.txt"); let mut lines = defs_string.lines(); + let mut state = ParserState::default(); + let mut line_nr = -1; while let Some(line) = lines.next() { line_nr += 1; @@ -331,7 +358,6 @@ pub fn load_defs( continue; } let command = caps.unwrap().get(1).unwrap().as_str(); - info!(command); let mut parts: Vec<&str> = Vec::new(); parts.push(command); @@ -346,25 +372,46 @@ pub fn load_defs( match parts.as_slice() { ["chat", chat_name] => { - info!("Registering chat: {}", chat_name); + debug!("Registering chat: {}", chat_name); + state.name = chat_name.to_string(); + } + ["name", name] => { + debug!("Registering name: {}", name); + state.name = name.to_string(); } ["msg", sleep, text] => { info!("Registering message (sleep={}): {}", sleep, text); + // TODO: write previous message/choice + if let Ok(sleep_float) = sleep.parse::() { + state.delay = sleep_float; + state.text = text.to_string(); + } else { + error!("The 'sleep' value for this message is not a float: {}", line); + continue; + } } ["choice", sleep, text] => { info!("Registering choice (sleep={}): {}", sleep, text); + // TODO: write previous message/choice + if let Ok(sleep_float) = sleep.parse::() { + state.delay = sleep_float; + state.text = text.to_string(); + } else { + error!("The 'sleep' value for this message is not a float: {}", line); + continue; + } } ["goto", label] => { - info!("Registering goto: {}", label); + debug!("Registering goto: {}", label); + state.goto = label.to_string(); } ["label", label] => { - info!("Registering label: {}", label); - } - ["name", name] => { - info!("Registering name: {}", name); + debug!("Registering label: {}", label); + state.label = label.to_string(); } ["lvl", level] => { info!("Registering level: {}", level); + state.level = level.to_string(); } _ => { error!("No match for [{}]", parts.join(","));