Skip to content

Commit

Permalink
decouple oneio from parser
Browse files Browse the repository at this point in the history
the new `BgpkitParser::new_with_reader` allows parser to be used without oneio
  • Loading branch information
digizeph committed Oct 16, 2024
1 parent ce66eab commit 4b77766
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ default = ["parser", "rustls"]
parser = [
"bytes",
"chrono",
"env_logger",
"regex",
]
cli = [
Expand Down
5 changes: 5 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ error module defines the error types used in bgpkit-parser.
*/
use crate::models::{Afi, Bgp4MpType, BgpState, EntryType, Safi, TableDumpV2Type};
use num_enum::TryFromPrimitiveError;
#[cfg(feature = "oneio")]
use oneio::OneIoError;
use std::fmt::{Display, Formatter};
use std::io::ErrorKind;
Expand All @@ -12,6 +13,7 @@ use std::{error::Error, fmt, io};
pub enum ParserError {
IoError(io::Error),
EofError(io::Error),
#[cfg(feature = "oneio")]
OneIoError(OneIoError),
EofExpected,
ParseError(String),
Expand Down Expand Up @@ -47,12 +49,14 @@ impl Display for ParserError {
ParserError::TruncatedMsg(s) => write!(f, "Error: {}", s),
ParserError::Unsupported(s) => write!(f, "Error: {}", s),
ParserError::EofExpected => write!(f, "Error: reach end of file"),
#[cfg(feature = "oneio")]
ParserError::OneIoError(e) => write!(f, "Error: {}", e),
ParserError::FilterError(e) => write!(f, "Error: {}", e),
}
}
}

#[cfg(feature = "oneio")]
impl From<OneIoError> for ParserErrorWithBytes {
fn from(error: OneIoError) -> Self {
ParserErrorWithBytes {
Expand All @@ -62,6 +66,7 @@ impl From<OneIoError> for ParserErrorWithBytes {
}
}

#[cfg(feature = "oneio")]
impl From<OneIoError> for ParserError {
fn from(error: OneIoError) -> Self {
ParserError::OneIoError(error)
Expand Down
4 changes: 3 additions & 1 deletion src/parser/iters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ impl<R: Read> Iterator for RecordIterator<R> {
}
None
}
ParserError::OneIoError(_) | ParserError::FilterError(_) => {
#[cfg(feature = "oneio")]
ParserError::OneIoError(_) => None,
ParserError::FilterError(_) => {
// this should not happen at this stage
None
}
Expand Down
14 changes: 14 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) use self::utils::*;

use crate::models::MrtRecord;
pub use mrt::mrt_elem::Elementor;
#[cfg(feature = "oneio")]
use oneio::{get_cache_reader, get_reader};

pub use crate::error::{ParserError, ParserErrorWithBytes};
Expand Down Expand Up @@ -48,7 +49,18 @@ impl Default for ParserOptions {
}

impl BgpkitParser<Box<dyn Read + Send>> {
/// Creating a new parser from an object that implements [Read] trait.
pub fn new_with_reader(reader: Box<dyn Read + Send>) -> Self {
BgpkitParser {
reader,
core_dump: false,
filters: vec![],
options: ParserOptions::default(),
}
}

/// Creating a new parser from a object that implements [Read] trait.
#[cfg(feature = "oneio")]
pub fn new(path: &str) -> Result<Self, ParserErrorWithBytes> {
let reader = get_reader(path)?;
Ok(BgpkitParser {
Expand All @@ -64,6 +76,7 @@ impl BgpkitParser<Box<dyn Read + Send>> {
/// The cache file name is generated by the following format: `cache-<crc32 of file name>-<file name>`.
/// For example, the remote file `http://archive.routeviews.org/route-views.chile/bgpdata/2023.03/RIBS/rib.20230326.0600.bz2`
/// will be cached as `cache-682cb1eb-rib.20230326.0600.bz2` in the cache directory.
#[cfg(feature = "oneio")]
pub fn new_cached(path: &str, cache_dir: &str) -> Result<Self, ParserErrorWithBytes> {
let file_name = path.rsplit('/').next().unwrap().to_string();
let new_file_name = format!(
Expand All @@ -80,6 +93,7 @@ impl BgpkitParser<Box<dyn Read + Send>> {
}
}

#[cfg(feature = "oneio")]
fn add_suffix_to_filename(filename: &str, suffix: &str) -> String {
let mut parts: Vec<&str> = filename.split('.').collect(); // Split filename by dots
if parts.len() > 1 {
Expand Down

0 comments on commit 4b77766

Please sign in to comment.