1use nom::{
7 branch::alt,
8 character::complete::multispace1,
9 combinator::{all_consuming, map, opt},
10 sequence::preceded,
11 IResult, Parser,
12};
13
14use crate::ast::{OrderBy, OrderDirection, *};
15use crate::error::{ParseError, Result};
16
17mod expressions;
18mod identifiers;
19mod literals;
20mod operators;
21mod utils;
22
23use expressions::{parse_comma_sequence, parse_if};
24use identifiers::parse_identifier;
25use utils::keyword;
26
27pub struct FilterParser {
29 }
31
32impl FilterParser {
33 pub fn new() -> Self {
35 Self {}
36 }
37
38 pub fn parse(&self, input: &str) -> Result<Filter> {
40 let input = input.trim();
41 if input.is_empty() {
42 return Err(ParseError::EmptyInput);
43 }
44
45 match all_consuming(parse_filter).parse(input) {
46 Ok((_, filter)) => Ok(filter),
47 Err(e) => Err(ParseError::from(e)),
48 }
49 }
50}
51
52fn parse_filter(input: &str) -> IResult<&str, Filter> {
54 all_consuming(map(alt((parse_if, parse_comma_sequence)), |expr| Filter {
55 expr,
56 }))
57 .parse(input)
58}
59
60#[allow(dead_code)]
62fn parse_order_by(input: &str) -> IResult<&str, OrderBy> {
63 map(
64 (
65 parse_identifier,
66 opt(preceded(
67 multispace1,
68 alt((
69 map(keyword("asc"), |_| OrderDirection::Asc),
70 map(keyword("desc"), |_| OrderDirection::Desc),
71 )),
72 )),
73 ),
74 |(column, direction)| OrderBy {
75 column,
76 direction: direction.unwrap_or(OrderDirection::Asc),
77 },
78 )
79 .parse(input)
80}
81
82impl Default for FilterParser {
83 fn default() -> Self {
84 Self::new()
85 }
86}