Skip to content

Commit

Permalink
fixup! feat: have file parsers take BufRead types
Browse files Browse the repository at this point in the history
  • Loading branch information
chrjabs committed Apr 15, 2024
1 parent 8e1b0db commit 0634ad4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
16 changes: 11 additions & 5 deletions rustsat/src/instances/fio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@ pub struct ObjNoExist(usize);
/// With feature `compression` supports bzip2 and gzip compression.
pub fn open_compressed_uncompressed_read<P: AsRef<Path>>(
path: P,
) -> Result<Box<dyn io::Read>, io::Error> {
) -> Result<Box<dyn io::BufRead>, io::Error> {
let path = path.as_ref();
let raw_reader = File::open(path)?;
#[cfg(feature = "compression")]
if let Some(ext) = path.extension() {
if ext.eq_ignore_ascii_case(std::ffi::OsStr::new("bz2")) {
return Ok(Box::new(bzip2::read::BzDecoder::new(raw_reader)));
return Ok(Box::new(io::BufReader::new(bzip2::read::BzDecoder::new(
raw_reader,
))));
}
if ext.eq_ignore_ascii_case(std::ffi::OsStr::new("gz")) {
return Ok(Box::new(flate2::read::GzDecoder::new(raw_reader)));
return Ok(Box::new(io::BufReader::new(flate2::read::GzDecoder::new(
raw_reader,
))));
}
if ext.eq_ignore_ascii_case(std::ffi::OsStr::new("xz")) {
return Ok(Box::new(xz2::read::XzDecoder::new(raw_reader)));
return Ok(Box::new(io::BufReader::new(xz2::read::XzDecoder::new(
raw_reader,
))));
}
}
Ok(Box::new(raw_reader))
Ok(Box::new(io::BufReader::new(raw_reader)))
}

/// Opens a writer for the file at Path.
Expand Down
4 changes: 2 additions & 2 deletions rustsat/src/instances/multiopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ impl<VM: ManageVars + Default> MultiOptInstance<VM> {
/// Parses a DIMACS instance from a file path. For more details see
/// [`OptInstance::from_dimacs`](super::OptInstance::from_dimacs).
pub fn from_dimacs_path<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
let reader = io::BufReader::new(fio::open_compressed_uncompressed_read(path)?);
let reader = fio::open_compressed_uncompressed_read(path)?;
Self::from_dimacs(reader)
}

Expand All @@ -482,7 +482,7 @@ impl<VM: ManageVars + Default> MultiOptInstance<VM> {
/// [`MultiOptInstance::from_opb`]. With feature `compression` supports
/// bzip2 and gzip compression, detected by the file extension.
pub fn from_opb_path<P: AsRef<Path>>(path: P, opts: fio::opb::Options) -> anyhow::Result<Self> {
let reader = io::BufReader::new(fio::open_compressed_uncompressed_read(path)?);
let reader = fio::open_compressed_uncompressed_read(path)?;
Self::from_opb(reader, opts)
}
}
Expand Down
8 changes: 4 additions & 4 deletions rustsat/src/instances/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ impl<VM: ManageVars + Default> OptInstance<VM> {
/// [`OptInstance::from_dimacs`]. With feature `compression` supports
/// bzip2 and gzip compression, detected by the file extension.
pub fn from_dimacs_path<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
let reader = io::BufReader::new(fio::open_compressed_uncompressed_read(path)?);
let reader = fio::open_compressed_uncompressed_read(path)?;
Self::from_dimacs(reader)
}

Expand All @@ -1377,7 +1377,7 @@ impl<VM: ManageVars + Default> OptInstance<VM> {
path: P,
obj_idx: usize,
) -> anyhow::Result<Self> {
let reader = io::BufReader::new(fio::open_compressed_uncompressed_read(path)?);
let reader = fio::open_compressed_uncompressed_read(path)?;
Self::from_dimacs_with_idx(reader, obj_idx)
}

Expand Down Expand Up @@ -1407,7 +1407,7 @@ impl<VM: ManageVars + Default> OptInstance<VM> {
/// [`OptInstance::from_opb`]. With feature `compression` supports
/// bzip2 and gzip compression, detected by the file extension.
pub fn from_opb_path<P: AsRef<Path>>(path: P, opts: fio::opb::Options) -> anyhow::Result<Self> {
let reader = io::BufReader::new(fio::open_compressed_uncompressed_read(path)?);
let reader = fio::open_compressed_uncompressed_read(path)?;
Self::from_opb(reader, opts)
}

Expand All @@ -1421,7 +1421,7 @@ impl<VM: ManageVars + Default> OptInstance<VM> {
obj_idx: usize,
opts: fio::opb::Options,
) -> anyhow::Result<Self> {
let reader = io::BufReader::new(fio::open_compressed_uncompressed_read(path)?);
let reader = fio::open_compressed_uncompressed_read(path)?;
Self::from_opb_with_idx(reader, obj_idx, opts)
}
}
Expand Down
10 changes: 4 additions & 6 deletions rustsat/src/instances/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,8 @@ impl<VM: ManageVars + Default> SatInstance<VM> {
/// [`SatInstance::from_dimacs`]. With feature `compression` supports
/// bzip2 and gzip compression, detected by the file extension.
pub fn from_dimacs_path<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
let reader = io::BufReader::new(
fio::open_compressed_uncompressed_read(path).context("failed to open reader")?,
);
let reader =
fio::open_compressed_uncompressed_read(path).context("failed to open reader")?;
SatInstance::from_dimacs(reader)
}

Expand All @@ -896,9 +895,8 @@ impl<VM: ManageVars + Default> SatInstance<VM> {
/// [`SatInstance::from_opb`]. With feature `compression` supports
/// bzip2 and gzip compression, detected by the file extension.
pub fn from_opb_path<P: AsRef<Path>>(path: P, opts: fio::opb::Options) -> anyhow::Result<Self> {
let reader = io::BufReader::new(
fio::open_compressed_uncompressed_read(path).context("failed to open reader")?,
);
let reader =
fio::open_compressed_uncompressed_read(path).context("failed to open reader")?;
SatInstance::from_opb(reader, opts)
}
}
Expand Down

0 comments on commit 0634ad4

Please sign in to comment.