Skip to content

Commit

Permalink
Implement (Partial)Ord for all NLRI types and enum Nlri, derive Copy …
Browse files Browse the repository at this point in the history
…where possible
  • Loading branch information
DRiKE committed Apr 8, 2024
1 parent 61021d7 commit 110c7ed
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 138 deletions.
342 changes: 211 additions & 131 deletions src/bgp/nlri/afisafi.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/bgp/nlri/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::fmt;
/// Path Identifier for BGP Multiple Paths (RFC7911).
///
/// Used in all AddpathNlri variants.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PathId(pub u32);

Expand Down
20 changes: 20 additions & 0 deletions src/bgp/nlri/evpn.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp;
use std::fmt;

use octseq::{Octets, OctetsBuilder, Parser};
Expand Down Expand Up @@ -79,6 +80,25 @@ where Octs: AsRef<[u8]>,
}
}


impl<Octs> PartialOrd for EvpnNlri<Octs>
where Octs: AsRef<[u8]>,
{
fn partial_cmp(&self, other: &EvpnNlri<Octs>) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<Octs: AsRef<[u8]>> Eq for EvpnNlri<Octs> { }

impl<Octs: AsRef<[u8]>> Ord for EvpnNlri<Octs> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.route_type.cmp(&other.route_type)
.then(self.raw.as_ref().cmp(other.raw.as_ref()))
}
}


impl<T> fmt::Display for EvpnNlri<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "EVPN-{}", self.route_type)
Expand Down
18 changes: 17 additions & 1 deletion src/bgp/nlri/flowspec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{cmp, fmt};

use log::debug;
use octseq::{Octets, OctetsBuilder, Parser};
Expand Down Expand Up @@ -98,6 +98,8 @@ impl<Octs: AsRef<[u8]>> FlowSpecNlri<Octs> {
}
}

impl<Octs: AsRef<[u8]>> Eq for FlowSpecNlri<Octs> { }

impl<Octs, Other> PartialEq<FlowSpecNlri<Other>> for FlowSpecNlri<Octs>
where Octs: AsRef<[u8]>,
Other: AsRef<[u8]>
Expand All @@ -109,6 +111,20 @@ where Octs: AsRef<[u8]>,
}


impl<Octs> PartialOrd for FlowSpecNlri<Octs>
where Octs: AsRef<[u8]>,
{
fn partial_cmp(&self, other: &FlowSpecNlri<Octs>) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<Octs: AsRef<[u8]>> Ord for FlowSpecNlri<Octs> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.raw.as_ref().cmp(other.raw.as_ref())
}
}

impl<T> fmt::Display for FlowSpecNlri<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "FLOWSPEC-NLRI")
Expand Down
47 changes: 47 additions & 0 deletions src/bgp/nlri/mpls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use inetnum::addr::Prefix;
use octseq::OctetsFrom;
use std::cmp;
use std::fmt;

use octseq::{Octets, OctetsBuilder, Parser};
Expand Down Expand Up @@ -76,6 +78,8 @@ impl<Octs: AsRef<[u8]>> MplsNlri<Octs> {
}
}

impl<Octs: AsRef<[u8]>> Eq for MplsNlri<Octs> { }

impl<Octs, Other> PartialEq<MplsNlri<Other>> for MplsNlri<Octs>
where Octs: AsRef<[u8]>,
Other: AsRef<[u8]>
Expand All @@ -85,6 +89,20 @@ where Octs: AsRef<[u8]>,
}
}

impl<Octs> PartialOrd for MplsNlri<Octs>
where Octs: AsRef<[u8]>,
{
fn partial_cmp(&self, other: &MplsNlri<Octs>) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<Octs: AsRef<[u8]>> Ord for MplsNlri<Octs> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.prefix.cmp(&other.prefix).then(self.labels.as_ref().cmp(other.labels.as_ref()))
}
}


impl<T> fmt::Display for MplsNlri<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -93,6 +111,21 @@ impl<T> fmt::Display for MplsNlri<T> {
}
}

