Skip to main content

kernel/graph/snapshot/
mod.rs

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