Skip to content

Commit

Permalink
fix clippy issues
Browse files Browse the repository at this point in the history
jdidion committed Oct 5, 2023
1 parent bfe2f07 commit 48f50da
Showing 4 changed files with 21 additions and 28 deletions.
8 changes: 4 additions & 4 deletions pest-test-gen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ impl Args {
_ => abort!(lit, "Invalid argument to 'dir' attribute"),
};
if path.is_relative() {
path = PathBuf::from(pest_test::cargo_manifest_dir()).join(path)
path = pest_test::cargo_manifest_dir().join(path)
}
args.dir = path
}
@@ -172,10 +172,10 @@ impl Args {
let path = entry.path();
if path.is_dir() {
false
} else if self.ext == "" {
} else if self.ext.is_empty() {
path.extension().is_none()
} else {
entry.path().extension().map(|s| s.into()) == Some(self.ext.as_ref())
entry.path().extension() == Some(self.ext.as_ref())
}
})
.map(move |entry| {
@@ -202,7 +202,7 @@ fn rule_variant(rule_path: &Path, variant_ident: Ident) -> Path {
}

fn add_tests(module: &mut ItemMod, args: &Args) {
let (_, content) = module.content.get_or_insert_with(|| Default::default());
let (_, content) = module.content.get_or_insert_with(Default::default);

let test_dir = args.dir.as_os_str().to_str().unwrap().to_owned();
let test_ext = args.ext.clone();
7 changes: 2 additions & 5 deletions pest-test/src/diff.rs
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ impl ExpressionDiff {
value: Some(actual_value),
},
) if expected_name == actual_name
&& (ignore_missing_expected_values || actual_value == "") =>
&& (ignore_missing_expected_values || actual_value.is_empty()) =>
{
ExpressionDiff::Equal(actual.clone())
}
@@ -111,10 +111,7 @@ impl ExpressionDiff {
}
let partial = children
.iter()
.filter(|child| match child {
ExpressionDiff::Equal(_) => false,
_ => true,
})
.filter(|child| !matches!(child, ExpressionDiff::Equal(_)))
.count()
> 0;
if partial {
21 changes: 8 additions & 13 deletions pest-test/src/model.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ impl ModelError {
}
}

fn assert_rule<'a>(pair: Pair<'a, Rule>, rule: Rule) -> Result<Pair<'a, Rule>, ModelError> {
fn assert_rule(pair: Pair<'_, Rule>, rule: Rule) -> Result<Pair<'_, Rule>, ModelError> {
if pair.as_rule() == rule {
Ok(pair)
} else {
@@ -46,7 +46,7 @@ pub enum Expression {
}

impl Expression {
pub fn try_from_sexpr<'a>(pair: Pair<'a, Rule>) -> Result<Self, ModelError> {
pub fn try_from_sexpr(pair: Pair<'_, Rule>) -> Result<Self, ModelError> {
let mut inner = pair.into_inner();
let skip_depth: usize = if inner.peek().map(|pair| pair.as_rule()) == Some(Rule::skip) {
let depth_pair = inner
@@ -72,10 +72,8 @@ impl Expression {
None => Self::Terminal { name, value: None },
Some(pair) => match pair.as_rule() {
Rule::sub_expressions => {
let children: Result<Vec<Expression>, ModelError> = pair
.into_inner()
.map(|pair| Self::try_from_sexpr(pair))
.collect();
let children: Result<Vec<Expression>, ModelError> =
pair.into_inner().map(Self::try_from_sexpr).collect();
Self::NonTerminal {
name,
children: children?,
@@ -101,8 +99,8 @@ impl Expression {
}
}

pub fn try_from_code<'a, R: RuleType>(
pair: Pair<'a, R>,
pub fn try_from_code<R: RuleType>(
pair: Pair<'_, R>,
skip_rules: &HashSet<R>,
) -> Result<Self, ModelError> {
let name = format!("{:?}", pair.as_rule());
@@ -117,10 +115,7 @@ impl Expression {
name,
value: Some(value.to_owned()),
}),
Ok(children) => Ok(Self::NonTerminal {
name,
children: children,
}),
Ok(children) => Ok(Self::NonTerminal { name, children }),
Err(e) => Err(e),
}
}
@@ -286,7 +281,7 @@ pub struct TestCase {
}

impl TestCase {
pub fn try_from_pair<'a>(pair: Pair<'a, Rule>) -> Result<Self, ModelError> {
pub fn try_from_pair(pair: Pair<'_, Rule>) -> Result<Self, ModelError> {
let mut inner = pair.into_inner();
let name = inner
.next()
13 changes: 7 additions & 6 deletions pest-test/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use pest::{error::Error as PestError, iterators::Pair, Parser, RuleType};
use pest_derive;
use std::marker::PhantomData;
use thiserror::Error;

// TODO: clippy complains about the `Pest` variant being large -
// maybe box `source`?
#[derive(Error, Debug)]
pub enum ParserError<R> {
#[error("Error parsing source text")]
@@ -11,22 +12,22 @@ pub enum ParserError<R> {
Empty,
}

pub fn parse<'a, R: RuleType, P: Parser<R>>(
text: &'a str,
pub fn parse<R: RuleType, P: Parser<R>>(
text: &str,
rule: R,
_: PhantomData<P>,
) -> Result<Pair<'a, R>, ParserError<R>> {
) -> Result<Pair<'_, R>, ParserError<R>> {
P::parse(rule, text)
.map_err(|source| ParserError::Pest { source })
.and_then(|mut code_pairs| code_pairs.next().ok_or_else(|| ParserError::Empty))
.and_then(|mut code_pairs| code_pairs.next().ok_or(ParserError::Empty))
}

#[derive(pest_derive::Parser)]
#[grammar = "test.pest"]
pub struct TestParser;

impl TestParser {
pub fn parse<'a>(text: &'a str) -> Result<Pair<'a, Rule>, ParserError<Rule>> {
pub fn parse(text: &str) -> Result<Pair<'_, Rule>, ParserError<Rule>> {
parse(text, Rule::test_case, PhantomData::<Self>)
}
}

0 comments on commit 48f50da

Please sign in to comment.