bgpkit_parser/parser/iters/
mod.rs

1/*!
2Iterator implementations for bgpkit-parser.
3
4This module contains different iterator implementations for parsing BGP data:
5- `default`: Standard iterators that skip errors (RecordIterator, ElemIterator)
6- `fallible`: Fallible iterators that return Results (FallibleRecordIterator, FallibleElemIterator)
7- `update`: Iterators for BGP UPDATE messages (UpdateIterator, FallibleUpdateIterator)
8
9It also contains the trait implementations that enable BgpkitParser to be used with
10Rust's iterator syntax.
11*/
12
13pub mod default;
14pub mod fallible;
15mod raw;
16mod update;
17
18// Re-export all iterator types for convenience
19pub use default::{ElemIterator, RecordIterator};
20pub use fallible::{FallibleElemIterator, FallibleRecordIterator};
21pub use raw::RawRecordIterator;
22pub use update::{
23    Bgp4MpUpdate, FallibleUpdateIterator, MrtUpdate, TableDumpV2Entry, UpdateIterator,
24};
25
26use crate::models::BgpElem;
27use crate::parser::BgpkitParser;
28use std::io::Read;
29
30/// Use [ElemIterator] as the default iterator to return [BgpElem]s instead of [MrtRecord]s.
31impl<R: Read> IntoIterator for BgpkitParser<R> {
32    type Item = BgpElem;
33    type IntoIter = ElemIterator<R>;
34
35    fn into_iter(self) -> Self::IntoIter {
36        ElemIterator::new(self)
37    }
38}
39
40impl<R> BgpkitParser<R> {
41    pub fn into_record_iter(self) -> RecordIterator<R> {
42        RecordIterator::new(self)
43    }
44
45    pub fn into_elem_iter(self) -> ElemIterator<R> {
46        ElemIterator::new(self)
47    }
48
49    pub fn into_raw_record_iter(self) -> RawRecordIterator<R> {
50        RawRecordIterator::new(self)
51    }
52
53    /// Creates an iterator over BGP announcements from MRT data.
54    ///
55    /// This iterator yields `MrtUpdate` items from both UPDATES files (BGP4MP messages)
56    /// and RIB dump files (TableDump/TableDumpV2 messages). It's a middle ground
57    /// between `into_record_iter()` and `into_elem_iter()`:
58    ///
59    /// - More focused than `into_record_iter()` as it only returns BGP announcements
60    /// - More efficient than `into_elem_iter()` as it doesn't duplicate attributes per prefix
61    ///
62    /// The iterator returns an `MrtUpdate` enum with variants:
63    /// - `Bgp4MpUpdate`: BGP UPDATE messages from UPDATES files
64    /// - `TableDumpV2Entry`: RIB entries from TableDumpV2 RIB dumps
65    /// - `TableDumpMessage`: Legacy TableDump v1 messages
66    ///
67    /// # Example
68    /// ```no_run
69    /// use bgpkit_parser::{BgpkitParser, MrtUpdate};
70    ///
71    /// let parser = BgpkitParser::new("updates.mrt").unwrap();
72    /// for update in parser.into_update_iter() {
73    ///     match update {
74    ///         MrtUpdate::Bgp4MpUpdate(u) => {
75    ///             println!("Peer {} announced {} prefixes",
76    ///                 u.peer_ip,
77    ///                 u.message.announced_prefixes.len()
78    ///             );
79    ///         }
80    ///         MrtUpdate::TableDumpV2Entry(e) => {
81    ///             println!("RIB entry for {} with {} peers",
82    ///                 e.prefix,
83    ///                 e.rib_entries.len()
84    ///             );
85    ///         }
86    ///         MrtUpdate::TableDumpMessage(m) => {
87    ///             println!("Legacy table dump for {}", m.prefix);
88    ///         }
89    ///     }
90    /// }
91    /// ```
92    pub fn into_update_iter(self) -> UpdateIterator<R> {
93        UpdateIterator::new(self)
94    }
95
96    /// Creates a fallible iterator over MRT records that returns parsing errors.
97    ///
98    /// # Example
99    /// ```no_run
100    /// use bgpkit_parser::BgpkitParser;
101    ///
102    /// let parser = BgpkitParser::new("updates.mrt").unwrap();
103    /// for result in parser.into_fallible_record_iter() {
104    ///     match result {
105    ///         Ok(record) => {
106    ///             // Process the record
107    ///         }
108    ///         Err(e) => {
109    ///             // Handle the error
110    ///             eprintln!("Error parsing record: {}", e);
111    ///         }
112    ///     }
113    /// }
114    /// ```
115    pub fn into_fallible_record_iter(self) -> FallibleRecordIterator<R> {
116        FallibleRecordIterator::new(self)
117    }
118
119    /// Creates a fallible iterator over BGP elements that returns parsing errors.
120    ///
121    /// # Example
122    /// ```no_run
123    /// use bgpkit_parser::BgpkitParser;
124    ///
125    /// let parser = BgpkitParser::new("updates.mrt").unwrap();
126    /// for result in parser.into_fallible_elem_iter() {
127    ///     match result {
128    ///         Ok(elem) => {
129    ///             // Process the element
130    ///         }
131    ///         Err(e) => {
132    ///             // Handle the error
133    ///             eprintln!("Error parsing element: {}", e);
134    ///         }
135    ///     }
136    /// }
137    /// ```
138    pub fn into_fallible_elem_iter(self) -> FallibleElemIterator<R> {
139        FallibleElemIterator::new(self)
140    }
141
142    /// Creates a fallible iterator over BGP announcements that returns parsing errors.
143    ///
144    /// Unlike the default `into_update_iter()`, this iterator returns
145    /// `Result<MrtUpdate, ParserErrorWithBytes>` allowing users to handle parsing
146    /// errors explicitly instead of having them logged and skipped.
147    ///
148    /// # Example
149    /// ```no_run
150    /// use bgpkit_parser::{BgpkitParser, MrtUpdate};
151    ///
152    /// let parser = BgpkitParser::new("updates.mrt").unwrap();
153    /// for result in parser.into_fallible_update_iter() {
154    ///     match result {
155    ///         Ok(MrtUpdate::Bgp4MpUpdate(update)) => {
156    ///             println!("Peer {} announced {} prefixes",
157    ///                 update.peer_ip,
158    ///                 update.message.announced_prefixes.len()
159    ///             );
160    ///         }
161    ///         Ok(_) => { /* handle other variants */ }
162    ///         Err(e) => {
163    ///             eprintln!("Error parsing: {}", e);
164    ///         }
165    ///     }
166    /// }
167    /// ```
168    pub fn into_fallible_update_iter(self) -> FallibleUpdateIterator<R> {
169        FallibleUpdateIterator::new(self)
170    }
171}