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
8It also contains the trait implementations that enable BgpkitParser to be used with
9Rust's iterator syntax.
10*/
11
12pub mod default;
13pub mod fallible;
14mod raw;
15
16// Re-export all iterator types for convenience
17pub use default::{ElemIterator, RecordIterator};
18pub use fallible::{FallibleElemIterator, FallibleRecordIterator};
19pub use raw::RawRecordIterator;
20
21use crate::models::BgpElem;
22use crate::parser::BgpkitParser;
23use std::io::Read;
24
25/// Use [ElemIterator] as the default iterator to return [BgpElem]s instead of [MrtRecord]s.
26impl<R: Read> IntoIterator for BgpkitParser<R> {
27 type Item = BgpElem;
28 type IntoIter = ElemIterator<R>;
29
30 fn into_iter(self) -> Self::IntoIter {
31 ElemIterator::new(self)
32 }
33}
34
35impl<R> BgpkitParser<R> {
36 pub fn into_record_iter(self) -> RecordIterator<R> {
37 RecordIterator::new(self)
38 }
39
40 pub fn into_elem_iter(self) -> ElemIterator<R> {
41 ElemIterator::new(self)
42 }
43
44 pub fn into_raw_record_iter(self) -> RawRecordIterator<R> {
45 RawRecordIterator::new(self)
46 }
47
48 /// Creates a fallible iterator over MRT records that returns parsing errors.
49 ///
50 /// # Example
51 /// ```no_run
52 /// use bgpkit_parser::BgpkitParser;
53 ///
54 /// let parser = BgpkitParser::new("updates.mrt").unwrap();
55 /// for result in parser.into_fallible_record_iter() {
56 /// match result {
57 /// Ok(record) => {
58 /// // Process the record
59 /// }
60 /// Err(e) => {
61 /// // Handle the error
62 /// eprintln!("Error parsing record: {}", e);
63 /// }
64 /// }
65 /// }
66 /// ```
67 pub fn into_fallible_record_iter(self) -> FallibleRecordIterator<R> {
68 FallibleRecordIterator::new(self)
69 }
70
71 /// Creates a fallible iterator over BGP elements that returns parsing errors.
72 ///
73 /// # Example
74 /// ```no_run
75 /// use bgpkit_parser::BgpkitParser;
76 ///
77 /// let parser = BgpkitParser::new("updates.mrt").unwrap();
78 /// for result in parser.into_fallible_elem_iter() {
79 /// match result {
80 /// Ok(elem) => {
81 /// // Process the element
82 /// }
83 /// Err(e) => {
84 /// // Handle the error
85 /// eprintln!("Error parsing element: {}", e);
86 /// }
87 /// }
88 /// }
89 /// ```
90 pub fn into_fallible_elem_iter(self) -> FallibleElemIterator<R> {
91 FallibleElemIterator::new(self)
92 }
93}