Skip to main content

graphrecords_query/operations/traversal/
mod.rs

1mod edges;
2mod endpoint;
3mod neighbors;
4mod nodes;
5mod via_edges;
6mod via_neighbors;
7mod via_nodes;
8
9use crate::{BoxedIterator, registry::OperationManifest};
10pub use edges::EdgesOperation;
11pub use endpoint::EndpointOperation;
12use graphrecords_core::{
13    GraphRecord,
14    graphrecord::{EdgeIndex, NodeIndex},
15};
16pub use neighbors::NeighborsOperation;
17pub use nodes::NodesOperation;
18use std::fmt::{self, Display, Formatter};
19pub use via_edges::ViaEdgesOperation;
20pub use via_neighbors::ViaNeighborsOperation;
21pub use via_nodes::ViaNodesOperation;
22
23pub(super) fn operation_manifests() -> Vec<OperationManifest> {
24    vec![
25        edges::operation_manifest(),
26        neighbors::operation_manifest(),
27        nodes::operation_manifest(),
28        via_edges::operation_manifest(),
29        via_neighbors::operation_manifest(),
30        via_nodes::operation_manifest(),
31        endpoint::via_source_node::operation_manifest(),
32        endpoint::via_target_node::operation_manifest(),
33    ]
34}
35
36#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
37pub enum EdgeDirection {
38    Incoming,
39    Outgoing,
40    Both,
41}
42
43impl EdgeDirection {
44    fn edges_for_node<'a>(
45        self,
46        graphrecord: &'a GraphRecord,
47        node: &'a NodeIndex,
48    ) -> BoxedIterator<'a, &'a EdgeIndex> {
49        match self {
50            Self::Outgoing => Box::new(graphrecord.outgoing_edges(node).expect("Node must exist")),
51            Self::Incoming => Box::new(graphrecord.incoming_edges(node).expect("Node must exist")),
52            Self::Both => Box::new(
53                graphrecord
54                    .outgoing_edges(node)
55                    .expect("Node must exist")
56                    .chain(graphrecord.incoming_edges(node).expect("Node must exist")),
57            ),
58        }
59    }
60
61    fn neighbors_for_node<'a>(
62        self,
63        graphrecord: &'a GraphRecord,
64        node: &'a NodeIndex,
65    ) -> BoxedIterator<'a, &'a NodeIndex> {
66        match self {
67            Self::Outgoing => Box::new(
68                graphrecord
69                    .outgoing_neighbors(node)
70                    .expect("Node must exist"),
71            ),
72            Self::Incoming => Box::new(
73                graphrecord
74                    .incoming_neighbors(node)
75                    .expect("Node must exist"),
76            ),
77            Self::Both => Box::new(graphrecord.neighbors(node).expect("Node must exist")),
78        }
79    }
80}
81
82impl Display for EdgeDirection {
83    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
84        match self {
85            Self::Incoming => formatter.write_str("incoming"),
86            Self::Outgoing => formatter.write_str("outgoing"),
87            Self::Both => formatter.write_str("both"),
88        }
89    }
90}