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
69
70
71
72
73
74
75
use ockam_core::{
    compat::vec::Vec,
    errcode::{Kind, Origin},
    Decodable, Encodable, Error, LocalInfo, LocalMessage, Result, TransportType,
};
use serde::{Deserialize, Serialize};

/// Node LocalInfo unique Identifier
pub const EXTERNAL_IDENTIFIER: &str = "EXTERNAL_IDENTIFIER";

/// Used for LocalMessage that originates from outside the node (e.g. received from transport)
#[derive(Serialize, Deserialize)]
pub struct ExternalLocalInfo {
    transport_type: TransportType,
}

impl ExternalLocalInfo {
    /// Convert from LocalInfo
    pub fn from_local_info(value: &LocalInfo) -> Result<Self> {
        if value.type_identifier() != EXTERNAL_IDENTIFIER {
            return Err(Error::new_without_cause(Origin::Node, Kind::Invalid));
        }

        if let Ok(info) = ExternalLocalInfo::decode(value.data()) {
            return Ok(info);
        }

        Err(Error::new_without_cause(Origin::Node, Kind::Invalid))
    }

    /// Convert to LocalInfo
    pub fn to_local_info(&self) -> Result<LocalInfo> {
        Ok(LocalInfo::new(EXTERNAL_IDENTIFIER.into(), self.encode()?))
    }

    /// Find first such instance in LocalMessage
    pub fn find_info(local_msg: &LocalMessage) -> Result<Self> {
        if let Some(local_info) = local_msg
            .local_info()
            .iter()
            .find(|x| x.type_identifier() == EXTERNAL_IDENTIFIER)
        {
            Self::from_local_info(local_info)
        } else {
            Err(Error::new_without_cause(Origin::Node, Kind::Invalid))
        }
    }

    /// Find all such instances in LocalMessage
    pub fn find_all(local_msg: &LocalMessage) -> Result<Vec<Self>> {
        Ok(local_msg
            .local_info()
            .iter()
            .filter_map(|x| {
                if x.type_identifier() == EXTERNAL_IDENTIFIER {
                    Self::from_local_info(x).ok()
                } else {
                    None
                }
            })
            .collect())
    }
}

impl ExternalLocalInfo {
    /// Constructor
    pub fn new(transport_type: TransportType) -> Self {
        Self { transport_type }
    }

    /// Transport type
    pub fn transport_type(&self) -> TransportType {
        self.transport_type
    }
}