Skip to content

Commit 8b78962

Browse files
fmt
1 parent ebc8e6c commit 8b78962

File tree

8 files changed

+186
-100
lines changed

8 files changed

+186
-100
lines changed

jsonpath-ast/src/ast.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,22 @@ macro_rules! terminating_from_pest {
5454
use super::parse::{JSPathParser, Rule};
5555
#[cfg(feature = "compiled-path")]
5656
use crate::syn_parse::parse_impl::{
57-
parse_bool, parse_float, validate_function_name, validate_js_int, validate_js_str,
58-
validate_member_name_shorthand, ParseUtilsExt,
57+
ParseUtilsExt, parse_bool, parse_float, validate_function_name, validate_js_int,
58+
validate_js_str, validate_member_name_shorthand,
5959
};
6060
use derive_new::new;
6161
use from_pest::{ConversionError, FromPest, Void};
62-
use pest::iterators::{Pair, Pairs};
6362
use pest::Parser;
63+
use pest::iterators::{Pair, Pairs};
6464
use pest_ast::FromPest;
6565
use proc_macro2::Span;
66+
#[allow(unused_imports)]
67+
use syn::LitBool;
6668
#[cfg(feature = "compiled-path")]
6769
use syn::parse::ParseStream;
6870
use syn::punctuated::Punctuated;
6971
use syn::token::Bracket;
70-
#[allow(unused_imports)]
71-
use syn::LitBool;
72-
use syn::{token, Ident, Token};
72+
use syn::{Ident, Token, token};
7373
#[cfg(feature = "compiled-path")]
7474
use syn_derive::Parse;
7575

@@ -82,7 +82,7 @@ pub struct PestIgnoredPunctuated<T, P>(pub(crate) Punctuated<T, P>);
8282

8383
impl<'pest, T, P> FromPest<'pest> for PestIgnoredPunctuated<T, P>
8484
where
85-
T: FromPest<'pest, Rule=Rule, FatalError=Void> + KnowsRule + std::fmt::Debug,
85+
T: FromPest<'pest, Rule = Rule, FatalError = Void> + KnowsRule + std::fmt::Debug,
8686
P: Default,
8787
{
8888
type Rule = Rule;
@@ -173,7 +173,6 @@ pub enum Segment {
173173
),
174174
#[cfg_attr(feature = "compiled-path", parse(peek_func = ChildSegment::peek))]
175175
Child(ChildSegment),
176-
177176
}
178177

179178
#[derive(Debug, new, PartialEq, FromPest)]
@@ -317,10 +316,9 @@ impl<'pest> from_pest::FromPest<'pest> for MemberNameShorthand {
317316

318317
#[derive(Debug, new, PartialEq)]
319318
#[cfg_attr(feature = "compiled-path", derive(Parse))]
320-
pub struct JSString(#[cfg_attr(
321-
feature = "compiled-path",
322-
parse(validate_js_str)
323-
)] pub(crate) String);
319+
pub struct JSString(
320+
#[cfg_attr(feature = "compiled-path", parse(validate_js_str))] pub(crate) String,
321+
);
324322

325323
impl<'pest> from_pest::FromPest<'pest> for JSString {
326324
type Rule = Rule;
@@ -375,25 +373,21 @@ impl KnowsRule for Selector {
375373
#[cfg_attr(feature = "compiled-path", derive(Parse))]
376374
#[pest_ast(rule(Rule::slice_selector))]
377375
pub struct SliceSelector(
378-
#[cfg_attr(
379-
feature = "compiled-path",
380-
parse(SliceStart::maybe_parse)
381-
)] pub(crate) Option<SliceStart>,
376+
#[cfg_attr(feature = "compiled-path", parse(SliceStart::maybe_parse))]
377+
pub(crate) Option<SliceStart>,
382378
pub(crate) PestLiteralWithoutRule<Token![:]>,
383-
#[cfg_attr(
384-
feature = "compiled-path",
385-
parse(SliceEnd::maybe_parse)
386-
)] pub(crate) Option<SliceEnd>,
387-
#[cfg_attr(
388-
feature = "compiled-path",
389-
parse(SliceStep::maybe_parse)
390-
)] pub(crate) Option<SliceStep>,
379+
#[cfg_attr(feature = "compiled-path", parse(SliceEnd::maybe_parse))] pub(crate) Option<SliceEnd>,
380+
#[cfg_attr(feature = "compiled-path", parse(SliceStep::maybe_parse))]
381+
pub(crate) Option<SliceStep>,
391382
);
392383

