populate ParserState struct

This commit is contained in:
yuni 2024-03-20 04:54:39 +01:00
parent 4394905d17
commit 96584db0a3

View file

@ -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( pub fn load_defs(
) { ) {
let re1 = Regex::new(r"^\s*([a-z]+)\s+(.*)$").unwrap(); let re1 = Regex::new(r"^\s*([a-z]+)\s+(.*)$").unwrap();
let re2 = Regex::new("\"([^\"]*)\"|([0-9]+(?:\\.[0-9]+)?)").unwrap(); let re2 = Regex::new("\"([^\"]*)\"|([0-9]+(?:\\.[0-9]+)?)").unwrap();
let defs_string = include_str!("defs.txt"); let defs_string = include_str!("defs.txt");
let mut lines = defs_string.lines(); let mut lines = defs_string.lines();
let mut state = ParserState::default();
let mut line_nr = -1; let mut line_nr = -1;
while let Some(line) = lines.next() { while let Some(line) = lines.next() {
line_nr += 1; line_nr += 1;
@ -331,7 +358,6 @@ pub fn load_defs(
continue; continue;
} }
let command = caps.unwrap().get(1).unwrap().as_str(); let command = caps.unwrap().get(1).unwrap().as_str();
info!(command);
let mut parts: Vec<&str> = Vec::new(); let mut parts: Vec<&str> = Vec::new();
parts.push(command); parts.push(command);
@ -346,25 +372,46 @@ pub fn load_defs(
match parts.as_slice() { match parts.as_slice() {
["chat", chat_name] => { ["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] => { ["msg", sleep, text] => {
info!("Registering message (sleep={}): {}", sleep, text); info!("Registering message (sleep={}): {}", sleep, text);
// TODO: write previous message/choice
if let Ok(sleep_float) = sleep.parse::<f64>() {
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] => { ["choice", sleep, text] => {
info!("Registering choice (sleep={}): {}", sleep, text); info!("Registering choice (sleep={}): {}", sleep, text);
// TODO: write previous message/choice
if let Ok(sleep_float) = sleep.parse::<f64>() {
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] => { ["goto", label] => {
info!("Registering goto: {}", label); debug!("Registering goto: {}", label);
state.goto = label.to_string();
} }
["label", label] => { ["label", label] => {
info!("Registering label: {}", label); debug!("Registering label: {}", label);
} state.label = label.to_string();
["name", name] => {
info!("Registering name: {}", name);
} }
["lvl", level] => { ["lvl", level] => {
info!("Registering level: {}", level); info!("Registering level: {}", level);
state.level = level.to_string();
} }
_ => { _ => {
error!("No match for [{}]", parts.join(",")); error!("No match for [{}]", parts.join(","));