Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ let buffer = /* your netflow data */;
let mut parser = NetflowParser::default();

// Process packets without collecting into a Vec
for packet in parser.parse_bytes_iter(&buffer) {
for packet in parser.iter_packets(&buffer) {
match packet {
NetflowPacket::V5(v5) => {
// Process V5 packet
Expand Down Expand Up @@ -158,17 +158,17 @@ for packet in parser.parse_bytes_iter(&buffer) {

```rust
// Count V5 packets without collecting
let count = parser.parse_bytes_iter(&buffer)
let count = parser.iter_packets(&buffer)
.filter(|p| p.is_v5())
.count();

// Process only the first 10 packets
for packet in parser.parse_bytes_iter(&buffer).take(10) {
for packet in parser.iter_packets(&buffer).take(10) {
// Handle packet
}

// Collect only if needed (equivalent to parse_bytes())
let packets: Vec<_> = parser.parse_bytes_iter(&buffer).collect();
let packets: Vec<_> = parser.iter_packets(&buffer).collect();
```

## Parsing Out Unneeded Versions
Expand Down Expand Up @@ -404,7 +404,7 @@ This library includes several performance optimizations:

**Best practices for optimal performance:**
- Reuse parser instances instead of creating new ones for each packet
- Use `parse_bytes_iter()` instead of `parse_bytes()` when you don't need all packets in a Vec
- Use `iter_packets()` instead of `parse_bytes()` when you don't need all packets in a Vec
- Use `parse_bytes_as_netflow_common_flowsets()` when you only need flow data
- For V9/IPFIX, batch process packets from the same source to maximize template cache hits

Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! # netflow_parser
//!
A Netflow Parser library for Cisco V5, V7, V9, and IPFIX written in Rust. Supports chaining of multiple versions in the same stream.
//! A Netflow Parser library for Cisco V5, V7, V9, and IPFIX written in Rust. Supports chaining of multiple versions in the same stream.
//!
//! ## Example
//!
Expand Down Expand Up @@ -94,7 +94,7 @@ A Netflow Parser library for Cisco V5, V7, V9, and IPFIX written in Rust. Suppor
//! let mut parser = NetflowParser::default();
//!
//! // Process packets without collecting into a Vec
//! for packet in parser.parse_bytes_iter(&buffer) {
//! for packet in parser.iter_packets(&buffer) {
//! match packet {
//! NetflowPacket::V5(v5) => {
//! // Process V5 packet
Expand Down Expand Up @@ -132,17 +132,17 @@ A Netflow Parser library for Cisco V5, V7, V9, and IPFIX written in Rust. Suppor
//! # let buffer = [0u8; 72];
//! # let mut parser = NetflowParser::default();
//! // Count V5 packets without collecting
//! let count = parser.parse_bytes_iter(&buffer)
//! let count = parser.iter_packets(&buffer)
//! .filter(|p| p.is_v5())
//! .count();
//!
//! // Process only the first 10 packets
//! for packet in parser.parse_bytes_iter(&buffer).take(10) {
//! for packet in parser.iter_packets(&buffer).take(10) {
//! // Handle packet
//! }
//!
//! // Collect only if needed (equivalent to parse_bytes())
//! let packets: Vec<_> = parser.parse_bytes_iter(&buffer).collect();
//! let packets: Vec<_> = parser.iter_packets(&buffer).collect();
//! ```
//!
//! ## Parsing Out Unneeded Versions
Expand Down Expand Up @@ -366,7 +366,7 @@ A Netflow Parser library for Cisco V5, V7, V9, and IPFIX written in Rust. Suppor
//!
//! **Best practices for optimal performance:**
//! - Reuse parser instances instead of creating new ones for each packet
//! - Use `parse_bytes_iter()` instead of `parse_bytes()` when you don't need all packets in a Vec
//! - Use `iter_packets()` instead of `parse_bytes()` when you don't need all packets in a Vec
//! - Use `parse_bytes_as_netflow_common_flowsets()` when you only need flow data
//! - For V9/IPFIX, batch process packets from the same source to maximize template cache hits
//!
Expand Down Expand Up @@ -641,7 +641,7 @@ impl NetflowParser {
/// let v5_packet = [0, 5, 0, 1, 3, 0, 4, 0, 5, 0, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7,];
/// let mut parser = NetflowParser::default();
///
/// for packet in parser.parse_bytes_iter(&v5_packet) {
/// for packet in parser.iter_packets(&v5_packet) {
/// match packet {
/// NetflowPacket::V5(v5) => println!("V5 packet: {:?}", v5.header.version),
/// NetflowPacket::Error(e) => println!("Error: {:?}", e),
Expand All @@ -650,7 +650,7 @@ impl NetflowParser {
/// }
/// ```
#[inline]
pub fn parse_bytes_iter<'a>(&'a mut self, packet: &'a [u8]) -> NetflowPacketIterator<'a> {
pub fn iter_packets<'a>(&'a mut self, packet: &'a [u8]) -> NetflowPacketIterator<'a> {
NetflowPacketIterator {
parser: self,
remaining: packet,
Expand Down
10 changes: 5 additions & 5 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ mod base_tests {
let mut parser2 = NetflowParser::default();

let vec_result = parser1.parse_bytes(&v5_packet);
let iter_result: Vec<_> = parser2.parse_bytes_iter(&v5_packet).collect();
let iter_result: Vec<_> = parser2.iter_packets(&v5_packet).collect();

assert_eq!(vec_result.len(), iter_result.len());
assert_eq!(vec_result.len(), 1);
Expand All @@ -582,7 +582,7 @@ mod base_tests {
let mut parser4 = NetflowParser::default();

let vec_result_chained = parser3.parse_bytes(&chained);
let iter_result_chained: Vec<_> = parser4.parse_bytes_iter(&chained).collect();
let iter_result_chained: Vec<_> = parser4.iter_packets(&chained).collect();

assert_eq!(vec_result_chained.len(), iter_result_chained.len());
assert_eq!(vec_result_chained.len(), 2);
Expand All @@ -592,7 +592,7 @@ mod base_tests {
fn it_parses_bytes_iter_handles_empty_buffer() {
let mut parser = NetflowParser::default();
let empty: &[u8] = &[];
let result: Vec<_> = parser.parse_bytes_iter(empty).collect();
let result: Vec<_> = parser.iter_packets(empty).collect();
assert_eq!(result.len(), 0);
}

Expand All @@ -601,7 +601,7 @@ mod base_tests {
let mut parser = NetflowParser::default();
let invalid_packet = [0]; // Incomplete packet - cannot parse header

let result: Vec<_> = parser.parse_bytes_iter(&invalid_packet).collect();
let result: Vec<_> = parser.iter_packets(&invalid_packet).collect();
assert_eq!(result.len(), 1);
assert!(result[0].is_error());
}
Expand All @@ -618,7 +618,7 @@ mod base_tests {
let mut count = 0;

// Demonstrate zero-allocation usage pattern
for packet in parser.parse_bytes_iter(&v5_packet) {
for packet in parser.iter_packets(&v5_packet) {
match packet {
NetflowPacket::V5(_) => count += 1,
_ => {}
Expand Down