mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-13 09:10:14 +00:00
format rust code
This commit is contained in:
parent
beec38ef8b
commit
d804303615
3 changed files with 59 additions and 49 deletions
|
|
@ -136,11 +136,7 @@ pub fn parse_command(
|
||||||
// update best attempt if we're deeper
|
// update best attempt if we're deeper
|
||||||
if best_attempt.as_ref().map(|x| x.1.len()).unwrap_or(0) < matched_tokens.len()
|
if best_attempt.as_ref().map(|x| x.1.len()).unwrap_or(0) < matched_tokens.len()
|
||||||
{
|
{
|
||||||
best_attempt = Some((
|
best_attempt = Some((next_tree.clone(), matched_tokens.clone(), *new_pos));
|
||||||
next_tree.clone(),
|
|
||||||
matched_tokens.clone(),
|
|
||||||
*new_pos,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filtered_tokens.clear(); // new branch, new tokens
|
filtered_tokens.clear(); // new branch, new tokens
|
||||||
|
|
@ -230,11 +226,11 @@ pub fn parse_command(
|
||||||
cmd_a == cmd_b
|
cmd_a == cmd_b
|
||||||
});
|
});
|
||||||
// re-sort after extending
|
// re-sort after extending
|
||||||
possible_commands.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
|
possible_commands
|
||||||
|
.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
|
||||||
}
|
}
|
||||||
|
|
||||||
if possible_commands.is_empty().not() {
|
if possible_commands.is_empty().not() {
|
||||||
|
|
||||||
error.push_str(" Perhaps you meant one of the following commands:\n");
|
error.push_str(" Perhaps you meant one of the following commands:\n");
|
||||||
fmt_commands_list(&mut error, &prefix, possible_commands);
|
fmt_commands_list(&mut error, &prefix, possible_commands);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -442,13 +438,15 @@ fn rank_possible_commands(
|
||||||
.filter(|cmd| cmd.show_in_suggestions)
|
.filter(|cmd| cmd.show_in_suggestions)
|
||||||
.flat_map(|cmd| {
|
.flat_map(|cmd| {
|
||||||
let versions = generate_command_versions(cmd, input_tokens);
|
let versions = generate_command_versions(cmd, input_tokens);
|
||||||
versions.into_iter().map(move |(display, scoring, is_alias)| {
|
versions
|
||||||
let similarity = strsim::jaro_winkler(&input, &scoring);
|
.into_iter()
|
||||||
// if similarity > 0.7 {
|
.map(move |(display, scoring, is_alias)| {
|
||||||
// println!("DEBUG: ranking: '{}' vs '{}' = {}", input, scoring, similarity);
|
let similarity = strsim::jaro_winkler(&input, &scoring);
|
||||||
// }
|
// if similarity > 0.7 {
|
||||||
(cmd, display, similarity, is_alias)
|
// println!("DEBUG: ranking: '{}' vs '{}' = {}", input, scoring, similarity);
|
||||||
})
|
// }
|
||||||
|
(cmd, display, similarity, is_alias)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
@ -484,7 +482,11 @@ fn rank_possible_commands(
|
||||||
commands_to_show
|
commands_to_show
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_commands_list(f: &mut String, prefix: &str, commands_to_show: Vec<(Command, String, f64, bool)>) {
|
fn fmt_commands_list(
|
||||||
|
f: &mut String,
|
||||||
|
prefix: &str,
|
||||||
|
commands_to_show: Vec<(Command, String, f64, bool)>,
|
||||||
|
) {
|
||||||
for (command, version, _, is_alias) in commands_to_show {
|
for (command, version, _, is_alias) in commands_to_show {
|
||||||
writeln!(
|
writeln!(
|
||||||
f,
|
f,
|
||||||
|
|
@ -523,7 +525,11 @@ fn generate_command_versions(cmd: &Command, input_tokens: &[&str]) -> Vec<(Strin
|
||||||
versions
|
versions
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_command_string(cmd: &Command, alias_replacement: Option<(usize, &str)>, input_tokens: &[&str]) -> String {
|
fn build_command_string(
|
||||||
|
cmd: &Command,
|
||||||
|
alias_replacement: Option<(usize, &str)>,
|
||||||
|
input_tokens: &[&str],
|
||||||
|
) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
for (idx, token) in cmd.tokens.iter().enumerate() {
|
for (idx, token) in cmd.tokens.iter().enumerate() {
|
||||||
if idx > 0 {
|
if idx > 0 {
|
||||||
|
|
@ -542,12 +548,12 @@ fn build_command_string(cmd: &Command, alias_replacement: Option<(usize, &str)>,
|
||||||
Token::Parameter(param) => {
|
Token::Parameter(param) => {
|
||||||
// if we have an input token at this position, use it
|
// if we have an input token at this position, use it
|
||||||
// otherwise use the placeholder
|
// otherwise use the placeholder
|
||||||
if let Some(input_token) = input_tokens.get(idx) {
|
if let Some(input_token) = input_tokens.get(idx) {
|
||||||
result.push_str(input_token);
|
result.push_str(input_token);
|
||||||
} else {
|
} else {
|
||||||
write!(&mut result, "{param}").unwrap()
|
write!(&mut result, "{param}").unwrap()
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use command_parser::{parse_command, Tree, command::Command, parameter::*, tokens};
|
use command_parser::{Tree, command::Command, parameter::*, parse_command, tokens};
|
||||||
|
|
||||||
/// this checks if we properly keep track of filtered tokens (eg. branches we failed on)
|
/// this checks if we properly keep track of filtered tokens (eg. branches we failed on)
|
||||||
/// when we backtrack. a previous parser bug would cause infinite loops since it did not
|
/// when we backtrack. a previous parser bug would cause infinite loops since it did not
|
||||||
|
|
@ -40,6 +40,12 @@ fn test_dirty_params() {
|
||||||
let result = parse_command(tree, "pk;".to_string(), input.to_string()).unwrap();
|
let result = parse_command(tree, "pk;".to_string(), input.to_string()).unwrap();
|
||||||
|
|
||||||
println!("params: {:?}", result.parameters);
|
println!("params: {:?}", result.parameters);
|
||||||
assert!(!result.parameters.contains_key("param1"), "params should not contain 'param1' from failed branch");
|
assert!(
|
||||||
assert!(result.parameters.contains_key("param2"), "params should contain 'param2'");
|
!result.parameters.contains_key("param1"),
|
||||||
|
"params should not contain 'param1' from failed branch"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
result.parameters.contains_key("param2"),
|
||||||
|
"params should contain 'param2'"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use command_parser::{parse_command, Tree, command::Command, parameter::*, tokens};
|
use command_parser::{Tree, command::Command, parameter::*, parse_command, tokens};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_typoed_command_with_parameter() {
|
fn test_typoed_command_with_parameter() {
|
||||||
|
|
@ -8,8 +8,9 @@ fn test_typoed_command_with_parameter() {
|
||||||
// message <optional msg ref> author
|
// message <optional msg ref> author
|
||||||
let cmd = Command::new(
|
let cmd = Command::new(
|
||||||
tokens!(message_token, Optional(MESSAGE_REF), author_token),
|
tokens!(message_token, Optional(MESSAGE_REF), author_token),
|
||||||
"message_author"
|
"message_author",
|
||||||
).help("Shows the author of a proxied message");
|
)
|
||||||
|
.help("Shows the author of a proxied message");
|
||||||
|
|
||||||
let mut tree = Tree::default();
|
let mut tree = Tree::default();
|
||||||
tree.register_command(cmd);
|
tree.register_command(cmd);
|
||||||
|
|
@ -32,13 +33,10 @@ fn test_typoed_command_with_flags() {
|
||||||
let message_token = ("message", ["msg", "messageinfo"]);
|
let message_token = ("message", ["msg", "messageinfo"]);
|
||||||
let author_token = ("author", ["sender", "a"]);
|
let author_token = ("author", ["sender", "a"]);
|
||||||
|
|
||||||
let cmd = Command::new(
|
let cmd = Command::new(tokens!(message_token, author_token), "message_author")
|
||||||
tokens!(message_token, author_token),
|
.flag(("flag", ["f"]))
|
||||||
"message_author"
|
.flag(("flag2", ["f2"]))
|
||||||
)
|
.help("Shows the author of a proxied message");
|
||||||
.flag(("flag", ["f"]))
|
|
||||||
.flag(("flag2", ["f2"]))
|
|
||||||
.help("Shows the author of a proxied message");
|
|
||||||
|
|
||||||
let mut tree = Tree::default();
|
let mut tree = Tree::default();
|
||||||
tree.register_command(cmd);
|
tree.register_command(cmd);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue