use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use crate::builtins;
use crate::classes;
use crate::dialect::Dialect;
pub const SURFACE_V2: &str = include_str!("../reference/ahk-v2.0.19.surface.tsv");
pub const PENDING_V2: &str = include_str!("../reference/ahk-v2.0.19.pending.tsv");
pub const SURFACE_V1: &str = include_str!("../reference/ahk-v1.1.37.02.surface.tsv");
pub const PENDING_V1: &str = include_str!("../reference/ahk-v1.1.37.02.pending.tsv");
const NOT_CARRIED: &str = "this runtime does not carry that name yet.";
const NO_CLASS: &str = "this runtime does not carry that class yet.";
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Kind {
Command,
Function,
Variable,
Method,
Class,
Directive,
}
impl Kind {
pub fn as_str(&self) -> &'static str {
match self {
Kind::Command => "command",
Kind::Function => "function",
Kind::Variable => "variable",
Kind::Method => "method",
Kind::Class => "class",
Kind::Directive => "directive",
}
}
fn of(text: &str) -> Kind {
match text {
"command" => Kind::Command,
"function" => Kind::Function,
"variable" => Kind::Variable,
"class" => Kind::Class,
"directive" => Kind::Directive,
_ => Kind::Method,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum State {
Value,
Refusal(&'static str),
Pending(&'static str),
}
impl State {
pub fn text(&self) -> String {
match self {
State::Value => "value".to_string(),
State::Refusal(reason) => format!("refusal: {reason}"),
State::Pending(reason) => format!("pending: {reason}"),
}
}
}
#[derive(Clone, Debug)]
pub struct Entry {
pub kind: Kind,
pub class: &'static str,
pub name: &'static str,
pub state: State,
}
impl Entry {
pub fn full_name(&self) -> String {
if self.class.is_empty() {
self.name.to_string()
} else {
format!("{}.{}", self.class, self.name)
}
}
pub fn line(&self) -> String {
format!(
"{} {} {}",
self.full_name(),
self.kind.as_str(),
self.state.text()
)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Summary {
pub dialect: &'static str,
pub total: usize,
pub values: usize,
pub refusals: usize,
pub pending: usize,
}
impl Summary {
pub fn text(&self) -> String {
format!(
"the surface of {} has {} entries: {} answer with a value, {} are refused, {} are pending.",
self.dialect, self.total, self.values, self.refusals, self.pending
)
}
}
pub fn entries() -> Vec<Entry> {
entries_of(crate::dialect::v2())
}
pub fn entries_of(dialect: &'static dyn Dialect) -> Vec<Entry> {
let (surface, pending) = dialect.tables();
let extension = dialect.the_extension_rows();
rows(surface)
.into_iter()
.chain(rows(extension))
.map(|row| {
let kind = Kind::of(row[0]);
let (class, name) = (row[1], row[2]);
Entry {
kind,
class,
name,
state: state_of(dialect, kind, class, name, pending),
}
})
.collect()
}
pub fn summary(entries: &[Entry]) -> Summary {
summary_of(entries, crate::dialect::v2())
}
pub fn summary_of(entries: &[Entry], dialect: &'static dyn Dialect) -> Summary {
let mut summary = Summary {
dialect: dialect.name(),
total: entries.len(),
values: 0,
refusals: 0,
pending: 0,
};
for entry in entries {
match entry.state {
State::Value => summary.values += 1,
State::Refusal(_) => summary.refusals += 1,
State::Pending(_) => summary.pending += 1,
}
}
summary
}
fn rows(table: &'static str) -> Vec<Vec<&'static str>> {
let mut out: Vec<Vec<&'static str>> = Vec::new();
for line in table.lines() {
let line = line.trim_end_matches('\r');
if line.is_empty() || line.starts_with('#') {
continue;
}
let fields: Vec<&'static str> = line.split('\t').collect();
if fields[0] == "kind" {
continue;
}
out.push(fields);
}
out
}
fn state_of(
dialect: &'static dyn Dialect,
kind: Kind,
class: &'static str,
name: &'static str,
pending: &'static str,
) -> State {
match kind {
Kind::Command | Kind::Function => match the_builtin_of(dialect, name) {
None => State::Pending(reason_of_pending(pending, kind, class, name)),
Some(builtin) => match builtins::refusal_of_the(builtin) {
None => State::Value,
Some(reason) => State::Refusal(reason),
},
},
Kind::Variable => {
if !dialect.carries_variable(name) && !dialect.carries_the_variable_of_the_extension(name)
{
return State::Pending(reason_of_pending(pending, kind, class, name));
}
if dialect.carries_the_variable_of_the_extension(name) {
return State::Value;
}
match dialect.refusal_of_variable(name) {
None => State::Value,
Some(reason) => State::Refusal(reason),
}
}
Kind::Class => {
if classes::is_a_name_of_a_class(name)
|| dialect.carries_the_class_of_the_extension(name)
{
return State::Value;
}
State::Pending(reason_of_pending(pending, kind, class, name))
}
Kind::Method => {
if classes::carries(class, name) || dialect.carries_the_member_of_the_extension(class, name)
{
return State::Value;
}
if !carries_class(class) {
return State::Refusal(NO_CLASS);
}
State::Pending(reason_of_pending(pending, kind, class, name))
}
Kind::Directive => {
let bare = name.strip_prefix('#').unwrap_or(name);
if !dialect.carries_directive(bare) {
return State::Pending(reason_of_pending(pending, kind, class, name));
}
match crate::runtime::refusal_of_directive(bare) {
None => State::Value,
Some(reason) => State::Refusal(reason),
}
}
}
}
fn the_builtin_of(dialect: &'static dyn Dialect, name: &str) -> Option<&'static builtins::Builtin> {
let wanted = name.to_lowercase();
dialect
.builtins()
.iter()
.find(|builtin| builtin.name.to_lowercase() == wanted)
.or_else(|| dialect.the_builtin_of_the_extension(&wanted))
}
fn carries_class(class: &str) -> bool {
!classes::names(class).is_empty()
}
fn reason_of_pending(
pending: &'static str,
kind: Kind,
class: &'static str,
name: &'static str,
) -> &'static str {
for row in rows(pending) {
if row[0] != kind.as_str() || row[1] != class || row[2] != name {
continue;
}
return match row[3] {
"windows" => "this is a name of Windows, and this desktop is not Windows.",
"hotkey" => "this runtime has no hotkeys yet.",
"class" => NO_CLASS,
"variable" => "this runtime does not carry that variable yet.",
_ => NOT_CARRIED,
};
}
NOT_CARRIED
}