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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
use std::fmt::{Display, Formatter};
use crate::bgp::attributes::{*};
use crate::network::NextHopAddress;
use itertools::{Itertools};


impl Display for Origin {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Origin::IGP => {"IGP"}
            Origin::EGP => {"EGP"}
            Origin::INCOMPLETE => {"INCOMPLETE"}
        };
        write!(f, "{}", s)
    }
}

impl Display for AtomicAggregate {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", match self {
            AtomicAggregate::NAG => {"NAG"}
            AtomicAggregate::AG => {"AG"}
        })
    }
}

const NOEXPORT: &str ="no-export";

impl Display for Community {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", match self {
            Community::NoExport => {
                "no-export".to_string()
            }
            Community::NoAdvertise => {
                "no-advertise".to_string()
            }
            Community::NoExportSubConfed => {
                "no-export-sub-confed".to_string()
            }
            Community::Custom(asn, value) => {
                format!("{}:{}", asn, value)
            }
        }
        )
    }
}

impl Display for NextHopAddress {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}",
               match self {
                   NextHopAddress::Ipv4(v) => {v.to_string()}
                   NextHopAddress::Ipv6(v) => {v.to_string()}
                   NextHopAddress::Ipv6LinkLocal(v1, _v2) => {v1.to_string()}
               }
        )
    }
}

impl Display for AsPath {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "{}",
        self
            .segments()
            .iter()
            .map(|seg| match seg {
                AsPathSegment::AsSequence(v) | AsPathSegment::ConfedSequence(v) => v
                    .iter()
                    .join(" "),
                AsPathSegment::AsSet(v) | AsPathSegment::ConfedSet(v) => {
                    format!(
                        "{{{}}}",
                        v.iter()
                            .join(",")
                    )
                }
            })
            .join(" ")
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_aspath_display() {
        let aspath = AsPath{
            segments: vec![AsPathSegment::AsSequence([1,2,3,5].to_vec())]
        };
        let mut paths = vec![];
        for _ in 0..1000000{
            paths.push(aspath.clone());
        }
        for p in paths{
            p.to_string();
        }
    }

}