use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use crate::runtime::{Runtime, RuntimeError, Value};
const NO_MENU: &str = "Target menu not found.";
const THE_FIRST_HANDLE: u64 = 0x1_0000;
pub(crate) struct TheItem {
pub(crate) name: String,
pub(crate) callback: String,
pub(crate) checked: bool,
pub(crate) enabled: bool,
}
pub(crate) struct TheMenu {
pub(crate) name: String,
pub(crate) handle: u64,
pub(crate) items: Vec<TheItem>,
pub(crate) default: Option<String>,
}
fn the_menus<'a>(rt: &'a mut Runtime<'_>) -> &'a mut Vec<TheMenu> {
rt.the_menus_of_the_v1()
}
#[allow(clippy::ptr_arg)]
fn the_menu_of_the_name<'a>(menus: &'a mut Vec<TheMenu>, name: &str) -> &'a mut TheMenu {
let wanted = name.to_lowercase();
if let Some(index) = menus
.iter()
.position(|menu| menu.name.to_lowercase() == wanted)
{
return &mut menus[index];
}
let handle = THE_FIRST_HANDLE + menus.len() as u64;
menus.push(TheMenu {
name: name.to_string(),
handle,
items: Vec::new(),
default: None,
});
let last = menus.len() - 1;
&mut menus[last]
}
#[allow(clippy::ptr_arg)]
fn the_menu_that_is_there<'a>(
menus: &'a mut Vec<TheMenu>,
name: &str,
) -> Option<&'a mut TheMenu> {
let wanted = name.to_lowercase();
menus
.iter_mut()
.find(|menu| menu.name.to_lowercase() == wanted)
}
fn the_failure_of_a_menu_that_is_not_there(line: u32) -> RuntimeError {
RuntimeError {
message: NO_MENU.to_string(),
line,
}
}
fn the_item<'a>(menu: &'a mut TheMenu, item: &str) -> Option<&'a mut TheItem> {
menu.items
.iter_mut()
.find(|held| held.name.eq_ignore_ascii_case(item))
}
pub(crate) fn add_item(
rt: &mut Runtime<'_>,
name: &str,
item: &str,
callback: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
menu.items.push(TheItem {
name: item.to_string(),
callback: callback.to_string(),
checked: false,
enabled: true,
});
let _ = line;
Ok(Value::Text(String::new()))
}
pub(crate) fn insert_item(
rt: &mut Runtime<'_>,
name: &str,
item: &str,
callback: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
let held = TheItem {
name: item.to_string(),
callback: callback.to_string(),
checked: false,
enabled: true,
};
let at = menu.items.len().saturating_sub(1);
menu.items.insert(at, held);
let _ = line;
Ok(Value::Text(String::new()))
}
pub(crate) fn delete_item(
rt: &mut Runtime<'_>,
name: &str,
item: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
let before = menu.items.len();
menu.items
.retain(|held| !held.name.eq_ignore_ascii_case(item));
if menu.items.len() == before {
return Err(the_failure_of_a_menu_that_is_not_there(line));
}
Ok(Value::Text(String::new()))
}
pub(crate) fn delete_all(
rt: &mut Runtime<'_>,
name: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
if menu.items.is_empty() {
return Err(the_failure_of_a_menu_that_is_not_there(line));
}
menu.items.clear();
menu.default = None;
Ok(Value::Text(String::new()))
}
pub(crate) fn set_enabled(
rt: &mut Runtime<'_>,
name: &str,
item: &str,
on: bool,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
let Some(held) = the_item(menu, item) else {
return Err(the_failure_of_a_menu_that_is_not_there(line));
};
held.enabled = on;
Ok(Value::Text(String::new()))
}
pub(crate) fn toggle_enabled(
rt: &mut Runtime<'_>,
name: &str,
item: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
let Some(held) = the_item(menu, item) else {
return Err(the_failure_of_a_menu_that_is_not_there(line));
};
held.enabled = !held.enabled;
Ok(Value::Text(String::new()))
}
pub(crate) fn set_checked(
rt: &mut Runtime<'_>,
name: &str,
item: &str,
checked: bool,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
let Some(held) = the_item(menu, item) else {
return Err(the_failure_of_a_menu_that_is_not_there(line));
};
held.checked = checked;
Ok(Value::Text(String::new()))
}
pub(crate) fn toggle_checked(
rt: &mut Runtime<'_>,
name: &str,
item: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
let Some(held) = the_item(menu, item) else {
return Err(the_failure_of_a_menu_that_is_not_there(line));
};
held.checked = !held.checked;
Ok(Value::Text(String::new()))
}
pub(crate) fn set_default(
rt: &mut Runtime<'_>,
name: &str,
item: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
if the_item(menu, item).is_none() {
return Err(the_failure_of_a_menu_that_is_not_there(line));
}
menu.default = Some(item.to_string());
Ok(Value::Text(String::new()))
}
pub(crate) fn no_default(
rt: &mut Runtime<'_>,
name: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
menu.default = None;
let _ = line;
Ok(Value::Text(String::new()))
}
pub(crate) fn rename_item(
rt: &mut Runtime<'_>,
name: &str,
old: &str,
new: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
let menu = the_menu_of_the_name(menus, name);
let Some(held) = the_item(menu, old) else {
return Err(the_failure_of_a_menu_that_is_not_there(line));
};
held.name = new.to_string();
Ok(Value::Text(String::new()))
}
pub(crate) fn the_handle_of(
rt: &mut Runtime<'_>,
name: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let menus = the_menus(rt);
for menu in menus.iter() {
if menu.name.eq_ignore_ascii_case(name) {
return Ok(Value::Int(menu.handle as i64));
}
}
Err(the_failure_of_a_menu_that_is_not_there(line))
}
pub(crate) fn the_name_of(
rt: &mut Runtime<'_>,
handle: &str,
line: u32,
) -> Result<Value, RuntimeError> {
let wanted = handle.trim();
let menus = the_menus(rt);
for menu in menus.iter() {
if wanted == menu.handle.to_string() {
return Ok(Value::Text(menu.name.clone()));
}
}
Err(the_failure_of_a_menu_that_is_not_there(line))
}
pub(crate) fn the_items_of_the_menu(rt: &mut Runtime<'_>, name: &str) -> String {
let menus = the_menus(rt);
let Some(menu) = the_menu_that_is_there(menus, name) else {
return String::from("[]");
};
let mut json = String::from("[");
for (index, item) in menu.items.iter().enumerate() {
if index > 0 {
json.push(',');
}
json.push('[');
json.push_str(&the_json_text(&item.name));
json.push_str(&format!(",{},{}", item.checked, item.enabled));
if !item.callback.is_empty() {
json.push(',');
json.push_str(&the_json_text(&item.callback));
}
json.push(']');
}
json.push(']');
json
}
fn the_json_text(text: &str) -> String {
let mut out = String::from("\"");
for character in text.chars() {
match character {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
other => out.push(other),
}
}
out.push('"');
out
}