use super::*;
#[derive(Debug, Clone, PartialEq)]
pub struct PatternPath {
pub variable: Option<SmallName>,
pub subpath_variable: Option<SmallName>,
pub elements: Vec<PatternElement>,
pub where_expr: Option<Expr>,
pub mode: Option<PathMode>,
pub selector: Option<PathSelector>,
pub is_shortest_path_function: bool,
}
impl PatternPath {
pub fn path_names(&self) -> impl Iterator<Item = &SmallName> {
[self.variable.as_ref(), self.subpath_variable.as_ref()]
.into_iter()
.flatten()
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum PatternElement {
Node(NodePattern),
Relationship(RelPattern),
Quantified(QuantifiedPathPattern),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PathQuantifier {
pub min: u32,
pub max: Option<u32>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct QuantifiedPathPattern {
pub subpath_variable: Option<SmallName>,
pub elements: Vec<PatternElement>,
pub where_expr: Option<Expr>,
pub quantifier: PathQuantifier,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NodePattern {
pub variable: Option<SmallName>,
pub label_expr: Option<LabelExpr>,
pub properties: Vec<(SmallName, Expr)>,
pub has_properties: bool,
pub properties_param: Option<Expr>,
pub where_expr: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RelPattern {
pub variable: Option<SmallName>,
pub type_expr: Option<LabelExpr>,
pub properties: Vec<(SmallName, Expr)>,
pub has_properties: bool,
pub properties_param: Option<Expr>,
pub direction: Direction,
pub min_hops: Option<u32>,
pub max_hops: Option<u32>,
pub where_expr: Option<Expr>,
pub quantified: bool,
}
impl RelPattern {
pub fn is_variable_length(&self) -> bool {
self.min_hops.is_some() || self.max_hops.is_some()
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum LabelExpr {
Label(SmallName),
Wildcard,
Dynamic(DynamicLabelExpr),
Not(Box<LabelExpr>),
And(Vec<LabelExpr>),
Or(Vec<LabelExpr>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DynamicLabelQuantifier {
Default,
All,
Any,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DynamicLabelExpr {
pub expr: Box<Expr>,
pub quantifier: DynamicLabelQuantifier,
}
impl LabelExpr {
pub fn as_label_list(&self) -> Option<Vec<SmallName>> {
match self {
LabelExpr::Label(name) => Some(vec![name.clone()]),
LabelExpr::And(items) => {
let mut out = Vec::with_capacity(items.len());
for item in items {
match item {
LabelExpr::Label(name) => out.push(name.clone()),
_ => return None,
}
}
Some(out)
}
_ => None,
}
}
pub fn contains_not_or_wildcard(&self) -> bool {
match self {
LabelExpr::Label(_) | LabelExpr::Dynamic(_) => false,
LabelExpr::Wildcard | LabelExpr::Not(_) => true,
LabelExpr::And(items) | LabelExpr::Or(items) => {
items.iter().any(LabelExpr::contains_not_or_wildcard)
}
}
}
pub fn as_ddl_list(&self) -> Option<Vec<SmallName>> {
match self {
LabelExpr::Label(name) => Some(vec![name.clone()]),
LabelExpr::Or(items) => {
let mut out = Vec::with_capacity(items.len());
for item in items {
match item {
LabelExpr::Label(name) => out.push(name.clone()),
_ => return None,
}
}
Some(out)
}
_ => None,
}
}
pub fn contains_dynamic(&self) -> bool {
match self {
LabelExpr::Label(_) | LabelExpr::Wildcard => false,
LabelExpr::Dynamic(_) => true,
LabelExpr::Not(inner) => inner.contains_dynamic(),
LabelExpr::And(items) | LabelExpr::Or(items) => {
items.iter().any(LabelExpr::contains_dynamic)
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Right, Left, Both, }