botserver/src/basic/keywords/for_next.rs
Rodrigo Rodriguez (Pragmatismo) 5da86bbef2 Fix clippy warnings: match arms, async/await, Debug impls, formatting
- Fix match arms with identical bodies by consolidating patterns
- Fix case-insensitive file extension comparisons using eq_ignore_ascii_case
- Fix unnecessary Debug formatting in log/format macros
- Fix clone_from usage instead of clone assignment
- Fix let...else patterns where appropriate
- Fix format! append to String using write! macro
- Fix unwrap_or with function calls to use unwrap_or_else
- Add missing fields to manual Debug implementations
- Fix duplicate code in if blocks
- Add type aliases for complex types
- Rename struct fields to avoid common prefixes
- Various other clippy warning fixes

Note: Some 'unused async' warnings remain for functions that are
called with .await but don't contain await internally - these are
kept async for API compatibility.
2025-12-26 08:59:25 -03:00

62 lines
2.3 KiB
Rust

use crate::shared::models::UserSession;
use crate::shared::state::AppState;
use rhai::Dynamic;
use rhai::Engine;
pub fn for_keyword(_state: &AppState, _user: UserSession, engine: &mut Engine) {
engine
.register_custom_syntax(["EXIT", "FOR"], false, |_context, _inputs| {
Err("EXIT FOR".into())
})
.unwrap();
engine
.register_custom_syntax(
[
"FOR", "EACH", "$ident$", "IN", "$expr$", "$block$", "NEXT", "$ident$",
],
true,
|context, inputs| {
let loop_var = inputs[0].get_string_value().unwrap().to_lowercase();
let next_var = inputs[3].get_string_value().unwrap().to_lowercase();
if loop_var != next_var {
return Err(format!(
"NEXT variable '{}' doesn't match FOR EACH variable '{}'",
next_var, loop_var
)
.into());
}
let collection = context.eval_expression_tree(&inputs[1])?;
let ccc = collection.clone();
let array = match collection.into_array() {
Ok(arr) => arr,
Err(err) => {
return Err(format!(
"foreach expected array, got {}: {}",
ccc.type_name(),
err
)
.into());
}
};
let block = &inputs[2];
let orig_len = context.scope().len();
for item in array {
context.scope_mut().push(&loop_var, item);
match context.eval_expression_tree(block) {
Ok(_) => (),
Err(e) if e.to_string() == "EXIT FOR" => {
context.scope_mut().rewind(orig_len);
break;
}
Err(e) => {
context.scope_mut().rewind(orig_len);
return Err(e);
}
}
context.scope_mut().rewind(orig_len);
}
Ok(Dynamic::UNIT)
},
)
.unwrap();
}