1use petgraph::Directed;
20use petgraph::stable_graph::{EdgeIndex, NodeIndex};
21use rkyv::{
22 Archive, Archived, Deserialize, Place, Resolver, Serialize,
23 rancor::Fallible,
24 with::{ArchiveWith, DeserializeWith, SerializeWith},
25};
26use uuid::Uuid;
27
28pub type NodeKey = NodeIndex;
30
31pub type EdgeKey = EdgeIndex;
33
34pub type GraphDirection = Directed;
36
37pub type GraphIndex = petgraph::graph::DefaultIx;
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
49pub struct GraphViewId(Uuid);
50
51impl GraphViewId {
52 #[cfg(not(target_arch = "wasm32"))]
57 pub fn new() -> Self {
58 Self(Uuid::new_v4())
59 }
60
61 pub fn from_uuid(id: Uuid) -> Self {
62 Self(id)
63 }
64
65 pub fn as_uuid(self) -> Uuid {
66 self.0
67 }
68}
69
70#[cfg(not(target_arch = "wasm32"))]
71impl Default for GraphViewId {
72 fn default() -> Self {
73 Self::new()
74 }
75}
76
77pub(crate) struct UuidAsBytes;
78
79impl ArchiveWith<Uuid> for UuidAsBytes {
80 type Archived = Archived<[u8; 16]>;
81 type Resolver = Resolver<[u8; 16]>;
82
83 fn resolve_with(field: &Uuid, resolver: Self::Resolver, out: Place<Self::Archived>) {
84 let bytes = *field.as_bytes();
85 bytes.resolve(resolver, out);
86 }
87}
88
89impl<S> SerializeWith<Uuid, S> for UuidAsBytes
90where
91 S: Fallible + ?Sized,
92 [u8; 16]: Serialize<S>,
93{
94 fn serialize_with(field: &Uuid, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
95 let bytes = *field.as_bytes();
96 bytes.serialize(serializer)
97 }
98}
99
100impl<D> DeserializeWith<Archived<[u8; 16]>, Uuid, D> for UuidAsBytes
101where
102 D: Fallible + ?Sized,
103 Archived<[u8; 16]>: Deserialize<[u8; 16], D>,
104{
105 fn deserialize_with(
106 field: &Archived<[u8; 16]>,
107 deserializer: &mut D,
108 ) -> Result<Uuid, D::Error> {
109 let bytes = field.deserialize(deserializer)?;
110 Ok(Uuid::from_bytes(bytes))
111 }
112}
113
114pub(crate) struct LogIdAsString;
127
128impl ArchiveWith<codicil::LogId> for LogIdAsString {
129 type Archived = Archived<String>;
130 type Resolver = Resolver<String>;
131
132 fn resolve_with(field: &codicil::LogId, resolver: Self::Resolver, out: Place<Self::Archived>) {
133 field.as_str().to_string().resolve(resolver, out);
134 }
135}
136
137impl<S> SerializeWith<codicil::LogId, S> for LogIdAsString
138where
139 S: Fallible + ?Sized,
140 String: Serialize<S>,
141{
142 fn serialize_with(
143 field: &codicil::LogId,
144 serializer: &mut S,
145 ) -> Result<Self::Resolver, S::Error> {
146 field.as_str().to_string().serialize(serializer)
147 }
148}
149
150impl<D> DeserializeWith<Archived<String>, codicil::LogId, D> for LogIdAsString
151where
152 D: Fallible + ?Sized,
153 Archived<String>: Deserialize<String, D>,
154{
155 fn deserialize_with(
156 field: &Archived<String>,
157 deserializer: &mut D,
158 ) -> Result<codicil::LogId, D::Error> {
159 let raw: String = field.deserialize(deserializer)?;
160 Ok(codicil::LogId::new(raw))
161 }
162}