From fbb5a4b503f7ffe300409583e7e4522d62406995 Mon Sep 17 00:00:00 2001 From: deadvey Date: Mon, 18 May 2026 14:18:25 +0100 Subject: [PATCH] fixed clippy lints --- server/src/main.rs | 9 +---- server/src/parsing.rs | 76 +++++++++++++++++++++--------------------- server/src/tokenise.rs | 21 +++++++----- 3 files changed, 52 insertions(+), 54 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index 4f52cae..32ff07b 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -103,14 +103,7 @@ async fn main() error!("Unable to read story file to string: {err}"); exit(14); }); - let space_seperated: Vec<&str> = file_contents - .split_whitespace() - .collect(); - if ! space_seperated.contains(&"END") // TODO remove - { - warn!("No END statement, story may exit unexpectedly"); - } - let (tokens, labels,_) = tokenise::tokenise(&space_seperated, 0) + let (tokens, labels) = tokenise::tokenise(&file_contents) .unwrap_or_exit("Unable to tokenise data", 15); debug!("{tokens:?}\n{labels:?}"); let data_clone2 = Arc::clone(&data_to_send); diff --git a/server/src/parsing.rs b/server/src/parsing.rs index 098d8f9..d8e3793 100644 --- a/server/src/parsing.rs +++ b/server/src/parsing.rs @@ -41,14 +41,11 @@ pub fn token_parse( // Get the next token let token: String = match tokens.get(index) { - Some(tokenise::Token::Keyword(s)) => s.to_string(), + Some(tokenise::Token::Keyword(s)) => s.clone(), // Ignore closing braces and jump over opening brace blocks Some(tokenise::Token::Bracket((bracket,new_index))) => { - if bracket == &tokenise::Bracket::Closing - { - index += 1; - } + if bracket == &tokenise::Bracket::Closing { index += 1; } else { warn!("Unexpected brace block, jumping over..."); @@ -59,7 +56,7 @@ pub fn token_parse( // Handle a character Some(tokenise::Token::Character(character_name)) => // TODO add support for narrator { - index = match character_parse::character_parse(index+1,tokens,character_name.to_string(),characters,data_to_send) + index = match character_parse::character_parse(index+1,tokens,character_name.clone(),characters,data_to_send) { Ok(increment) => increment, Err((err,increment)) => @@ -89,32 +86,8 @@ pub fn token_parse( }, "choice" => { - let mut next_token: String = token; - let mut choices: Vec = Vec::new(); - let mut choice_indeces: Vec = Vec::new(); - while next_token == "or" || next_token == "choice" - { - index += 1; - choices.push - ( - tokenise::get_string_token(tokens, index)? - ); - index += 1; - choice_indeces.push(index+1); - index = match tokenise::get_closing_index(tokens,index) - { - Ok(new_index) => new_index + 1, - Err(_) => break, - }; - next_token = match tokenise::get_keyword_token(tokens, index) - { - Ok(string) => string, - Err(_) => break, - } - } - debug!("{choices:?}"); + let choice_indeces = choice_parse(tokens, index, data_to_send)?; debug!("{choice_indeces:?}"); - api::modify_data(data_to_send, "choice".to_string(), String::new(), String::new(), choices); if rx.recv().is_err() { warn!("Error sending choices to client"); } let choice = match rx.recv() { @@ -141,14 +114,11 @@ pub fn token_parse( { index += 1; let label = tokenise::get_keyword_token(tokens, index)?; - index = match labels.get(&label) + index = if let Some(label_index) = labels.get(&label) { *label_index } + else { - Some(label_index) => *label_index, - None => - { - warn!("Label {label} does not exist"); - index - } + warn!("Label {label} does not exist"); + index + 1 }; debug!("Jumping to {index}"); continue 'parse_loop @@ -165,3 +135,33 @@ pub fn token_parse( } } } + +fn choice_parse(tokens: &[tokenise::Token], mut index: usize, data_to_send: &Arc>,) +-> Result, String> +{ + let mut next_token: String = "choice".to_string(); + let mut choices: Vec = Vec::new(); + let mut choice_indeces: Vec = Vec::new(); + while next_token == "or" || next_token == "choice" + { + index += 1; + choices.push + ( + tokenise::get_string_token(tokens, index)? + ); + index += 1; + choice_indeces.push(index+1); + index = match tokenise::get_closing_index(tokens,index) + { + Ok(new_index) => new_index + 1, + Err(_) => break, + }; + next_token = match tokenise::get_keyword_token(tokens, index) + { + Ok(string) => string, + Err(_) => break, + } + }; + api::modify_data(data_to_send, "choice".to_string(), String::new(), String::new(), choices); + Ok(choice_indeces) +} diff --git a/server/src/tokenise.rs b/server/src/tokenise.rs index bb029b9..f2f05f8 100644 --- a/server/src/tokenise.rs +++ b/server/src/tokenise.rs @@ -1,6 +1,5 @@ use crate:: { - exit, HashMap, }; @@ -13,7 +12,7 @@ pub enum Token Bracket((Bracket,usize)), // Stores the index of the matching deliminator Character(String), } -#[derive(Debug,Clone,PartialEq)] +#[derive(Debug,Clone,PartialEq, Eq)] pub enum Bracket { Opening, @@ -51,12 +50,16 @@ pub fn get_closing_index(tokens: &[Token], index: usize) // Tokenise the story to Vec // It can contain sub-objects (using recursive calls) // TODO check for END -pub fn tokenise(space_seperated: &Vec<&str>, mut index: usize) --> Result<(Vec,HashMap,usize),String> +pub fn tokenise(file_contents: &str) +-> Result<(Vec,HashMap),String> { + let space_seperated: Vec<&str> = file_contents + .split_whitespace() + .collect(); let mut tokenised_data: Vec = Vec::new(); let mut labels: HashMap = HashMap::new(); let mut bracket_stack: Vec = Vec::new(); + let mut index: usize = 0; while index < space_seperated.len() { let mut item = space_seperated[index].to_string(); @@ -70,8 +73,8 @@ pub fn tokenise(space_seperated: &Vec<&str>, mut index: usize) // Strings else if item.starts_with('"') // TODO support ' { - let Some((new_index, new_item)) = tokenise_string(space_seperated, index) - else { exit(1) }; + let Some((new_index, new_item)) = tokenise_string(&space_seperated, index) + else { return Err("File unexpectedly ended: No closing quote".to_string()) }; index = new_index; item = new_item; tokenised_data.push(Token::String(item)); @@ -96,14 +99,16 @@ pub fn tokenise(space_seperated: &Vec<&str>, mut index: usize) } else if item == "}" { - let prev_index = bracket_stack.pop().unwrap(); // TODO eh + let prev_index = bracket_stack.pop() + .ok_or_else(|| "Unexpected closing brace".to_string())?; tokenised_data[prev_index] = Token::Bracket((Bracket::Opening, tokenised_data.len())); tokenised_data.push(Token::Bracket((Bracket::Closing,prev_index))); } else { tokenised_data.push(Token::Keyword(item)); } index += 1; } - Ok((tokenised_data, labels, 0)) + if !bracket_stack.is_empty() { return Err("File unexpectedly ended: No closing brace".to_string()) } + Ok((tokenised_data, labels)) } fn tokenise_string(space_seperated: &[&str], mut index: usize)