h3ron_graph/graph/
mod.rs

1use serde::Serialize;
2
3use crate::error::Error;
4pub use h3edge::{H3EdgeGraph, H3EdgeGraphBuilder};
5use h3ron::{H3Cell, H3DirectedEdge};
6use node::NodeType;
7pub use prepared::PreparedH3EdgeGraph;
8
9use crate::graph::longedge::LongEdge;
10
11pub mod h3edge;
12pub mod longedge;
13pub mod modifiers;
14pub mod node;
15pub mod prepared;
16
17#[derive(Serialize)]
18pub struct GraphStats {
19    pub h3_resolution: u8,
20    pub num_nodes: usize,
21    pub num_edges: usize,
22}
23
24pub trait GetStats {
25    fn get_stats(&self) -> Result<GraphStats, Error>;
26}
27
28pub trait GetCellNode {
29    fn get_cell_node(&self, cell: &H3Cell) -> Option<NodeType>;
30}
31
32pub trait IterateCellNodes<'a> {
33    type CellNodeIterator;
34    fn iter_cell_nodes(&'a self) -> Self::CellNodeIterator;
35}
36
37pub trait GetCellEdges {
38    type EdgeWeightType;
39
40    /// get all edges and their values originating from cell `cell`
41    #[allow(clippy::complexity)]
42    fn get_edges_originating_from(
43        &self,
44        cell: &H3Cell,
45    ) -> Result<Vec<(H3DirectedEdge, EdgeWeight<Self::EdgeWeightType>)>, Error>;
46}
47
48pub trait GetEdge {
49    type EdgeWeightType;
50
51    fn get_edge(
52        &self,
53        edge: &H3DirectedEdge,
54    ) -> Result<Option<EdgeWeight<Self::EdgeWeightType>>, Error>;
55}
56
57impl<G> GetEdge for G
58where
59    G: GetCellEdges,
60{
61    type EdgeWeightType = G::EdgeWeightType;
62
63    fn get_edge(
64        &self,
65        edge: &H3DirectedEdge,
66    ) -> Result<Option<EdgeWeight<Self::EdgeWeightType>>, Error> {
67        let cell = edge.origin_cell()?;
68        for (found_edge, value) in self.get_edges_originating_from(&cell)? {
69            if edge == &found_edge {
70                return Ok(Some(value));
71            }
72        }
73        Ok(None)
74    }
75}
76
77#[derive(Clone)]
78pub struct EdgeWeight<'a, W> {
79    pub weight: W,
80
81    pub longedge: Option<(&'a LongEdge, W)>,
82}
83
84impl<'a, W> From<W> for EdgeWeight<'a, W> {
85    fn from(weight: W) -> Self {
86        EdgeWeight {
87            weight,
88            longedge: None,
89        }
90    }
91}