Skip to content

Commit 86d52fb

Browse files
committed
Introduce OwnedPathAttributes
1 parent e5fccaf commit 86d52fb

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/bgp/path_attributes.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,64 @@ pub trait AttributeHeader {
9999
}
100100

101101

102+
//------------ OwnedPathAttributes -------------------------------------------
103+
104+
105+
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
106+
pub struct OwnedPathAttributes {
107+
ppi: PduParseInfo,
108+
raw: Vec<u8>
109+
}
110+
111+
impl OwnedPathAttributes {
112+
pub fn new(ppi: PduParseInfo, raw: Vec<u8>) -> Self {
113+
Self { ppi, raw }
114+
}
115+
116+
pub fn iter(&self) -> PathAttributes<Vec<u8>> {
117+
let parser = Parser::from_ref(&self.raw);
118+
PathAttributes::new(parser, self.ppi)
119+
}
120+
121+
pub fn pdu_parse_info(&self) -> PduParseInfo {
122+
self.ppi
123+
}
124+
125+
pub fn into_vec(self) -> Vec<u8> {
126+
self.raw
127+
}
128+
129+
pub fn get<A: FromAttribute>(&self) -> Option<A> {
130+
if let Some(attr_type) = A::attribute_type() {
131+
self.iter().get(attr_type)
132+
.and_then(|a| a.to_owned().ok())
133+
.and_then(|a| A::from_attribute(a))
134+
} else {
135+
None
136+
}
137+
}
138+
}
139+
140+
impl<'a, O> From<PathAttributes<'a, O>> for OwnedPathAttributes
141+
where
142+
O: AsRef<[u8]>
143+
{
144+
fn from(value: PathAttributes<'a, O>) -> Self {
145+
OwnedPathAttributes {
146+
ppi: value.pdu_parse_info,
147+
raw: value.parser.as_slice().to_owned()
148+
}
149+
}
150+
}
151+
152+
impl From<(PduParseInfo, Vec<u8>)> for OwnedPathAttributes {
153+
fn from(value: (PduParseInfo, Vec<u8>)) -> Self {
154+
Self::new(value.0, value.1)
155+
}
156+
}
157+
158+
159+
102160
//------------ PathAttributesBuilder -----------------------------------------
103161

104162
pub type AttributesMap = BTreeMap<u8, PathAttribute>;

0 commit comments

Comments
 (0)