use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum EdgeType {
Calls = 0,
Imports = 1,
Inherits = 2,
Implements = 3,
Overrides = 4,
Contains = 5,
References = 6,
Tests = 7,
Documents = 8,
Configures = 9,
CouplesWith = 10,
BreaksWith = 11,
PatternOf = 12,
VersionOf = 13,
FfiBinds = 14,
UsesType = 15,
Returns = 16,
ParamType = 17,
}
impl EdgeType {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Calls),
1 => Some(Self::Imports),
2 => Some(Self::Inherits),
3 => Some(Self::Implements),
4 => Some(Self::Overrides),
5 => Some(Self::Contains),
6 => Some(Self::References),
7 => Some(Self::Tests),
8 => Some(Self::Documents),
9 => Some(Self::Configures),
10 => Some(Self::CouplesWith),
11 => Some(Self::BreaksWith),
12 => Some(Self::PatternOf),
13 => Some(Self::VersionOf),
14 => Some(Self::FfiBinds),
15 => Some(Self::UsesType),
16 => Some(Self::Returns),
17 => Some(Self::ParamType),
_ => None,
}
}
pub fn is_dependency(&self) -> bool {
matches!(
self,
Self::Calls
| Self::Imports
| Self::Inherits
| Self::Implements
| Self::UsesType
| Self::FfiBinds
)
}
pub fn is_temporal(&self) -> bool {
matches!(self, Self::CouplesWith | Self::BreaksWith | Self::VersionOf)
}
pub fn label(&self) -> &'static str {
match self {
Self::Calls => "calls",
Self::Imports => "imports",
Self::Inherits => "inherits",
Self::Implements => "implements",
Self::Overrides => "overrides",
Self::Contains => "contains",
Self::References => "references",
Self::Tests => "tests",
Self::Documents => "documents",
Self::Configures => "configures",
Self::CouplesWith => "couples_with",
Self::BreaksWith => "breaks_with",
Self::PatternOf => "pattern_of",
Self::VersionOf => "version_of",
Self::FfiBinds => "ffi_binds",
Self::UsesType => "uses_type",
Self::Returns => "returns",
Self::ParamType => "param_type",
}
}
pub const COUNT: usize = 18;
}
impl std::fmt::Display for EdgeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.label())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Edge {
pub source_id: u64,
pub target_id: u64,
pub edge_type: EdgeType,
pub weight: f32,
pub created_at: u64,
pub context: u32,
}
impl Edge {
pub fn new(source_id: u64, target_id: u64, edge_type: EdgeType) -> Self {
Self {
source_id,
target_id,
edge_type,
weight: 1.0,
created_at: crate::types::now_micros(),
context: 0,
}
}
pub fn with_weight(mut self, weight: f32) -> Self {
self.weight = weight.clamp(0.0, 1.0);
self
}
pub fn with_context(mut self, context: u32) -> Self {
self.context = context;
self
}
pub fn is_self_edge(&self) -> bool {
self.source_id == self.target_id
}
}