mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 14:30:10 +01:00
refactor: separate rule parser to another repo
This commit is contained in:
parent
becb692db0
commit
006e198240
6 changed files with 13 additions and 485 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,3 +1,2 @@
|
|||
/target
|
||||
Cargo.lock
|
||||
rule_parser/target
|
||||
|
|
|
|||
13
Cargo.toml
13
Cargo.toml
|
|
@ -3,9 +3,20 @@ name = "pay-respects"
|
|||
version = "0.4.13"
|
||||
edition = "2021"
|
||||
|
||||
# for crates.io
|
||||
description = "Terminal command correction, alternative to thefuck written in Rust"
|
||||
repository = "https://github.com/iffse/pay-respects-parser"
|
||||
keywords = ["cli", "terminal", "utility", "shell"]
|
||||
categories = ["command-line-utilities"]
|
||||
license = "AGPL-3.0"
|
||||
include = [
|
||||
"**/*.rs",
|
||||
"**/*.toml",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
colored = "2.0"
|
||||
rule_parser = { path = "rule_parser" }
|
||||
pay-respects-parser = "0.2.2"
|
||||
regex-lite = "0.1"
|
||||
|
||||
[profile.release]
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
[package]
|
||||
name = "rule_parser"
|
||||
version = "0.2.2"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = "1.0"
|
||||
quote = "1.0"
|
||||
proc-macro2 = "1.0"
|
||||
toml = "0.7"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
|
@ -1,203 +0,0 @@
|
|||
use std::path::Path;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
|
||||
mod replaces;
|
||||
|
||||
#[proc_macro]
|
||||
pub fn parse_rules(input: TokenStream) -> TokenStream {
|
||||
let directory = input.to_string().trim_matches('"').to_owned();
|
||||
let rules = get_rules(directory);
|
||||
|
||||
gen_match_rules(rules)
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct Rule {
|
||||
command: String,
|
||||
match_err: Vec<MatchError>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct MatchError {
|
||||
pattern: Vec<String>,
|
||||
suggest: Vec<String>,
|
||||
}
|
||||
|
||||
fn get_rules(directory: String) -> Vec<Rule> {
|
||||
let files = std::fs::read_dir(directory).expect("Failed to read directory.");
|
||||
|
||||
let mut rules = Vec::new();
|
||||
for file in files {
|
||||
let file = file.expect("Failed to read file.");
|
||||
let path = file.path();
|
||||
let path = path.to_str().expect("Failed to convert path to string.");
|
||||
|
||||
let rule_file = parse_file(Path::new(path));
|
||||
rules.push(rule_file);
|
||||
}
|
||||
rules
|
||||
}
|
||||
|
||||
fn gen_match_rules(rules: Vec<Rule>) -> TokenStream {
|
||||
let command = rules
|
||||
.iter()
|
||||
.map(|x| x.command.to_owned())
|
||||
.collect::<Vec<String>>();
|
||||
let command_matches = rules
|
||||
.iter()
|
||||
.map(|x| {
|
||||
x.match_err
|
||||
.iter()
|
||||
.map(|x| {
|
||||
let pattern = x
|
||||
.pattern
|
||||
.iter()
|
||||
.map(|x| x.to_lowercase())
|
||||
.collect::<Vec<String>>();
|
||||
let suggests = x
|
||||
.suggest
|
||||
.iter()
|
||||
.map(|x| x.to_string())
|
||||
.collect::<Vec<String>>();
|
||||
(pattern, suggests)
|
||||
})
|
||||
.collect::<Vec<(Vec<String>, Vec<String>)>>()
|
||||
})
|
||||
.collect::<Vec<Vec<(Vec<String>, Vec<String>)>>>();
|
||||
|
||||
let mut matches_tokens = Vec::new();
|
||||
|
||||
for match_err in command_matches {
|
||||
let mut suggestion_tokens = Vec::new();
|
||||
let mut patterns_tokens = Vec::new();
|
||||
for (pattern, suggests) in match_err {
|
||||
// let mut match_condition = Vec::new();
|
||||
let mut pattern_suggestions = Vec::new();
|
||||
for suggest in suggests {
|
||||
let (suggestion_no_condition, conditions) = parse_conditions(&suggest);
|
||||
let suggest = eval_suggest(&suggestion_no_condition);
|
||||
let suggestion = quote! {
|
||||
if #(#conditions)&&* {
|
||||
#suggest;
|
||||
};
|
||||
};
|
||||
pattern_suggestions.push(suggestion);
|
||||
};
|
||||
let match_tokens = quote! {
|
||||
#(#pattern_suggestions)*
|
||||
};
|
||||
|
||||
suggestion_tokens.push(match_tokens);
|
||||
|
||||
let string_patterns = pattern.join("\", \"");
|
||||
let string_patterns: TokenStream2 = format!("[\"{}\"]", string_patterns).parse().unwrap();
|
||||
patterns_tokens.push(string_patterns);
|
||||
}
|
||||
|
||||
matches_tokens.push(quote!{
|
||||
#(
|
||||
for pattern in #patterns_tokens {
|
||||
if error_msg.contains(pattern) {
|
||||
let split_command = split_command(&last_command);
|
||||
#suggestion_tokens;
|
||||
};
|
||||
})*
|
||||
})
|
||||
}
|
||||
quote! {
|
||||
let mut last_command = last_command.to_string();
|
||||
match executable {
|
||||
#(
|
||||
#command => {
|
||||
#matches_tokens
|
||||
return None;
|
||||
}
|
||||
)*
|
||||
_ => { return None; }
|
||||
};
|
||||
}.into()
|
||||
}
|
||||
|
||||
fn parse_file(file: &Path) -> Rule {
|
||||
let file = std::fs::read_to_string(file).expect("Failed to read file.");
|
||||
toml::from_str(&file).expect("Failed to parse toml.")
|
||||
}
|
||||
|
||||
fn parse_conditions(suggest: &str) -> (String, Vec<TokenStream2>) {
|
||||
let mut eval_conditions = Vec::new();
|
||||
if suggest.starts_with('#') {
|
||||
let mut lines = suggest.lines().collect::<Vec<&str>>();
|
||||
let mut conditions = String::new();
|
||||
for (i, line) in lines[0..].iter().enumerate() {
|
||||
conditions.push_str(line);
|
||||
if line.ends_with(']') {
|
||||
lines = lines[i + 1..].to_vec();
|
||||
break;
|
||||
}
|
||||
}
|
||||
let conditions = conditions
|
||||
.trim_start_matches(['#', '['])
|
||||
.trim_end_matches(']')
|
||||
.split(',')
|
||||
.collect::<Vec<&str>>();
|
||||
|
||||
for condition in conditions {
|
||||
let (mut condition, arg) = condition.split_once('(').unwrap();
|
||||
condition = condition.trim();
|
||||
let arg = arg.trim_start_matches('(').trim_end_matches(')');
|
||||
let reverse = match condition.starts_with('!') {
|
||||
true => {
|
||||
condition = condition.trim_start_matches('!');
|
||||
true
|
||||
}
|
||||
false => false,
|
||||
};
|
||||
let evaluated_condition = eval_condition(condition, arg);
|
||||
|
||||
eval_conditions.push(quote!{#evaluated_condition == !#reverse});
|
||||
}
|
||||
let suggest = lines.join("\n");
|
||||
return (suggest, eval_conditions);
|
||||
}
|
||||
(suggest.to_owned(), vec![quote!{true}])
|
||||
}
|
||||
|
||||
fn eval_condition(condition: &str, arg: &str) -> TokenStream2 {
|
||||
match condition {
|
||||
"executable" => quote!{check_executable(shell, #arg)},
|
||||
"err_contains" => quote!{error_msg.contains(#arg)},
|
||||
"cmd_contains" => quote!{last_command.contains(#arg)},
|
||||
"min_length" => quote!{(split_command.len() >= #arg.parse::<usize>().unwrap())},
|
||||
"length" => quote!{(split_command.len() == #arg.parse::<usize>().unwrap())},
|
||||
"max_length" => quote!{(split_command.len() <= #arg.parse::<usize>().unwrap() + 1)},
|
||||
_ => unreachable!("Unknown condition when evaluation condition: {}", condition),
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_suggest(suggest: &str) -> TokenStream2 {
|
||||
let mut suggest = suggest.to_owned();
|
||||
if suggest.contains("{{command}}") {
|
||||
suggest = suggest.replace("{{command}}", "{last_command}");
|
||||
}
|
||||
|
||||
let mut replace_list = Vec::new();
|
||||
let mut opt_list = Vec::new();
|
||||
let mut cmd_list = Vec::new();
|
||||
|
||||
replaces::opts(&mut suggest, &mut replace_list, &mut opt_list);
|
||||
replaces::cmd_reg(&mut suggest, &mut replace_list);
|
||||
replaces::err(&mut suggest, &mut replace_list);
|
||||
replaces::command(&mut suggest, &mut replace_list);
|
||||
replaces::shell(&mut suggest, &mut cmd_list);
|
||||
replaces::typo(&mut suggest, &mut replace_list);
|
||||
replaces::shell_tag(&mut suggest, &mut replace_list, cmd_list);
|
||||
|
||||
quote! {
|
||||
#(#opt_list)*
|
||||
return Some(format!{#suggest, #(#replace_list),*});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,263 +0,0 @@
|
|||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
|
||||
fn rtag(name: &str, x: i32, y: String) -> TokenStream2 {
|
||||
let tag = format!("{}{} = {}", name, x, y);
|
||||
let tag: TokenStream2 = tag.parse().unwrap();
|
||||
tag
|
||||
}
|
||||
|
||||
fn tag(name: &str, x: i32) -> String {
|
||||
let tag = format!("{{{}{}}}", name, x);
|
||||
let tag = tag.as_str();
|
||||
let tag = tag.to_owned();
|
||||
tag
|
||||
}
|
||||
|
||||
fn eval_placeholder(
|
||||
string: &str,
|
||||
start: &str,
|
||||
end: &str,
|
||||
) -> (std::ops::Range<usize>, std::ops::Range<usize>) {
|
||||
let start_index = string.find(start).unwrap();
|
||||
let end_index = string[start_index..].find(end).unwrap() + start_index + end.len();
|
||||
|
||||
let placeholder = start_index..end_index;
|
||||
|
||||
let args = start_index + start.len()..end_index - end.len();
|
||||
|
||||
(placeholder, args)
|
||||
}
|
||||
|
||||
pub fn opts(
|
||||
suggest: &mut String,
|
||||
replace_list: &mut Vec<TokenStream2>,
|
||||
opt_list: &mut Vec<TokenStream2>,
|
||||
) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "opts";
|
||||
while suggest.contains("{{opt::") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{opt::", "}}");
|
||||
|
||||
let opt = &suggest[args.to_owned()];
|
||||
let regex = opt.trim();
|
||||
let current_tag = tag(tag_name, replace_tag);
|
||||
let token_tag: TokenStream2 = format!("{}{}", tag_name, replace_tag).parse().unwrap();
|
||||
let command = quote! {
|
||||
let #token_tag = opt_regex(#regex, &mut last_command);
|
||||
};
|
||||
opt_list.push(command);
|
||||
|
||||
replace_list.push(rtag(tag_name, replace_tag, current_tag.to_owned()));
|
||||
suggest.replace_range(placeholder, ¤t_tag);
|
||||
replace_tag += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cmd_reg(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "cmd";
|
||||
|
||||
while suggest.contains("{{cmd::") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{cmd::", "}}");
|
||||
|
||||
let regex = suggest[args.to_owned()].trim();
|
||||
|
||||
let command = format!("cmd_regex(r###\"{}\"###, &last_command)", regex);
|
||||
|
||||
replace_list.push(rtag(tag_name, replace_tag, command));
|
||||
suggest.replace_range(placeholder, &tag(tag_name, replace_tag));
|
||||
replace_tag += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn err(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "err";
|
||||
|
||||
while suggest.contains("{{err::") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{err::", "}}");
|
||||
|
||||
let regex = suggest[args.to_owned()].trim();
|
||||
|
||||
let command = format!("err_regex(r###\"{}\"###, error_msg)", regex);
|
||||
|
||||
replace_list.push(rtag(tag_name, replace_tag, command));
|
||||
suggest.replace_range(placeholder, &tag(tag_name, replace_tag));
|
||||
replace_tag += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn command(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "command";
|
||||
while suggest.contains("{{command") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{command", "}}");
|
||||
|
||||
let range = suggest[args.to_owned()].trim_matches(|c| c == '[' || c == ']');
|
||||
if let Some((start, end)) = range.split_once(':') {
|
||||
let mut start_string = start.to_string();
|
||||
let start = start.parse::<i32>().unwrap_or(0);
|
||||
if start < 0 {
|
||||
start_string = format!("split_command.len() {}", start);
|
||||
};
|
||||
let end_string;
|
||||
let parsed_end = end.parse::<i32>();
|
||||
if parsed_end.is_err() {
|
||||
end_string = String::from("split_command.len()");
|
||||
} else {
|
||||
let end = parsed_end.clone().unwrap();
|
||||
if end < 0 {
|
||||
end_string = format!("split_command.len() {}", end + 1);
|
||||
} else {
|
||||
end_string = (end + 1).to_string();
|
||||
}
|
||||
};
|
||||
|
||||
let command = format! {r#"split_command[{}..{}].join(" ")"#, start_string, end_string};
|
||||
|
||||
replace_list.push(rtag(tag_name, replace_tag, command));
|
||||
suggest.replace_range(placeholder, &tag(tag_name, replace_tag));
|
||||
} else {
|
||||
let range = range.parse::<i32>().unwrap_or(0);
|
||||
let command = format!("split_command[{}]", range);
|
||||
|
||||
replace_list.push(rtag(tag_name, replace_tag, command));
|
||||
suggest.replace_range(placeholder, &tag(tag_name, replace_tag));
|
||||
}
|
||||
replace_tag += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn typo(suggest: &mut String, replace_list: &mut Vec<TokenStream2>) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "typo";
|
||||
|
||||
while suggest.contains("{{typo") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{typo", "}}");
|
||||
|
||||
let string_index = if suggest.contains('[') {
|
||||
let split = suggest[args.to_owned()]
|
||||
.split(&['[', ']'])
|
||||
.collect::<Vec<&str>>();
|
||||
let command_index = split[1];
|
||||
if !command_index.contains(':') {
|
||||
let command_index = command_index.parse::<i32>().unwrap();
|
||||
|
||||
let index = if command_index < 0 {
|
||||
format!("split_command.len() {}", command_index)
|
||||
} else {
|
||||
command_index.to_string()
|
||||
};
|
||||
format!("{}..{} + 1", index, index)
|
||||
} else {
|
||||
let (start, end) = command_index.split_once(':').unwrap();
|
||||
let start = start.parse::<i32>().unwrap_or(0);
|
||||
let start_string = if start < 0 {
|
||||
format!("split_command.len() {}", start)
|
||||
} else {
|
||||
start.to_string()
|
||||
};
|
||||
let end = end.parse::<i32>();
|
||||
let end_string = if end.is_err() {
|
||||
String::from("split_command.len()")
|
||||
} else {
|
||||
let end = end.unwrap();
|
||||
if end < 0 {
|
||||
format!("split_command.len() {}", end + 1)
|
||||
} else {
|
||||
(end + 1).to_string()
|
||||
}
|
||||
};
|
||||
|
||||
format!("{}..{}", start_string, end_string)
|
||||
}
|
||||
} else {
|
||||
unreachable!("Typo suggestion must have a command index");
|
||||
};
|
||||
let match_list;
|
||||
if suggest.contains('(') {
|
||||
let split = suggest[args.to_owned()]
|
||||
.split_once("(")
|
||||
.unwrap()
|
||||
.1
|
||||
.rsplit_once(")")
|
||||
.unwrap()
|
||||
.0;
|
||||
match_list = split.split(',').collect::<Vec<&str>>();
|
||||
} else {
|
||||
unreachable!("Typo suggestion must have a match list");
|
||||
}
|
||||
|
||||
let match_list = match_list
|
||||
.iter()
|
||||
.map(|s| s.trim().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
let command;
|
||||
if match_list[0].starts_with("eval_shell_command(") {
|
||||
let function = match_list.join(",");
|
||||
// add a " after first comma, and a " before last )
|
||||
let function = format!(
|
||||
"{}\"{}{}",
|
||||
&function[..function.find(',').unwrap() + 1],
|
||||
&function[function.find(',').unwrap() + 1..function.len() - 1],
|
||||
"\")"
|
||||
);
|
||||
command = format!(
|
||||
"suggest_typo(&split_command[{}], &{})",
|
||||
string_index, function
|
||||
);
|
||||
} else {
|
||||
let match_list = match_list
|
||||
.iter()
|
||||
.map(|s| s.trim().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
let string_match_list = match_list.join("\".to_string(), \"");
|
||||
let string_match_list = format!("\"{}\".to_string()", string_match_list);
|
||||
command = format!(
|
||||
"suggest_typo(&split_command[{}], &[{}])",
|
||||
string_index, string_match_list
|
||||
);
|
||||
}
|
||||
|
||||
replace_list.push(rtag(tag_name, replace_tag, command));
|
||||
suggest.replace_range(placeholder, &tag(tag_name, replace_tag));
|
||||
replace_tag += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shell(suggest: &mut String, cmd_list: &mut Vec<String>) {
|
||||
while suggest.contains("{{shell") {
|
||||
let (placeholder, args) = eval_placeholder(suggest, "{{shell", "}}");
|
||||
let range = suggest[args.to_owned()].trim_matches(|c| c == '(' || c == ')');
|
||||
|
||||
let command = format!("eval_shell_command(shell, {})", range);
|
||||
|
||||
suggest.replace_range(placeholder, &command);
|
||||
cmd_list.push(command);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shell_tag(
|
||||
suggest: &mut String,
|
||||
replace_list: &mut Vec<TokenStream2>,
|
||||
cmd_list: Vec<String>,
|
||||
) {
|
||||
let mut replace_tag = 0;
|
||||
let tag_name = "shell";
|
||||
|
||||
for command in cmd_list {
|
||||
if suggest.contains(&command) {
|
||||
*suggest = suggest.replace(&command, &tag(tag_name, replace_tag));
|
||||
|
||||
let split = command.split_once(',').unwrap();
|
||||
let argument = split.1.trim_end_matches(')').trim();
|
||||
let argument = format!("\"{}\"", argument);
|
||||
let function = format!("{}, {}).join(\"\")", split.0, argument);
|
||||
// let function = format!("\"{}, {}\"", split.0, split.1);
|
||||
replace_list.push(rtag(tag_name, replace_tag, function));
|
||||
replace_tag += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ use std::time::{Duration, Instant};
|
|||
|
||||
use regex_lite::Regex;
|
||||
|
||||
use rule_parser::parse_rules;
|
||||
use pay_respects_parser::parse_rules;
|
||||
|
||||
use crate::files::{get_best_match_file, get_path_files};
|
||||
use crate::shell::PRIVILEGE_LIST;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue