From 59f32e975ba7276733e19c7ac25d3827f127beea Mon Sep 17 00:00:00 2001 From: deadvey Date: Tue, 26 May 2026 11:54:53 +0100 Subject: [PATCH] clippy lints and moved the operator matchbox to it's own function --- server/Cargo.lock | 2 +- server/Cargo.toml | 2 +- server/src/parsing/identifier_parse.rs | 26 ++++++++++++-------------- server/src/parsing/keyword_parse.rs | 23 ++++++++--------------- server/src/tokenise.rs | 20 ++++++++------------ 5 files changed, 30 insertions(+), 43 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index 870c4ab..18f0a8a 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -457,7 +457,7 @@ dependencies = [ [[package]] name = "happening-server" -version = "0.0.2" +version = "0.0.3" dependencies = [ "env_logger", "log", diff --git a/server/Cargo.toml b/server/Cargo.toml index f01156e..ec40ec5 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "happening-server" -version = "0.0.2" +version = "0.0.3" edition = "2024" license = "GPL-3" repository = "https://git.javalsai.tuxcord.net/deadvey/happening/" diff --git a/server/src/parsing/identifier_parse.rs b/server/src/parsing/identifier_parse.rs index ada3b1c..2c0cea4 100644 --- a/server/src/parsing/identifier_parse.rs +++ b/server/src/parsing/identifier_parse.rs @@ -18,20 +18,18 @@ pub fn identifier_parse ) -> Result<(usize,bool),(String,usize)> { let mut sum_index: usize = index; - if ! variables.contains_key(identifier) - { - variables.insert(identifier.to_string(), tokenise::Value::Null); - } - let current = variables.get(identifier).unwrap().clone(); + let current = variables + .entry(identifier.clone()) + .or_insert(tokenise::Value::Null) + .clone(); let operator = tokenise::get_operator_token(tokens, sum_index) .map_err(|err| (err, sum_index))?; sum_index += 1; - let mut result = true; - match tokenise::get_value_token(tokens, sum_index) + let result: bool = match tokenise::get_value_token(tokens, sum_index) { - Ok(value) => result = operator_match(¤t,value,operator,identifier,variables), + Ok(value) => operator_match(¤t,value,operator,identifier,variables), Err(_) => todo!(), - } + }; sum_index += 1; @@ -54,7 +52,7 @@ fn operator_match // Changing a value tokenise::Operator::Assignment => { - variables.insert(identifier.to_string(), value); + variables.insert(identifier.to_owned(), value); } , tokenise::Operator::Add => { @@ -62,20 +60,20 @@ fn operator_match { (tokenise::Value::Integer(int1),tokenise::Value::Integer(int2)) => tokenise::Value::Integer(int1 + int2), (tokenise::Value::String(str1),tokenise::Value::String(str2)) => tokenise::Value::String(format!("{str1}{str2}")), - _ => value.clone(), // otherwise invalid + _ => value, // otherwise invalid }; - variables.insert(identifier.to_string(), result); + variables.insert(identifier.to_owned(), result); }, tokenise::Operator::Sub => { let result: tokenise::Value = match (value.clone(), current) { (tokenise::Value::Integer(int1),tokenise::Value::Integer(int2)) => tokenise::Value::Integer(int2 - int1), - _ => value.clone(), // otherwise invalid + _ => value, // otherwise invalid }; - variables.insert(identifier.to_string(), result); + variables.insert(identifier.to_owned(), result); }, // TODO choice assignment // TODO input assignment diff --git a/server/src/parsing/keyword_parse.rs b/server/src/parsing/keyword_parse.rs index 5edd9a8..545a170 100644 --- a/server/src/parsing/keyword_parse.rs +++ b/server/src/parsing/keyword_parse.rs @@ -47,14 +47,13 @@ pub fn keyword_parse( { // TODO can this go in a function? let mut keyword = "if".to_string(); - while keyword == "if".to_string() || keyword == "elif".to_string() || keyword == "else".to_string() // TODO less beefy?? + while keyword == "if" || keyword == "elif" || keyword == "else" // TODO less beefy?? { index += 1; let mut result: bool = true; if keyword != "else" { - let identifier = tokenise::get_identifier_token(tokens, index) - .map(|err| err)?; + let identifier = tokenise::get_identifier_token(tokens, index)?; (index,result) = match identifier_parse::identifier_parse(index+1, &identifier, tokens, variables) { Ok((increment, result)) => (increment,result), @@ -66,28 +65,22 @@ pub fn keyword_parse( } } if result { index += 1; break; } - else + index = tokenise::get_closing_index(tokens,index)?; + index += 1; + keyword = match tokenise::get_keyword_token(tokens,index) { - index = tokenise::get_closing_index(tokens, index) - .map(|err| err)?; - index += 1; - keyword = match tokenise::get_keyword_token(tokens,index) - { - Ok(keyword) => keyword, - Err(_) => break, - } + Ok(keyword) => keyword, + Err(_) => break, } } }, "else" => { index += 1; - index = tokenise::get_closing_index(tokens, index) - .map(|err| err)?; + index = tokenise::get_closing_index(tokens, index)?; }, "elif" => { - let current = "elif".to_string(); loop { index += 1; diff --git a/server/src/tokenise.rs b/server/src/tokenise.rs index 61afbbb..b622ad5 100644 --- a/server/src/tokenise.rs +++ b/server/src/tokenise.rs @@ -13,7 +13,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 Value { String(String), @@ -142,15 +142,11 @@ pub fn tokenise(file_contents: &str) "<=" => Some(Operator::Comparison(Comparison::LessOrEqual)), _ => None, }; - match op + if let Some(op) = op { - Some(op) => - { - tokenised_data.push(Token::Operator(op)); - index += 1; - continue // skip the rest of this loop - }, - None => (), + tokenised_data.push(Token::Operator(op)); + index += 1; + continue // skip the rest of this loop } // Characters if item.starts_with('@') @@ -168,9 +164,9 @@ pub fn tokenise(file_contents: &str) item = new_item; tokenised_data.push(Token::Value(Value::String(item))); } - else if item.parse::().is_ok() + else if let Ok(value) = item.parse::() { - tokenised_data.push(Token::Value(Value::Integer(item.parse::().unwrap()))); // unwrap is fine here because I checked + tokenised_data.push(Token::Value(Value::Integer(value))); // unwrap is fine here because I checked } else if item == "true" { @@ -181,7 +177,7 @@ pub fn tokenise(file_contents: &str) tokenised_data.push(Token::Value(Value::Bool(false))); } // variable/identifier - else if item.starts_with("$") + else if item.starts_with('$') { let mut chars = item.chars(); chars.next();