use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::OPERATOR_ENVELOPE_V1;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OperatorSpec {
pub binary_id: String,
pub operator: String,
pub layer: String,
pub class: String,
#[serde(default)]
pub parent: String,
#[serde(rename = "crate")]
pub crate_name: String,
pub package: String,
#[serde(default)]
pub telemetry_fork: String,
pub verify: bool,
pub neo4j_pass: bool,
#[serde(default)]
pub default_limit: Option<usize>,
#[serde(default)]
pub node_types: Vec<String>,
#[serde(default)]
pub relationship_types: Vec<String>,
#[serde(default)]
pub anchor_tags: Vec<String>,
#[serde(default)]
pub result_definition_ref: String,
#[serde(default)]
pub retrieval_step: String,
#[serde(default)]
pub pass_through: bool,
#[serde(default)]
pub property_key: Option<String>,
#[serde(default)]
pub taxonomy: Option<String>,
#[serde(default)]
pub wave: Option<String>,
}
impl OperatorSpec {
pub fn from_catalog_value(v: Value) -> Result<Self, serde_json::Error> {
serde_json::from_value(v)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OperatorNode {
pub id: u64,
#[serde(rename = "kind")]
pub kind: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tags: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OperatorRel {
pub from: u64,
pub to: u64,
#[serde(rename = "type")]
pub rel_type: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AnchorWeight {
pub tag: String,
pub weight: u32,
pub height: u8,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OperatorGraph {
pub class: String,
pub layer: String,
pub weight: u32,
pub height: u8,
pub shape: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub anchors: Vec<AnchorWeight>,
}
impl OperatorGraph {
#[must_use]
pub fn from_spec(spec: &OperatorSpec) -> Self {
let hit = |t: &str| crate::token_stat(t);
let weight = spec
.anchor_tags
.iter()
.chain(spec.node_types.iter())
.chain(spec.relationship_types.iter())
.map(|t| hit(t).0)
.chain(spec.property_key.as_deref().map(|k| hit(k).0))
.max()
.unwrap_or(0);
let height = crate::wave_height(spec.wave.as_deref());
let shape = match weight {
0 => "isolated",
1 => "uncommon",
_ => "common",
}
.to_string();
let anchors = spec
.anchor_tags
.iter()
.map(|t| {
let (w, h) = hit(t);
AnchorWeight { tag: t.clone(), weight: w, height: h }
})
.collect();
Self {
class: spec.class.clone(),
layer: spec.layer.clone(),
weight,
height,
shape,
anchors,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OperatorEnvelope {
pub schema: String,
pub binary_id: String,
pub operator: String,
pub schema_version: String,
#[serde(rename = "crate")]
pub crate_name: String,
pub plan_hash: String,
#[serde(default, skip_serializing_if = "is_unbound")]
pub requirement_id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub subject_ids: Vec<u64>,
#[serde(rename = "resultDefinitionRef")]
pub result_definition_ref: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub anchor_tags: Vec<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub neo4j_hit: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub nodes: Vec<OperatorNode>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub relationships: Vec<OperatorRel>,
#[serde(default, skip_serializing_if = "Map::is_empty")]
pub properties: Map<String, Value>,
pub verify: bool,
pub coverage_state: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_finding_reason: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub limitations: Vec<String>,
pub content_hash: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub graph: Option<OperatorGraph>,
#[serde(skip_serializing_if = "Option::is_none")]
pub telemetry: Option<Value>,
}
pub const ENVELOPE_KEYS: &[(&str, bool)] = &[
("schema", true),
("binary_id", true),
("operator", true),
("schema_version", true),
("crate", true),
("plan_hash", true),
("requirement_id", false),
("subject_ids", false),
("resultDefinitionRef", true),
("anchor_tags", false),
("neo4j_hit", false),
("nodes", false),
("relationships", false),
("properties", false),
("verify", true),
("coverage_state", true),
("no_finding_reason", false),
("limitations", false),
("content_hash", true),
("graph", false),
("telemetry", false),
];
impl OperatorEnvelope {
#[must_use]
pub fn schema_tag() -> &'static str {
OPERATOR_ENVELOPE_V1
}
#[must_use]
pub fn has_working_data(&self) -> bool {
!self.nodes.is_empty() || !self.relationships.is_empty() || !self.properties.is_empty()
}
}
fn is_unbound(s: &str) -> bool {
s == "unbound"
}