393384
#[derive(Debug, new, PartialEq, FromPest)]
394385
#[cfg_attr(feature = "compiled-path", derive(Parse))]
395386
#[pest_ast(rule(Rule::step))]
396-
pub struct SliceStep(pub(crate) PestLiteralWithoutRule<Token![:]>, pub(crate) JSInt);
387+
pub struct SliceStep(
388+
pub(crate) PestLiteralWithoutRule<Token![:]>,
389+
pub(crate) JSInt,
390+
);
397391

398392
#[derive(Debug, new, PartialEq, FromPest)]
399393
#[cfg_attr(feature = "compiled-path", derive(Parse))]
@@ -474,7 +468,8 @@ impl<'pest> FromPest<'pest> for JSInt {
474468
let pair = clone.next().ok_or(ConversionError::NoMatch)?;
475469
if pair.as_rule() == Rule::int {
476470
let this = JSInt(
477-
pair.as_str().trim()
471+
pair.as_str()
472+
.trim()
478473
.parse::<i64>()
479474
.expect("int rule should always be a valid i64"),
480475
);
@@ -871,7 +866,7 @@ impl<'pest> FromPest<'pest> for Number {
871866

872867
#[derive(Debug, new, PartialEq)]
873868
#[cfg_attr(feature = "compiled-path", derive(Parse))]
874-
pub struct Bool(#[cfg_attr(feature = "compiled-path", parse(parse_bool))]pub(crate) bool);
869+
pub struct Bool(#[cfg_attr(feature = "compiled-path", parse(parse_bool))] pub(crate) bool);
875870

876871
terminating_from_pest!(Bool, Rule::bool, |inner: Pair<Rule>| Bool(
877872
inner.as_str().trim().parse::<bool>().expect("bool")

jsonpath-ast/src/syn_parse.rs

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#[cfg(feature = "compiled-path")]
22
pub(crate) mod parse_impl {
33
use crate::ast::parse::{JSPathParser, Rule};
4-
use crate::ast::{kw, CompOp, IndexSelector, Main, NameSelector};
54
use crate::ast::{
65
AbsSingularQuery, AtomExpr, Bool, BracketName, BracketedSelection, ChildSegment, CompExpr,
7-
Comparable, DescendantSegment, FilterSelector, FunctionArgument, FunctionExpr, FunctionName, IndexSegment,
8-
JPQuery, JSInt, JSString, Literal, LogicalExpr, LogicalExprAnd, MemberNameShorthand,
9-
NameSegment, NotOp, Null, Number, ParenExpr, PestIgnoredPunctuated, PestLiteralWithoutRule,
10-
RelQuery, RelSingularQuery, Root, Segment, Segments, Selector, SingularQuery,
11-
SingularQuerySegment, SingularQuerySegments, SliceEnd, SliceSelector, SliceStart, SliceStep, Test, TestExpr,
12-
WildcardSelector, WildcardSelectorOrMemberNameShorthand, EOI,
6+
Comparable, DescendantSegment, EOI, FilterSelector, FunctionArgument, FunctionExpr,
7+
FunctionName, IndexSegment, JPQuery, JSInt, JSString, Literal, LogicalExpr, LogicalExprAnd,
8+
MemberNameShorthand, NameSegment, NotOp, Null, Number, ParenExpr, PestIgnoredPunctuated,
9+
PestLiteralWithoutRule, RelQuery, RelSingularQuery, Root, Segment, Segments, Selector,
10+
SingularQuery, SingularQuerySegment, SingularQuerySegments, SliceEnd, SliceSelector,
11+
SliceStart, SliceStep, Test, TestExpr, WildcardSelector,
12+
WildcardSelectorOrMemberNameShorthand,
1313
};
14+
use crate::ast::{CompOp, IndexSelector, Main, NameSelector, kw};
1415
use pest::Parser;
1516
use proc_macro2::{Ident, TokenStream};
16-
use quote::{quote, ToTokens};
17+
use quote::{ToTokens, quote};
1718
use syn::parse::{Parse, ParseStream};
1819
use syn::punctuated::Punctuated;
19-
use syn::{token, LitBool, LitInt, LitStr, Token};
2020
use syn::token::Token;
21+
use syn::{LitBool, LitInt, LitStr, Token, token};
2122

2223
pub trait ParseUtilsExt: Parse {
2324
fn peek(input: ParseStream) -> bool;
@@ -43,10 +44,16 @@ pub(crate) mod parse_impl {
4344
Ok(PestIgnoredPunctuated(Punctuated::parse_terminated(input)?))
4445
}
4546

46-
pub(crate) fn parse_separated_nonempty(input: ParseStream) -> syn::Result<Self> where P: Token {
47+
pub(crate) fn parse_separated_nonempty(input: ParseStream) -> syn::Result<Self>
48+
where
49+
P: Token,
50+
{
4751
let res = Punctuated::parse_separated_nonempty(input)?;
4852
if res.is_empty() {
49-
Err(input.error(format!("Expected at least one {}", std::any::type_name::<T>())))
53+
Err(input.error(format!(
54+
"Expected at least one {}",
55+
std::any::type_name::<T>()
56+
)))
5057
} else {
5158
Ok(PestIgnoredPunctuated(res))
5259
}
@@ -558,9 +565,15 @@ pub(crate) mod parse_impl {
558565
impl ToTokens for Comparable {
559566
fn to_tokens(&self, tokens: &mut TokenStream) {
560567
let variant = match self {
561-
Comparable::Literal(inner) => { quote!( new_literal(#inner) ) }
562-
Comparable::SingularQuery(inner) => { quote!( new_singular_query(#inner) ) }
563-
Comparable::FunctionExpr(inner) => { quote!( new_function_expr(#inner) ) }
568+
Comparable::Literal(inner) => {
569+
quote!( new_literal(#inner) )
570+
}
571+
Comparable::SingularQuery(inner) => {
572+
quote!( new_singular_query(#inner) )
573+
}
574+
Comparable::FunctionExpr(inner) => {
575+
quote!( new_function_expr(#inner) )
576+
}
564577
};
565578
tokens.extend(quote!(::jsonpath_ast::ast::Comparable::#variant));
566579
}
@@ -569,10 +582,18 @@ pub(crate) mod parse_impl {
569582
impl ToTokens for Literal {
570583
fn to_tokens(&self, tokens: &mut TokenStream) {
571584
let variant = match self {
572-
Literal::Number(inner) => { quote!(new_number(#inner)) }
573-
Literal::String(inner) => { quote!(new_string(#inner)) }
574-
Literal::Bool(inner) => { quote!(new_bool(#inner)) }
575-
Literal::Null(inner) => { quote!(new_null(#inner)) }
585+
Literal::Number(inner) => {
586+
quote!(new_number(#inner))
587+
}
588+
Literal::String(inner) => {
589+
quote!(new_string(#inner))
590+
}
591+
Literal::Bool(inner) => {
592+
quote!(new_bool(#inner))
593+
}
594+
Literal::Null(inner) => {
595+
quote!(new_null(#inner))
596+
}
576597
};
577598
tokens.extend(quote!(::jsonpath_ast::ast::Literal::#variant))
578599
}
@@ -581,8 +602,12 @@ pub(crate) mod parse_impl {
581602
impl ToTokens for Number {
582603
fn to_tokens(&self, tokens: &mut TokenStream) {
583604
let variant = match self {
584-
Number::Int(inner) => { quote!(new_int(#inner)) }
585-
Number::Float(inner) => { quote!(new_float(#inner)) }
605+
Number::Int(inner) => {
606+
quote!(new_int(#inner))
607+
}
608+
Number::Float(inner) => {
609+
quote!(new_float(#inner))
610+
}
586611
};
587612
tokens.extend(quote!(::jsonpath_ast::ast::Number::#variant))
588613
}
@@ -591,8 +616,12 @@ pub(crate) mod parse_impl {
591616
impl ToTokens for SingularQuery {
592617
fn to_tokens(&self, tokens: &mut TokenStream) {
593618
let variant = match self {
594-
SingularQuery::RelSingularQuery(inner) => { quote!(new_rel_singular_query(#inner)) }
595-
SingularQuery::AbsSingularQuery(inner) => { quote!(new_abs_singular_query(#inner)) }
619+
SingularQuery::RelSingularQuery(inner) => {
620+
quote!(new_rel_singular_query(#inner))
621+
}
622+
SingularQuery::AbsSingularQuery(inner) => {
623+
quote!(new_abs_singular_query(#inner))
624+
}
596625
};
597626
tokens.extend(quote!(::jsonpath_ast::ast::SingularQuery::#variant ))
598627
}
@@ -610,7 +639,11 @@ pub(crate) mod parse_impl {
610639

611640
impl ToTokens for FunctionExpr {
612641
fn to_tokens(&self, tokens: &mut TokenStream) {
613-
let Self { name, paren: _, args } = self;
642+
let Self {
643+
name,
644+
paren: _,
645+
args,
646+
} = self;
614647
tokens.extend(quote! {
615648
::jsonpath_ast::ast::FunctionExpr::new(
616649
#name,
@@ -624,12 +657,24 @@ pub(crate) mod parse_impl {
624657
impl ToTokens for CompOp {
625658
fn to_tokens(&self, tokens: &mut TokenStream) {
626659
let variant = match self {
627-
CompOp::Eq(_) => { quote!(new_eq) }
628-
CompOp::Ne(_) => { quote!(new_ne) }
629-
CompOp::Le(_) => { quote!(new_le) }
630-
CompOp::Ge(_) => { quote!(new_ge) }
631-
CompOp::Lt(_) => { quote!(new_lt) }
632-
CompOp::Gt(_) => { quote!(new_gt) }
660+
CompOp::Eq(_) => {
661+
quote!(new_eq)
662+
}
663+
CompOp::Ne(_) => {
664+
quote!(new_ne)
665+
}
666+
CompOp::Le(_) => {
667+
quote!(new_le)
668+
}
669+
CompOp::Ge(_) => {
670+
quote!(new_ge)
671+
}
672+
CompOp::Lt(_) => {
673+
quote!(new_lt)
674+
}
675+
CompOp::Gt(_) => {
676+
quote!(new_gt)
677+
}
633678
};
634679
tokens.extend(quote!(::jsonpath_ast::ast::CompOp::#variant(Default::default())));
635680
}
@@ -679,7 +724,8 @@ pub(crate) mod parse_impl {
679724
for segment in self.segments.iter() {
680725
out.extend(quote!(#segment,));
681726
}
682-
tokens.extend(quote!(::jsonpath_ast::ast::SingularQuerySegments::new(Vec::from([#out]))));
727+
tokens
728+
.extend(quote!(::jsonpath_ast::ast::SingularQuerySegments::new(Vec::from([#out]))));
683729
}
684730
}
685731

@@ -777,9 +823,15 @@ pub(crate) mod parse_impl {
777823
impl ToTokens for Test {
778824
fn to_tokens(&self, tokens: &mut TokenStream) {
779825
let variant = match self {
780-
Test::RelQuery(inner) => { quote!(new_rel_query(#inner)) }
781-
Test::JPQuery(inner) => { quote!(new_jp_query(#inner)) }
782-
Test::FunctionExpr(inner) => { quote!(new_function_expr(#inner)) }
826+
Test::RelQuery(inner) => {
827+
quote!(new_rel_query(#inner))
828+
}
829+
Test::JPQuery(inner) => {
830+
quote!(new_jp_query(#inner))
831+
}
832+
Test::FunctionExpr(inner) => {
833+
quote!(new_function_expr(#inner))
834+
}
783835
};
784836
tokens.extend(quote!(::jsonpath_ast::ast::Test::#variant));
785837
}

0 commit comments

Comments
 (0)