1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::structure::{Property, Vertex, GID};
use std::collections::HashMap;
use std::hash::Hasher;

#[derive(Debug, Clone)]
pub struct Edge {
    id: GID,
    label: String,
    in_v: Vertex,
    out_v: Vertex,
    properties: HashMap<String, Property>,
}

impl Edge {
    pub(crate) fn new<T>(
        id: GID,
        label: T,
        in_v_id: GID,
        in_v_label: T,
        out_v_id: GID,
        out_v_label: T,
        properties: HashMap<String, Property>,
    ) -> Edge
    where
        T: Into<String>,
    {
        Edge {
            id,
            label: label.into(),
            in_v: Vertex::new(in_v_id, in_v_label, HashMap::new()),
            out_v: Vertex::new(out_v_id, out_v_label, HashMap::new()),
            properties,
        }
    }

    pub fn id(&self) -> &GID {
        &self.id
    }

    pub fn label(&self) -> &String {
        &self.label
    }

    pub fn in_v(&self) -> &Vertex {
        &self.in_v
    }
    pub fn out_v(&self) -> &Vertex {
        &self.out_v
    }

    pub fn property(&self, key: &str) -> Option<&Property> {
        self.properties.get(key)
    }
}

impl std::cmp::Eq for Edge {}

impl PartialEq for Edge {
    fn eq(&self, other: &Edge) -> bool {
        &self.id == other.id()
    }
}

impl std::hash::Hash for Edge {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}