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
use log::warn;
use serde_json::{json, Value};

use crate::graph_description::IpPort;
use crate::node::NodeT;

impl IpPort {
    pub fn new(ip_address: impl Into<String>, port: u16, protocol: impl Into<String>) -> Self {
        let ip_address = ip_address.into();
        let protocol = protocol.into();

        Self {
            node_key: format!("{}{}{}", ip_address, port, protocol),
            ip_address,
            port: port as u32,
            protocol,
        }
    }

    pub fn into_json(self) -> Value {
        json!({
            "node_key": self.node_key,
            "dgraph.type": "IpPort",
            "port": self.port,
            "protocol": self.protocol,
        })
    }
}

impl NodeT for IpPort {
    fn get_asset_id(&self) -> Option<&str> {
        None
    }

    fn set_asset_id(&mut self, _asset_id: impl Into<String>) {
        panic!("Can not set asset_id on IpPort");
    }

    fn get_node_key(&self) -> &str {
        &self.node_key
    }

    fn set_node_key(&mut self, node_key: impl Into<String>) {
        self.node_key = node_key.into();
    }

    fn merge(&mut self, other: &Self) -> bool {
        if self.node_key != other.node_key {
            warn!("Attempted to merge two IpPort Nodes with differing node_keys");
            return false;
        }

        if self.ip_address != other.ip_address {
            warn!("Attempted to merge two IpPort Nodes with differing IPs");
            return false;
        }

        // There is no variable information in an IpPort
        false
    }

    fn merge_into(&mut self, _other: Self) -> bool {
        false
    }
}