From d564eb95e16d94356da83986a5cb6bd43ef3c8f7 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Wed, 15 Nov 2023 06:39:42 +0100 Subject: [PATCH] fix bot command suggestion with `@`- char was detected as email address fixes #53 --- CHANGELOG.md | 3 +++ docs.md | 6 ++--- src/parser/parse_from_text/text_elements.rs | 8 +++--- tests/text_to_ast/text_only.rs | 28 +++++++++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d675d9..0a33819 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +### Fixed +- fix bot command suggestion with `@`- char was detected as email address + ## 0.8.0 - Nom 7 and more Generic URI Schemes ### Changed diff --git a/docs.md b/docs.md index ff751ff..b195ffe 100644 --- a/docs.md +++ b/docs.md @@ -14,9 +14,9 @@ while !remaining_input.is_empty() { let res = { // try the following parsers in this order (order is important with some parsers) 1. hashtag(input) - 2. email_address(input) - 3. link(input) - 4. bot_command_suggestion(input) + 2. bot_command_suggestion(input) + 3. email_address(input) + 4. link(input) 5. linebreak(input) last option: consumes all text until [parse_text_element] works again } diff --git a/src/parser/parse_from_text/text_elements.rs b/src/parser/parse_from_text/text_elements.rs index 161d8c6..6914cd6 100644 --- a/src/parser/parse_from_text/text_elements.rs +++ b/src/parser/parse_from_text/text_elements.rs @@ -263,10 +263,6 @@ pub(crate) fn parse_text_element( if let Ok((i, elm)) = hashtag(input) { Ok((i, elm)) - } else if let Ok((i, elm)) = email_address(input) { - Ok((i, elm)) - } else if let Ok((i, elm)) = link(input) { - Ok((i, elm)) } else if let Ok((i, elm)) = { if prev_char == Some(' ') || prev_char.is_none() { bot_command_suggestion(input) @@ -275,6 +271,10 @@ pub(crate) fn parse_text_element( } } { Ok((i, elm)) + } else if let Ok((i, elm)) = email_address(input) { + Ok((i, elm)) + } else if let Ok((i, elm)) = link(input) { + Ok((i, elm)) } else if let Ok((i, _)) = linebreak(input) { Ok((i, Element::Linebreak)) } else { diff --git a/tests/text_to_ast/text_only.rs b/tests/text_to_ast/text_only.rs index 2650973..a9bf15c 100644 --- a/tests/text_to_ast/text_only.rs +++ b/tests/text_to_ast/text_only.rs @@ -609,3 +609,31 @@ fn link_in_parenthesis2() { ] ); } + + +#[test] +fn bot_suggestion_is_no_email() { + assert_eq!( + parse_only_text("/command@bot@addr.com"), + vec![ + BotCommandSuggestion("/command@bot@addr.com"), + ] + ); + assert_eq!( + parse_only_text("\n/command@bot@addr.com"), + vec![ + Linebreak, + BotCommandSuggestion("/command@bot@addr.com"), + ] + ); + + assert_eq!( + parse_only_text("Bots that can be selected \n/command@bot@addr.com BOT"), + vec![ + Text("Bots that can be selected "), + Linebreak, + BotCommandSuggestion("/command@bot@addr.com"), + Text(" BOT"), + ] + ); +}