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
use hedera_proto::services;

use crate::protobuf::{
    FromProtobuf,
    ToProtobuf,
};
use crate::NodeAddress;

/// A list of nodes and their metadata.
///
/// Response from [`NodeAddressBookQuery`](crate::NodeAddressBookQuery)
#[derive(Clone, Debug)]
pub struct NodeAddressBook {
    /// all the nodes this address book contains.
    pub node_addresses: Vec<NodeAddress>,
}

impl NodeAddressBook {
    /// Create a new `NodeAddressBook` from protobuf-encoded `bytes`.
    ///
    /// # Errors
    /// - [`Error::FromProtobuf`](crate::Error::FromProtobuf) if decoding the bytes fails to produce a valid protobuf.
    /// - [`Error::FromProtobuf`](crate::Error::FromProtobuf) if decoding the protobuf fails.
    pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> {
        FromProtobuf::from_bytes(bytes)
    }

    /// Convert `self` to a protobuf-encoded [`Vec<u8>`].
    #[must_use]
    pub fn to_bytes(&self) -> Vec<u8> {
        ToProtobuf::to_bytes(self)
    }
}

impl FromProtobuf<services::NodeAddressBook> for NodeAddressBook {
    fn from_protobuf(pb: services::NodeAddressBook) -> crate::Result<Self> {
        Ok(Self { node_addresses: Vec::from_protobuf(pb.node_address)? })
    }
}

impl ToProtobuf for NodeAddressBook {
    type Protobuf = services::NodeAddressBook;

    fn to_protobuf(&self) -> Self::Protobuf {
        services::NodeAddressBook { node_address: self.node_addresses.to_protobuf() }
    }
}