open_pql/pql_parser/ast/
selector.rs1use super::*;
2
3#[derive(PartialEq, Eq, Debug, Display)]
4pub enum SelectorKind {
5 Avg,
6 Count,
7 Max,
8 Min,
9}
10
11#[derive(PartialEq, Eq, Debug)]
12pub struct Selector<'i> {
13 pub kind: SelectorKind,
14 pub expr: Expr<'i>,
15 pub alias: Option<Ident<'i>>,
16}
17
18impl<'i> Selector<'i> {
19 pub fn new(
20 kind: &Ident<'i>,
21 expr: Expr<'i>,
22 alias: Option<Ident<'i>>,
23 ) -> ResultE<'i, Self> {
24 let kind = match kind.inner.to_ascii_lowercase().as_str() {
25 "avg" => SelectorKind::Avg,
26 "count" => SelectorKind::Count,
27 "max" => SelectorKind::Max,
28 "min" => SelectorKind::Min,
29 _ => return Err(Error::UnrecognizedSelector(kind.loc).into()),
30 };
31
32 Ok(Self { kind, expr, alias })
33 }
34}
35
36#[cfg_attr(coverage_nightly, coverage(off))]
37#[cfg(test)]
38mod tests {
39 use super::{super::super::parser::*, *};
40
41 fn s(s: &str) -> Selector {
42 SelectorParser::new().parse(s).unwrap()
43 }
44
45 fn e(s: &str) -> Error {
46 SelectorParser::new().parse(s).unwrap_err().into()
47 }
48
49 #[test]
50 fn test_selector() {
51 let obj1 = s("avg(equity(hero, river))");
52 let obj2 = s("avg(equity(hero, river)) as s1");
53
54 assert_eq!(obj1.kind, obj2.kind);
55 assert_eq!(obj1.expr, obj2.expr);
56
57 assert_eq!(obj1.kind, SelectorKind::Avg);
58 assert![matches!(obj1.expr, Expr::FnCall(_))];
59 assert_eq!(obj1.alias, None);
60
61 assert_eq!(obj2.alias, Some(("s1", (28, 30)).into()));
62 }
63
64 #[test]
65 fn test_selector_err() {
66 assert_eq!(e("invalid(_)"), Error::UnrecognizedSelector((0, 7)));
67 }
68}