impl<Octs, SrcOcts: Octets> OctetsFrom<MplsNlri<SrcOcts>> for MplsNlri<Octs>
where Octs: OctetsFrom<SrcOcts>,
{
type Error = Octs::Error;

fn try_octets_from(
source: MplsNlri<SrcOcts>
) -> Result<Self, Self::Error> {
Ok(MplsNlri {
prefix: source.prefix,
labels: Labels::try_octets_from(source.labels)?
})
}
}

//------------ Labels --------------------------------------------------------

/// MPLS labels, part of [`MplsNlri`] and [`MplsVpnNlri`].
Expand Down Expand Up @@ -191,3 +224,17 @@ impl<Octs: AsRef<[u8]>> AsRef<[u8]> for Labels<Octs> {
}
}

impl<Octs, SrcOcts: Octets> OctetsFrom<Labels<SrcOcts>> for Labels<Octs>
where Octs: OctetsFrom<SrcOcts>,
{
type Error = Octs::Error;

fn try_octets_from(
source: Labels<SrcOcts>
) -> Result<Self, Self::Error> {
Ok(Labels {
octets: Octs::try_octets_from(source.octets)?
})
}
}

19 changes: 19 additions & 0 deletions src/bgp/nlri/mpls_vpn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use inetnum::addr::Prefix;
use std::cmp;
use std::fmt;

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -88,6 +89,8 @@ impl<Octs: AsRef<[u8]>> MplsVpnNlri<Octs> {
}
}

impl<Octs: AsRef<[u8]>> Eq for MplsVpnNlri<Octs> { }

impl<Octs, Other> PartialEq<MplsVpnNlri<Other>> for MplsVpnNlri<Octs>
where Octs: AsRef<[u8]>,
Other: AsRef<[u8]>
Expand All @@ -99,6 +102,22 @@ where Octs: AsRef<[u8]>,
}
}

impl<Octs> PartialOrd for MplsVpnNlri<Octs>
where Octs: AsRef<[u8]>,
{
fn partial_cmp(&self, other: &MplsVpnNlri<Octs>) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<Octs: AsRef<[u8]>> Ord for MplsVpnNlri<Octs> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.prefix.cmp(&other.prefix)
.then(self.labels.as_ref().cmp(other.labels.as_ref()))
.then(self.rd.cmp(&other.rd))
}
}

impl<T> fmt::Display for MplsVpnNlri<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MPLS-VPN:{}", self.prefix())
Expand Down
18 changes: 17 additions & 1 deletion src/bgp/nlri/routetarget.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{cmp, fmt};

use octseq::{Octets, OctetsBuilder, Parser};
use crate::util::parser::ParseError;
Expand Down Expand Up @@ -42,6 +42,7 @@ impl<Octs: AsRef<[u8]>> RouteTargetNlri<Octs> {
}
}

impl<Octs: AsRef<[u8]>> Eq for RouteTargetNlri<Octs> { }

impl<Octs, Other> PartialEq<RouteTargetNlri<Other>> for RouteTargetNlri<Octs>
where Octs: AsRef<[u8]>,
Expand All @@ -52,8 +53,23 @@ where Octs: AsRef<[u8]>,
}
}

impl<Octs> PartialOrd for RouteTargetNlri<Octs>
where Octs: AsRef<[u8]>,
{
fn partial_cmp(&self, other: &RouteTargetNlri<Octs>) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<Octs: AsRef<[u8]>> Ord for RouteTargetNlri<Octs> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.raw.as_ref().cmp(other.raw.as_ref())
}
}

impl<T> fmt::Display for RouteTargetNlri<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ROUTE-TARGET-NLRI")
}
}

5 changes: 1 addition & 4 deletions src/bgp/nlri/vpls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::util::parser::ParseError;
use super::mpls_vpn::RouteDistinguisher;

/// VPLS Information as defined in RFC4761.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VplsNlri {
rd: RouteDistinguisher,
Expand Down Expand Up @@ -63,9 +63,6 @@ impl VplsNlri {
}
}




impl fmt::Display for VplsNlri {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "VPLS-{}", self.rd)
Expand Down

0 comments on commit 110c7ed

Please sign in to comment.