Skip to main content

khive_query/
ast.rs

1//! GQL abstract syntax tree.
2
3use std::collections::HashMap;
4
5#[derive(Debug, Clone)]
6pub struct GqlQuery {
7    pub pattern: MatchPattern,
8    pub where_clause: Vec<Condition>,
9    pub return_items: Vec<String>,
10    pub limit: Option<usize>,
11}
12
13#[derive(Debug, Clone)]
14pub struct MatchPattern {
15    pub elements: Vec<PatternElement>,
16}
17
18impl MatchPattern {
19    pub fn nodes(&self) -> impl Iterator<Item = &NodePattern> {
20        self.elements.iter().filter_map(|e| match e {
21            PatternElement::Node(n) => Some(n),
22            _ => None,
23        })
24    }
25
26    pub fn edges(&self) -> impl Iterator<Item = &EdgePattern> {
27        self.elements.iter().filter_map(|e| match e {
28            PatternElement::Edge(e) => Some(e),
29            _ => None,
30        })
31    }
32
33    pub fn has_variable_length(&self) -> bool {
34        self.edges().any(|e| e.max_hops > 1)
35    }
36}
37
38#[derive(Debug, Clone)]
39pub enum PatternElement {
40    Node(NodePattern),
41    Edge(EdgePattern),
42}
43
44#[derive(Debug, Clone)]
45pub struct NodePattern {
46    pub variable: Option<String>,
47    pub kind: Option<String>,
48    pub properties: HashMap<String, String>,
49}
50
51#[derive(Debug, Clone)]
52pub struct EdgePattern {
53    pub variable: Option<String>,
54    pub relations: Vec<String>,
55    pub direction: EdgeDirection,
56    pub min_hops: usize,
57    pub max_hops: usize,
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum EdgeDirection {
62    Out,
63    In,
64    Both,
65}
66
67#[derive(Debug, Clone)]
68pub struct Condition {
69    pub variable: String,
70    pub property: String,
71    pub op: CompareOp,
72    pub value: ConditionValue,
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub enum CompareOp {
77    Eq,
78    Neq,
79    Gt,
80    Lt,
81    Gte,
82    Lte,
83    Like,
84}
85
86#[derive(Debug, Clone)]
87pub enum ConditionValue {
88    String(String),
89    Number(f64),
90    Bool(bool),
91}