plexus/graph/mutation/
vertex.rs

1use crate::geometry::Geometry;
2use crate::graph::container::{Bind, Consistent, Core, Reborrow};
3use crate::graph::mutation::alias::Mutable;
4use crate::graph::mutation::edge::{self, EdgeRemoveCache};
5use crate::graph::mutation::{Mutate, Mutation};
6use crate::graph::payload::VertexPayload;
7use crate::graph::storage::convert::AsStorage;
8use crate::graph::storage::{ArcKey, Storage, VertexKey};
9use crate::graph::view::convert::FromKeyedSource;
10use crate::graph::view::VertexView;
11use crate::graph::GraphError;
12
13pub struct VertexMutation<G>
14where
15    G: Geometry,
16{
17    storage: Storage<VertexPayload<G>>,
18}
19
20impl<G> VertexMutation<G>
21where
22    G: Geometry,
23{
24    pub fn insert_vertex(&mut self, geometry: G::Vertex) -> VertexKey {
25        self.storage.insert(VertexPayload::new(geometry))
26    }
27
28    pub fn connect_outgoing_arc(&mut self, a: VertexKey, ab: ArcKey) -> Result<(), GraphError> {
29        VertexView::from_keyed_source((a, &mut self.storage))
30            .ok_or_else(|| GraphError::TopologyNotFound)
31            .map(|mut vertex| {
32                vertex.arc = Some(ab);
33            })
34    }
35
36    pub fn disconnect_outgoing_arc(&mut self, a: VertexKey) -> Result<Option<ArcKey>, GraphError> {
37        VertexView::from_keyed_source((a, &mut self.storage))
38            .ok_or_else(|| GraphError::TopologyNotFound)
39            .map(|mut vertex| vertex.arc.take())
40    }
41}
42
43impl<G> AsStorage<VertexPayload<G>> for VertexMutation<G>
44where
45    G: Geometry,
46{
47    fn as_storage(&self) -> &Storage<VertexPayload<G>> {
48        &self.storage
49    }
50}
51
52impl<G> Mutate for VertexMutation<G>
53where
54    G: Geometry,
55{
56    type Mutant = Core<Storage<VertexPayload<G>>, (), (), ()>;
57    type Error = GraphError;
58
59    fn commit(self) -> Result<Self::Mutant, Self::Error> {
60        let VertexMutation {
61            storage: vertices, ..
62        } = self;
63        Ok(Core::empty().bind(vertices))
64    }
65
66    fn mutate(mutant: Self::Mutant) -> Self {
67        let (vertices, ..) = mutant.into_storage();
68        VertexMutation { storage: vertices }
69    }
70}
71
72pub struct VertexRemoveCache<G>
73where
74    G: Geometry,
75{
76    cache: Vec<EdgeRemoveCache<G>>,
77}
78
79impl<G> VertexRemoveCache<G>
80where
81    G: Geometry,
82{
83    pub fn snapshot<M>(storage: M, a: VertexKey) -> Result<Self, GraphError>
84    where
85        M: Reborrow,
86        M::Target: AsStorage<VertexPayload<G>> + Consistent,
87    {
88        unimplemented!()
89    }
90}
91
92pub fn remove_with_cache<M, N, G>(
93    mut mutation: N,
94    cache: VertexRemoveCache<G>,
95) -> Result<VertexPayload<G>, GraphError>
96where
97    N: AsMut<Mutation<M, G>>,
98    M: Mutable<G>,
99    G: Geometry,
100{
101    let VertexRemoveCache { cache } = cache;
102    for cache in cache {
103        edge::remove_with_cache(mutation.as_mut(), cache)?;
104    }
105    unimplemented!()
106}