Skip to main content

link_cli/
link.rs

1//! Link - A doublet (source, target) pair with an index
2//!
3//! This module provides the core Link data structure that represents
4//! a link in the doublet storage.
5
6/// Link represents a doublet (source, target) pair with an index
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
8pub struct Link {
9    pub index: u32,
10    pub source: u32,
11    pub target: u32,
12}
13
14impl Link {
15    /// Creates a new link with the given index, source, and target
16    pub fn new(index: u32, source: u32, target: u32) -> Self {
17        Self {
18            index,
19            source,
20            target,
21        }
22    }
23
24    /// Returns true if this link is null (all zeros)
25    pub fn is_null(&self) -> bool {
26        self.index == 0 && self.source == 0 && self.target == 0
27    }
28
29    /// Returns true if this is a full point (self-referential link)
30    pub fn is_full_point(&self) -> bool {
31        self.index == self.source && self.source == self.target
32    }
33
34    /// Returns true if this link references itself from at least one side.
35    pub fn is_partial_point(&self) -> bool {
36        self.index == self.source || self.index == self.target
37    }
38
39    /// Formats the link for display
40    pub fn format(&self) -> String {
41        format!("({} {} {})", self.index, self.source, self.target)
42    }
43}
44
45/// Link type from the upstream `doublets` crate used as the Rust basis.
46pub type DoubletsLink = doublets::Link<u32>;
47
48impl From<DoubletsLink> for Link {
49    fn from(link: DoubletsLink) -> Self {
50        Self::new(link.index, link.source, link.target)
51    }
52}
53
54impl From<Link> for DoubletsLink {
55    fn from(link: Link) -> Self {
56        Self::new(link.index, link.source, link.target)
57    }
58}