stack_graphs/serde/
stitching.rs1use crate::graph::StackGraph;
9use crate::partial::PartialPaths;
10
11use super::Error;
12use super::Filter;
13use super::ImplicationFilter;
14use super::NoFilter;
15use super::PartialPath;
16
17#[derive(PartialEq, Eq, Debug, Clone)]
18#[cfg_attr(
19 feature = "serde",
20 derive(serde::Deserialize, serde::Serialize),
21 serde(transparent)
22)]
23pub struct Database {
24 paths: Vec<PartialPath>,
25}
26
27impl Database {
28 pub fn from_database(
29 graph: &crate::graph::StackGraph,
30 partials: &mut PartialPaths,
31 value: &crate::stitching::Database,
32 ) -> Self {
33 Self::from_database_filter(graph, partials, value, &NoFilter)
34 }
35
36 pub fn from_database_filter(
37 graph: &crate::graph::StackGraph,
38 partials: &mut PartialPaths,
39 value: &crate::stitching::Database,
40 filter: &dyn Filter,
41 ) -> Self {
42 let filter = ImplicationFilter(filter);
43 let mut paths = Vec::new();
44 for path in value.iter_partial_paths() {
45 let path = &value[path];
46 if !filter.include_partial_path(graph, partials, path) {
47 continue;
48 }
49 let path = PartialPath::from_partial_path(graph, partials, &path);
50 paths.push(path);
51 }
52 Self { paths }
53 }
54
55 pub fn load_into(
56 &self,
57 graph: &mut crate::graph::StackGraph,
58 partials: &mut PartialPaths,
59 value: &mut crate::stitching::Database,
60 ) -> Result<(), Error> {
61 for path in &self.paths {
62 let path = path.to_partial_path(graph, partials)?;
63 value.add_partial_path(graph, partials, path);
64 }
65 Ok(())
66 }
67}
68
69impl crate::stitching::Database {
70 pub fn to_serializable(&self, graph: &StackGraph, partials: &mut PartialPaths) -> Database {
71 Database::from_database(graph, partials, self)
72 }
73
74 pub fn to_serializable_filter(
75 &self,
76 graph: &StackGraph,
77 partials: &mut PartialPaths,
78 filter: &dyn Filter,
79 ) -> Database {
80 Database::from_database_filter(graph, partials, self, filter)
81 }
82}