use crate::message::attributes::*;
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg(feature = "serialization")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
pub struct BgpClusterList {
pub value: Vec<std::net::IpAddr>,
}
impl BgpClusterList {
pub fn decode_from(peer: &BgpSessionParams, buf: &[u8]) -> Result<BgpClusterList, BgpError> {
let mut pos: usize = 0;
let mut v = Vec::new();
let itemsize = match peer.peer_mode {
BgpTransportMode::IPv4 => 4,
BgpTransportMode::IPv6 => 16,
};
while (pos + itemsize) <= buf.len() {
v.push(decode_addr_from(&buf[pos..(pos + itemsize)])?);
pos += itemsize;
}
Ok(BgpClusterList { value: v })
}
}
impl std::fmt::Debug for BgpClusterList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BgpClusterList")
.field("value", &self.value)
.finish()
}
}
impl std::fmt::Display for BgpClusterList {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
crate::util::fmt_vec(self.value.iter(), ',', f)
}
}
impl BgpAttr for BgpClusterList {
fn attr(&self) -> BgpAttrParams {
BgpAttrParams {
typecode: 10,
flags: 0x80,
}
}
fn encode_to(&self, _peer: &BgpSessionParams, buf: &mut [u8]) -> Result<usize, BgpError> {
let mut pos: usize = 0;
for i in &self.value {
pos += encode_addr_to(i, &mut buf[pos..])?;
}
Ok(pos)
}
}