use crate::Point;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GraphNode {
id: u64,
label: String,
properties: HashMap<String, Value>,
#[serde(skip_serializing_if = "Option::is_none")]
vector: Option<Vec<f32>>,
}
impl GraphNode {
#[must_use]
pub fn new(id: u64, label: &str) -> Self {
Self {
id,
label: label.to_string(),
properties: HashMap::new(),
vector: None,
}
}
#[must_use]
pub fn with_properties(mut self, properties: HashMap<String, Value>) -> Self {
self.properties = properties;
self
}
#[must_use]
pub fn with_vector(mut self, vector: Vec<f32>) -> Self {
self.vector = Some(vector);
self
}
#[must_use]
pub fn id(&self) -> u64 {
self.id
}
#[must_use]
pub fn label(&self) -> &str {
&self.label
}
#[must_use]
pub fn properties(&self) -> &HashMap<String, Value> {
&self.properties
}
#[must_use]
pub fn property(&self, name: &str) -> Option<&Value> {
self.properties.get(name)
}
#[must_use]
pub fn vector(&self) -> Option<&Vec<f32>> {
self.vector.as_ref()
}
pub fn set_property(&mut self, name: &str, value: Value) {
self.properties.insert(name.to_string(), value);
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Element {
Point(Point),
Node(GraphNode),
}
impl Element {
#[must_use]
pub fn id(&self) -> u64 {
match self {
Self::Point(p) => p.id,
Self::Node(n) => n.id(),
}
}
#[must_use]
pub fn is_point(&self) -> bool {
matches!(self, Self::Point(_))
}
#[must_use]
pub fn is_node(&self) -> bool {
matches!(self, Self::Node(_))
}
#[must_use]
pub fn as_point(&self) -> Option<&Point> {
match self {
Self::Point(p) => Some(p),
Self::Node(_) => None,
}
}
#[must_use]
pub fn as_node(&self) -> Option<&GraphNode> {
match self {
Self::Point(_) => None,
Self::Node(n) => Some(n),
}
}
#[must_use]
pub fn has_vector(&self) -> bool {
match self {
Self::Point(_) => true,
Self::Node(n) => n.vector().is_some(),
}
}
#[must_use]
pub fn vector(&self) -> Option<&Vec<f32>> {
match self {
Self::Point(p) => Some(&p.vector),
Self::Node(n) => n.vector(),
}
}
}