Skip to content

Commit

Permalink
fix #67: Mistaking UN*X absolute paths as bot commands (#68)
Browse files Browse the repository at this point in the history
* fix #67

* ah clippy spare me!(in other words, I fixed a clippy issue)

* fix formatting. now you rustfmt?
  • Loading branch information
farooqkz authored May 7, 2024
1 parent f010384 commit 26c88c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/parser/parse_from_text/text_elements.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
///! nom parsers for text elements

Check warning on line 1 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

this is an outer doc comment and does not apply to the parent module or crate

warning: this is an outer doc comment and does not apply to the parent module or crate --> src/parser/parse_from_text/text_elements.rs:1:1 | 1 | ///! nom parsers for text elements | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_doc_comments = note: `#[warn(clippy::suspicious_doc_comments)]` on by default help: use an inner doc comment to document the parent module or crate | 1 | //! nom parsers for text elements |

Check warning on line 1 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

this is an outer doc comment and does not apply to the parent module or crate

warning: this is an outer doc comment and does not apply to the parent module or crate --> src/parser/parse_from_text/text_elements.rs:1:1 | 1 | ///! nom parsers for text elements | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_doc_comments = note: `#[warn(clippy::suspicious_doc_comments)]` on by default help: use an inner doc comment to document the parent module or crate | 1 | //! nom parsers for text elements |

Check warning on line 1 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

this is an outer doc comment and does not apply to the parent module or crate

warning: this is an outer doc comment and does not apply to the parent module or crate --> src/parser/parse_from_text/text_elements.rs:1:1 | 1 | ///! nom parsers for text elements | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_doc_comments = note: `#[warn(clippy::suspicious_doc_comments)]` on by default help: use an inner doc comment to document the parent module or crate | 1 | //! nom parsers for text elements |
use crate::parser::link_url::LinkDestination;

use super::hashtag_content_char_ranges::hashtag_content_char;
use super::Element;
use nom::{
bytes::{
complete::{tag, take, take_while, take_while1},
streaming::take_till1,
},
character,
character::complete::char,
combinator::{peek, recognize, verify},
sequence::tuple,
AsChar, IResult, Offset, Slice,
};

use super::base_parsers::CustomError;
use super::hashtag_content_char_ranges::hashtag_content_char;
use super::Element;
use crate::parser::link_url::LinkDestination;

fn linebreak(input: &str) -> IResult<&str, char, CustomError<&str>> {
char('\n')(input)
}

fn hashtag(input: &str) -> IResult<&str, Element, CustomError<&str>> {
let (input, content) = recognize(tuple((
character::complete::char('#'),
take_while1(hashtag_content_char),
)))(input)?;
let (input, content) = recognize(tuple((char('#'), take_while1(hashtag_content_char))))(input)?;

Ok((input, Element::Tag(content)))
}
Expand Down Expand Up @@ -230,7 +228,7 @@ pub(crate) fn link(input: &str) -> IResult<&str, Element, CustomError<&str>> {
*/
fn is_allowed_bot_cmd_suggestion_char(char: char) -> bool {
match char {
'@' | '\\' | '_' | '/' | '.' | '-' => true,
'@' | '\\' | '_' | '.' | '-' | '/' => true,
_ => char.is_alphanum(),
}
}
Expand All @@ -248,8 +246,11 @@ fn bot_command_suggestion(input: &str) -> IResult<&str, Element, CustomError<&st
s.len() < 256
}),
)))(input)?;

Ok((input, Element::BotCommandSuggestion(content)))
if content.slice(1..).contains('/') {
Ok((input, Element::Text(content)))
} else {
Ok((input, Element::BotCommandSuggestion(content)))
}
}

pub(crate) fn parse_text_element(
Expand All @@ -269,7 +270,9 @@ pub(crate) fn parse_text_element(
if prev_char == Some(' ') || prev_char.is_none() {
bot_command_suggestion(input)
} else {
Err(nom::Err::Error(CustomError::PrecedingWhitespaceMissing))
Err(nom::Err::Error(
CustomError::<&str>::PrecedingWhitespaceMissing,
))
}
} {
Ok((i, elm))
Expand Down
6 changes: 6 additions & 0 deletions tests/text_to_ast/text_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ fn command_suggestions() {
);
}

#[test]
fn unix_abs_path_is_not_command() {
let input = "/etc/nginx";
assert_eq!(parse_only_text(input), vec![Text("/etc/nginx")]);
}

#[test]
fn invalid_command_suggestions() {
let input = "/1\n /hello world";
Expand Down

0 comments on commit 26c88c6

Please sign in to comment.