use super::property::{PropertyMap, PropertyValue};
use serde::{Deserialize, Serialize};
pub type NodeId = u64;
pub type EdgeId = u64;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NodeType {
CodeFile,
Function,
Class,
Module,
Variable,
Type,
Interface,
Generic,
}
impl std::fmt::Display for NodeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NodeType::CodeFile => write!(f, "CodeFile"),
NodeType::Function => write!(f, "Function"),
NodeType::Class => write!(f, "Class"),
NodeType::Module => write!(f, "Module"),
NodeType::Variable => write!(f, "Variable"),
NodeType::Type => write!(f, "Type"),
NodeType::Interface => write!(f, "Interface"),
NodeType::Generic => write!(f, "Generic"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EdgeType {
Imports,
ImportsFrom,
Contains,
Calls,
Invokes,
Instantiates,
Extends,
Implements,
Uses,
Defines,
References,
}
impl std::fmt::Display for EdgeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EdgeType::Imports => write!(f, "Imports"),
EdgeType::ImportsFrom => write!(f, "ImportsFrom"),
EdgeType::Contains => write!(f, "Contains"),
EdgeType::Calls => write!(f, "Calls"),
EdgeType::Invokes => write!(f, "Invokes"),
EdgeType::Instantiates => write!(f, "Instantiates"),
EdgeType::Extends => write!(f, "Extends"),
EdgeType::Implements => write!(f, "Implements"),
EdgeType::Uses => write!(f, "Uses"),
EdgeType::Defines => write!(f, "Defines"),
EdgeType::References => write!(f, "References"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Direction {
Outgoing,
Incoming,
Both,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Node {
pub id: NodeId,
pub node_type: NodeType,
pub properties: PropertyMap,
}
impl Node {
pub fn new(id: NodeId, node_type: NodeType, properties: PropertyMap) -> Self {
Self {
id,
node_type,
properties,
}
}
pub fn set_property(&mut self, key: impl Into<String>, value: impl Into<PropertyValue>) {
self.properties.insert(key, value);
}
pub fn get_property(&self, key: &str) -> Option<&PropertyValue> {
self.properties.get(key)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Edge {
pub id: EdgeId,
pub source_id: NodeId,
pub target_id: NodeId,
pub edge_type: EdgeType,
pub properties: PropertyMap,
}
impl Edge {
pub fn new(
id: EdgeId,
source_id: NodeId,
target_id: NodeId,
edge_type: EdgeType,
properties: PropertyMap,
) -> Self {
Self {
id,
source_id,
target_id,
edge_type,
properties,
}
}
pub fn set_property(&mut self, key: impl Into<String>, value: impl Into<PropertyValue>) {
self.properties.insert(key, value);
}
pub fn get_property(&self, key: &str) -> Option<&PropertyValue> {
self.properties.get(key)
}
}