Skip to main content

linkrs_query/optimizer/analysis/required_properties/
requirement.rs

1//! Property requirement for a single variable binding.
2
3use std::collections::BTreeSet;
4
5/// Property requirement for one variable binding.
6#[derive(Debug, Clone, Default, PartialEq, Eq)]
7pub struct PropertyRequirement {
8    /// Variable (alias) the properties belong to.
9    pub alias: String,
10    /// Resolved tag / edge type when the binding is single-typed.
11    pub tag_name: Option<String>,
12    /// Property names demanded of the binding.
13    pub prop_names: BTreeSet<String>,
14    /// The variable is consumed in a non-prunable position (bare reference,
15    /// opaque object, function argument, ...): its whole value is needed,
16    /// so the binding must not be narrowed. Sticky: once set it is never
17    /// cleared by later property references.
18    pub full_value: bool,
19}
20
21impl PropertyRequirement {
22    pub fn new(alias: impl Into<String>) -> Self {
23        Self {
24            alias: alias.into(),
25            ..Default::default()
26        }
27    }
28
29    /// Merge another requirement (same alias) into this one.
30    pub fn merge(&mut self, other: &PropertyRequirement) {
31        self.tag_name = other.tag_name.clone().or_else(|| self.tag_name.clone());
32        self.prop_names.extend(other.prop_names.iter().cloned());
33        self.full_value |= other.full_value;
34    }
35
36    /// Whether the binding is narrowable at all.
37    pub fn is_narrowable(&self) -> bool {
38        !self.full_value && !self.prop_names.is_empty()
39    }
40}