algae_graph/cmp/
edge.rs

1/*
2    Appellation: edge <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4    Description: an edge consists of two nodes and an optional edge value
5*/
6use super::Pair;
7use crate::Node;
8use serde::{Deserialize, Serialize};
9
10pub trait Related<N: Node, V> {}
11
12#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
13pub struct Edge<N = String, V = i64>
14where
15    N: Node,
16{
17    pair: Pair<N>,
18    weight: V,
19}
20
21impl<N, V> Edge<N, V>
22where
23    N: Node,
24{
25    pub fn new(a: N, b: N, weight: V) -> Self {
26        Self {
27            pair: Pair::new(a, b),
28            weight,
29        }
30    }
31    pub fn pair(&self) -> Pair<N> {
32        self.pair.clone()
33    }
34    pub fn value(&self) -> &V {
35        &self.weight
36    }
37}
38
39impl<N, V> From<(N, N, V)> for Edge<N, V>
40where
41    N: Node,
42{
43    fn from(data: (N, N, V)) -> Self {
44        Self::new(data.0, data.1, data.2)
45    }
46}
47
48impl<N, V> From<(Pair<N>, V)> for Edge<N, V>
49where
50    N: Node,
51{
52    fn from(data: (Pair<N>, V)) -> Self {
53        Self {
54            pair: data.0,
55            weight: data.1,
56        }
57    }
58}