Skip to main content

neco_nodegraph/
id.rs

1use alloc::string::{String, ToString};
2use core::fmt;
3
4use crate::error::GraphError;
5
6/// Opaque identifier for a node.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct NodeId(u64);
9
10impl NodeId {
11    /// Creates a node identifier from a raw value.
12    pub const fn new(value: u64) -> Self {
13        Self(value)
14    }
15
16    /// Returns the raw numeric value.
17    pub const fn as_u64(self) -> u64 {
18        self.0
19    }
20}
21
22impl fmt::Display for NodeId {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "{}", self.0)
25    }
26}
27
28/// Opaque identifier for an edge.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
30pub struct EdgeId(u64);
31
32impl EdgeId {
33    /// Creates an edge identifier from a raw value.
34    pub const fn new(value: u64) -> Self {
35        Self(value)
36    }
37
38    /// Returns the raw numeric value.
39    pub const fn as_u64(self) -> u64 {
40        self.0
41    }
42}
43
44impl fmt::Display for EdgeId {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(f, "{}", self.0)
47    }
48}
49
50/// Opaque identifier for a node port.
51#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
52pub struct PortId(String);
53
54impl PortId {
55    /// Creates a port identifier. Empty strings are rejected.
56    pub fn new(value: &str) -> Result<Self, GraphError> {
57        if value.is_empty() {
58            return Err(GraphError::EmptyPortId);
59        }
60        Ok(Self(value.to_string()))
61    }
62
63    /// Returns the underlying string slice.
64    pub fn as_str(&self) -> &str {
65        self.0.as_str()
66    }
67}
68
69impl fmt::Display for PortId {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        f.write_str(self.as_str())
72    }
73}