sqlparser_mysql/base/
match_type.rs

1use nom::branch::alt;
2use nom::bytes::complete::tag_no_case;
3use nom::character::complete::multispace1;
4use nom::combinator::map;
5use nom::sequence::tuple;
6use nom::IResult;
7use std::fmt::{Display, Formatter};
8
9use base::ParseSQLError;
10
11/// parse `[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]`
12#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
13pub enum MatchType {
14    Full,
15    Partial,
16    Simple,
17}
18
19impl Display for MatchType {
20    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21        match *self {
22            MatchType::Full => write!(f, "MATCH FULL"),
23            MatchType::Partial => write!(f, "MATCH PARTIAL"),
24            MatchType::Simple => write!(f, "MATCH SIMPLE"),
25        }
26    }
27}
28
29impl MatchType {
30    pub fn parse(i: &str) -> IResult<&str, MatchType, ParseSQLError<&str>> {
31        map(
32            tuple((
33                tag_no_case("MATCH"),
34                multispace1,
35                alt((
36                    map(tag_no_case("FULL"), |_| MatchType::Full),
37                    map(tag_no_case("PARTIAL"), |_| MatchType::Partial),
38                    map(tag_no_case("SIMPLE"), |_| MatchType::Simple),
39                )),
40            )),
41            |x| x.2,
42        )(i)
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use base::MatchType;
49
50    #[test]
51    fn parse_algorithm_type() {
52        let str1 = "MATCH Full ";
53        let res1 = MatchType::parse(str1);
54        assert!(res1.is_ok());
55        assert_eq!(res1.unwrap().1, MatchType::Full);
56
57        let str2 = "match PARTIAL";
58        let res2 = MatchType::parse(str2);
59        assert!(res2.is_ok());
60        assert_eq!(res2.unwrap().1, MatchType::Partial);
61
62        let str3 = "match  SIMPLE   ";
63        let res3 = MatchType::parse(str3);
64        assert!(res3.is_ok());
65        assert_eq!(res3.unwrap().1, MatchType::Simple);
66    }
67}