Skip to content

Commit

Permalink
feat: Read .bz2
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Aug 12, 2023
1 parent b89debf commit 02b5b27
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"

[dependencies]
anyhow = "1.0"
bzip2 = "0.4"
clap = { version = "4.3", features = ["derive", "env"] }
env_logger = "0.10"
flate2 = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
};

use anyhow::{anyhow, Error, Result};
use bzip2::bufread::BzDecoder;
use flate2::bufread::GzDecoder;
use log::{debug, trace};

Expand Down Expand Up @@ -63,6 +64,7 @@ where
type1::parse(text).or_else(|| type2::parse(text))
}

/// Decompress a manpage if necessary
pub fn read_manpage<P>(manpage_path: P) -> Result<String>
where
P: AsRef<Path>,
Expand All @@ -71,18 +73,18 @@ where
trace!("Reading man page at {}", path.display());
match path.extension() {
Some(ext) => {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
let mut str = String::new();
// TODO GzDecoder and BzDecoder seem to only work with UTF-8?
if ext == "gz" {
let file = File::open(path)?;
let reader = BufReader::new(file);
let mut decoder = GzDecoder::new(reader);
let mut str = String::new();
// TODO this only works with UTF-8
decoder.read_to_string(&mut str)?;
Ok(str)
GzDecoder::new(reader).read_to_string(&mut str)?;
} else if ext == "bz2" {
BzDecoder::new(reader).read_to_string(&mut str)?;
} else {
let contents = std::fs::read_to_string(path)?;
Ok(contents)
reader.read_to_string(&mut str)?;
}
Ok(str)
}
None => todo!(),
}
Expand Down

0 comments on commit 02b5b27

Please sign in to comment.