Skip to content

Commit

Permalink
feat: added crates.io badge + rudimentary fuzzing for emails and URLS
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Mar 7, 2024
1 parent 0ce4b58 commit c7f0581
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![Harper LS](https://github.com/elijah-potter/harper/actions/workflows/build_harper_ls.yml/badge.svg)](https://github.com/elijah-potter/harper/actions/workflows/build_harper_ls.yml)
[![Web](https://github.com/elijah-potter/harper/actions/workflows/build_web.yml/badge.svg)](https://github.com/elijah-potter/harper/actions/workflows/build_web.yml)
[![Precommit](https://github.com/elijah-potter/harper/actions/workflows/precommit.yml/badge.svg)](https://github.com/elijah-potter/harper/actions/workflows/precommit.yml)
[![Crates.io](https://img.shields.io/crates/v/harper-ls)](https://crates.io/crates/harper-ls)

Harper is an English grammar checker designed to be _just right._
I created it after years of dealing with the shortcomings of the competition.
Expand Down
1 change: 1 addition & 0 deletions harper-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ thiserror = "1.0.56"

[dev-dependencies]
divan = "0.1.14"
rand = "0.8.5"

[[bench]]
name = "parse_demo"
Expand Down
22 changes: 19 additions & 3 deletions harper-core/src/lexing/email_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn lex_email_address(source: &[char]) -> Option<FoundToken> {

Some(FoundToken {
next_index: at_loc + 1 + domain_part_len,
token: TokenKind::EmailAddress
token: TokenKind::EmailAddress,
})
}

Expand Down Expand Up @@ -90,7 +90,7 @@ fn valid_unquoted_character(c: char) -> bool {

let others = [
'!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}',
'~', '.'
'~', '.',
];

if others.contains(&c) {
Expand All @@ -102,6 +102,8 @@ fn valid_unquoted_character(c: char) -> bool {

#[cfg(test)]
mod tests {
use rand::Rng;

use super::super::hostname::tests::example_domain_parts;
use super::{lex_email_address, validate_local_part};

Expand All @@ -123,7 +125,7 @@ mod tests {
r#"user-"#,
r#"postmaster"#,
r#"postmaster"#,
r#"_test"#
r#"_test"#,
]
.into_iter()
.map(|s| s.chars().collect())
Expand Down Expand Up @@ -152,4 +154,18 @@ mod tests {
}
}
}

/// Tests that the email parser will not throw a panic under some random situations.
#[test]
fn survives_random_chars() {
let mut rng = rand::thread_rng();

let mut buf = [' '; 128];

for _ in 0..1 << 16 {
rng.try_fill(&mut buf).unwrap();

lex_email_address(&buf);
}
}
}
20 changes: 18 additions & 2 deletions harper-core/src/lexing/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn lex_url(source: &[char]) -> Option<FoundToken> {

Some(FoundToken {
next_index: url_end + sep + 1,
token: TokenKind::Url
token: TokenKind::Url,
})
}

Expand Down Expand Up @@ -96,7 +96,7 @@ fn lex_hostport(source: &[char]) -> Option<usize> {
c.is_ascii_digit()
})
.map(|(i, _)| i)
.unwrap_or(source.len())
.unwrap_or(source.len()),
)
} else {
Some(hostname_end)
Expand Down Expand Up @@ -196,6 +196,8 @@ fn lex_uchar(source: &[char]) -> Option<usize> {

#[cfg(test)]
mod tests {
use rand::Rng;

use super::lex_url;

fn assert_consumes_full(url: &str) {
Expand Down Expand Up @@ -232,4 +234,18 @@ mod tests {
fn consumes_with_path() {
assert_consumes_full("https://elijahpotter.dev/articles/quantifying_hope_on_a_global_scale")
}

/// Tests that the URL parser will not throw a panic under some random situations.
#[test]
fn survives_random_chars() {
let mut rng = rand::thread_rng();

let mut buf = [' '; 128];

for _ in 0..1 << 16 {
rng.try_fill(&mut buf).unwrap();

lex_url(&buf);
}
}
}
6 changes: 3 additions & 3 deletions harper-core/src/linting/an_a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Linter for AnA {
let is_a_an = match chars_first {
['a'] => Some(true),
['a', 'n'] => Some(false),
_ => None
_ => None,
};

let Some(a_an) = is_a_an else {
Expand All @@ -28,15 +28,15 @@ impl Linter for AnA {
if a_an != should_be_a_an {
let replacement = match a_an {
true => vec!['a', 'n'],
false => vec!['a']
false => vec!['a'],
};

lints.push(Lint {
span: first.span,
lint_kind: LintKind::Formatting,
suggestions: vec![Suggestion::ReplaceWith(replacement)],
message: "This is not vocally correct.".to_string(),
priority: 31
priority: 31,
})
}
}
Expand Down

0 comments on commit c7f0581

Please sign in to comment.