Skip to main content

kernel/graph/snapshot/
mod.rs

1// Copyright 2026 Mark Alan Boykin
2// This Source Code Form is subject to the terms of the Mozilla Public
3// License, v. 2.0. If a copy of the MPL was not distributed with this
4// file, You can obtain one at https://mozilla.org/MPL/2.0/.
5// SPDX-License-Identifier: MPL-2.0
6
7//! Graph snapshot serialization.
8//!
9//! Originally a single `graph/snapshot.rs` (724 LOC); split per the
10//! 2026-05-11 kernel decomposition pass into three sub-modules:
11//!
12//! - [`to`] — `Graph::to_snapshot`
13//! - [`from`] — `Graph::from_snapshot`
14//!
15//! This `mod.rs` keeps the small shared pieces: the
16//! `remove_url_mapping` helper (used by both directions),
17//! [`containment_parent_url`] (used by `from_snapshot` and by graph
18//! mutators in `mod.rs`'s impl block), and the rkyv trait impls
19//! (`Archive` / `Serialize` / `Deserialize`) plus `impl Default for
20//! Graph`, all of which are short standalone items.
21
22use rkyv::{Archive, Archived, Deserialize, Place, Resolver, Serialize, rancor::Fallible};
23
24use super::Graph;
25use super::identity::NodeKey;
26use crate::persistence::GraphSnapshot;
27
28mod checked;
29pub mod from;
30pub mod to;
31
32pub use checked::ResourceSnapshotError;
33
34pub(crate) use from::{payload_from_persisted, payload_from_persisted_at};
35pub(crate) use to::persisted_edge_for_ids;
36
37#[cfg(test)]
38mod handle_tests;
39#[cfg(test)]
40mod bounded_resource_tests;
41#[cfg(test)]
42mod tests;
43
44impl Graph {
45    pub(crate) fn remove_url_mapping(&mut self, url: &str, key: NodeKey) {
46        if let Some(keys) = self.url_to_nodes.get_mut(url) {
47            keys.retain(|candidate| *candidate != key);
48            if keys.is_empty() {
49                self.url_to_nodes.remove(url);
50            }
51        }
52    }
53}
54
55pub(crate) fn containment_parent_url(url: &url::Url) -> Option<String> {
56    if !matches!(url.scheme(), "http" | "https" | "file") {
57        return None;
58    }
59
60    let mut parent = url.clone();
61    parent.set_query(None);
62    parent.set_fragment(None);
63
64    let mut segments: Vec<String> = parent
65        .path_segments()
66        .map(|parts| {
67            parts
68                .filter(|segment| !segment.is_empty())
69                .map(ToString::to_string)
70                .collect::<Vec<_>>()
71        })
72        .unwrap_or_default();
73
74    if segments.is_empty() {
75        return None;
76    }
77    segments.pop();
78
79    let parent_path = if segments.is_empty() {
80        "/".to_string()
81    } else {
82        format!("/{}/", segments.join("/"))
83    };
84
85    parent.set_path(&parent_path);
86    Some(parent.to_string())
87}
88
89impl Default for Graph {
90    fn default() -> Self {
91        Self::new()
92    }
93}
94
95impl Archive for Graph {
96    type Archived = Archived<GraphSnapshot>;
97    type Resolver = Resolver<GraphSnapshot>;
98
99    fn resolve(&self, resolver: Self::Resolver, out: Place<Self::Archived>) {
100        let snapshot = self.to_snapshot();
101        snapshot.resolve(resolver, out);
102    }
103}
104
105impl<S> Serialize<S> for Graph
106where
107    S: Fallible + ?Sized,
108    GraphSnapshot: Serialize<S>,
109{
110    fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
111        let snapshot = self.to_snapshot();
112        snapshot.serialize(serializer)
113    }
114}
115
116impl<D> Deserialize<Graph, D> for Archived<GraphSnapshot>
117where
118    D: Fallible + ?Sized,
119    Archived<GraphSnapshot>: Deserialize<GraphSnapshot, D>,
120{
121    fn deserialize(&self, deserializer: &mut D) -> Result<Graph, D::Error> {
122        let snapshot = <Archived<GraphSnapshot> as Deserialize<GraphSnapshot, D>>::deserialize(
123            self,
124            deserializer,
125        )?;
126        Ok(Graph::from_snapshot(&snapshot))
127    }
128}