Skip to content

Commit

Permalink
chore: workspace support
Browse files Browse the repository at this point in the history
and sort dependencies, fmt, spelling
  • Loading branch information
nyurik committed Apr 5, 2024
1 parent d2ceb21 commit 937648c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 76 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[workspace]
members = [".", "utils"]
default-members = [".", "utils"]

[package]
name = "qbsdiff"
version = "1.4.1"
Expand All @@ -16,11 +20,11 @@ bzip2 = "0.4.4"
byteorder = "1.4"
rayon = "1.5"
suffix_array = "0.5"
clap = { optional = true, version = "3.2", features = [ "derive" ] }
clap = { optional = true, version = "3.2", features = ["derive"] }

[dev-dependencies]
qbsdiff_test_bench_utils = { version ="0.1", path = "utils" }
criterion = { version = "0.3", features = [ "html_reports" ] }
qbsdiff_test_bench_utils = { version = "0.1", path = "utils" }
criterion = { version = "0.3", features = ["html_reports"] }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion cmd/qbsdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() {

fn execute(args: BsdiffArgs) -> io::Result<()> {
// validate command line arguments
if !(matches!(args.compress_level, Some(0..=9) | None)) {
if !matches!(args.compress_level, Some(0..=9) | None) {
return Err(io::Error::new(
io::ErrorKind::Other,
"compression level must be in range 0-9",
Expand Down
2 changes: 0 additions & 2 deletions src/bsdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ impl<'s, 't> Bsdiff<'s, 't> {
/// The fastest/default compression level is usually good enough.
/// In contrast, patch files produced with the best level appeared slightly
/// bigger in many test cases.
///
/// [struct]: bzip2::Compression
pub fn compression_level(mut self, compression_level: u32) -> Self {
self.compression_level = Compression::new(u32::min(u32::max(compression_level, 0), 9));
self
Expand Down
2 changes: 1 addition & 1 deletion src/bspatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<'s, 'p, T: Write> Context<'s, 'p, T> {

/// Move the cursor on source.
fn seek(&mut self, offset: i64) -> Result<()> {
self.source.seek(SeekFrom::Current(offset)).map(std::mem::drop)
self.source.seek(SeekFrom::Current(offset)).map(drop)
}
}

Expand Down
112 changes: 43 additions & 69 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Testing {
}

impl Testing {
/// Create new tesing context.
/// Create new testing context.
pub fn new(assets_dir: path::PathBuf) -> Self {
Testing { assets_dir }
}
Expand Down Expand Up @@ -137,7 +137,11 @@ impl Testing {
run_command_in(
dir,
"bsdiff",
&[sample.source.as_os_str(), sample.target.as_os_str(), sample.patch.as_os_str()],
&[
sample.source.as_os_str(),
sample.target.as_os_str(),
sample.patch.as_os_str(),
],
)?;
}
fs::read(sample.patch.as_path())
Expand Down Expand Up @@ -216,14 +220,17 @@ impl Benchmarking {
run_command_in(
dir,
"bsdiff",
&[sample.source.as_os_str(), sample.target.as_os_str(), sample.patch.as_os_str()],
&[
sample.source.as_os_str(),
sample.target.as_os_str(),
sample.patch.as_os_str(),
],
)?;
} else {
let mut patch = Vec::new();
let source = fs::read(sample.source.as_path())?;
let target = fs::read(sample.target.as_path())?;
Bsdiff::new(&source[..], &target[..])
.compare(io::Cursor::new(&mut patch))?;
Bsdiff::new(&source[..], &target[..]).compare(io::Cursor::new(&mut patch))?;
patch.shrink_to_fit();
fs::write(sample.patch.as_path(), &patch[..])?;
return Ok(patch);
Expand All @@ -233,7 +240,8 @@ impl Benchmarking {
}

fn should_use_bsdiff(&self, sample: &Sample) -> bool {
sample.patch
sample
.patch
.parent()
.and_then(|p| p.file_name())
.map(|d| d == "samples")
Expand Down Expand Up @@ -266,9 +274,9 @@ fn run_bspatch_in<P: AsRef<Path>>(dir: P, s: &[u8], p: &[u8]) -> io::Result<Vec<
}

fn run_command_in<P, S>(dir: P, cmd: &str, args: &[S]) -> io::Result<()>
where
P: AsRef<Path>,
S: AsRef<OsStr>,
where
P: AsRef<Path>,
S: AsRef<OsStr>,
{
let bin = get_binary_in(dir, cmd)?;
let mut proc = process::Command::new(&bin)
Expand All @@ -278,20 +286,24 @@ fn run_command_in<P, S>(dir: P, cmd: &str, args: &[S]) -> io::Result<()>
.spawn()?;

let mut errors = String::new();
let mut stderr = proc.stderr.take()
let mut stderr = proc
.stderr
.take()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "failed to get command stderr"))?;
stderr.read_to_string(&mut errors)?;

let status = proc.wait()?;
if !status.success() {
let message = format!("command [{} {}], {}, stderr:\n{}",
bin.to_string_lossy(),
args.iter()
.map(|arg| arg.as_ref().to_string_lossy())
.collect::<Vec<_>>()
.join(" "),
status,
errors);
let message = format!(
"command [{} {}], {}, stderr:\n{}",
bin.to_string_lossy(),
args.iter()
.map(|arg| arg.as_ref().to_string_lossy())
.collect::<Vec<_>>()
.join(" "),
status,
errors
);
return Err(io::Error::new(io::ErrorKind::Other, message));
} else {
Ok(())
Expand Down Expand Up @@ -346,15 +358,10 @@ fn get_samples_in<P: AsRef<Path>>(dir: P) -> io::Result<Vec<Sample>> {
if let Some(p) = pat.to_str() {
walker = glob(p).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"cannot convert to str",
));
return Err(io::Error::new(io::ErrorKind::Other, "cannot convert to str"));
}
for result in walker.into_iter() {
let source = result
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
.into_path();
let source = result.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?.into_path();

let name;
let target;
Expand All @@ -371,10 +378,7 @@ fn get_samples_in<P: AsRef<Path>>(dir: P) -> io::Result<Vec<Sample>> {
pbuf.push(".p");
patch = path::PathBuf::from(d).join(pbuf.as_os_str());
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"cannot make target or patch path",
));
return Err(io::Error::new(io::ErrorKind::Other, "cannot make target or patch path"));
}

if let Err(_) = fs::metadata(target.as_path()) {
Expand Down Expand Up @@ -417,10 +421,8 @@ fn get_random_caches_in<P: AsRef<Path>>(dir: P, descs: &[RandomSample]) -> io::R
let target = dir.as_ref().join(format!("{}.{}.t", desc.name, tdesc.name(id)));
if !exists_file(target.as_path()) {
match tdesc {
RandomTarget::Bytes(bytes) =>
fs::write(target.as_path(), bytes)?,
RandomTarget::Distort(rate) =>
fs::write(target.as_path(), &distort(&sdata[..], *rate)[..])?,
RandomTarget::Bytes(bytes) => fs::write(target.as_path(), bytes)?,
RandomTarget::Distort(rate) => fs::write(target.as_path(), &distort(&sdata[..], *rate)[..])?,
}
}
let patch = dir.as_ref().join(format!("{}.{}.p", desc.name, tdesc.name(id)));
Expand Down Expand Up @@ -483,13 +485,13 @@ r incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis no\
strud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Dui\
s aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fu\
giat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in cu\
lpa qui officia deserunt mollit anim id est laborum."
lpa qui officia deserunt mollit anim id est laborum.",
),
targets: vec![
TBytes(b""),
TBytes(
b"consectetur adip##cing elit, jed do eiusmod wir mussen wissen. wir werden wis\
sen/ laboris nisi ut al&^%ip ex ea coikodo consequat. "
sen/ laboris nisi ut al&^%ip ex ea coikodo consequat. ",
),
TBytes(b"the quick brown fox jumps over the lazy dog"),
Distort(0.0),
Expand All @@ -500,42 +502,22 @@ sen/ laboris nisi ut al&^%ip ex ea coikodo consequat. "
RandomSample {
name: "rand-4k",
source: Random(4096),
targets: vec![
TBytes(b""),
Distort(0.0),
Distort(0.5),
Distort(1.0),
],
targets: vec![TBytes(b""), Distort(0.0), Distort(0.5), Distort(1.0)],
},
RandomSample {
name: "rand-256k",
source: Random(256 * 1024),
targets: vec![
TBytes(b""),
Distort(0.0),
Distort(0.5),
Distort(1.0),
],
targets: vec![TBytes(b""), Distort(0.0), Distort(0.5), Distort(1.0)],
},
RandomSample {
name: "rand-1m",
source: Random(1024 * 1024),
targets: vec![
TBytes(b""),
Distort(0.0),
Distort(0.5),
Distort(1.0),
],
targets: vec![TBytes(b""), Distort(0.0), Distort(0.5), Distort(1.0)],
},
RandomSample {
name: "rand-8m",
source: Random(8 * 1024 * 1024),
targets: vec![
TBytes(b""),
Distort(0.0),
Distort(0.5),
Distort(1.0),
],
targets: vec![TBytes(b""), Distort(0.0), Distort(0.5), Distort(1.0)],
},
]
}
Expand All @@ -549,20 +531,12 @@ pub fn default_random_bench_samples() -> Vec<RandomSample> {
RandomSample {
name: "rand-512k",
source: Random(512 * 1024),
targets: vec![
Distort(0.05),
Distort(0.50),
Distort(0.95),
],
targets: vec![Distort(0.05), Distort(0.50), Distort(0.95)],
},
RandomSample {
name: "rand-4m",
source: Random(4 * 1024 * 1024),
targets: vec![
Distort(0.05),
Distort(0.50),
Distort(0.95),
],
targets: vec![Distort(0.05), Distort(0.50), Distort(0.95)],
},
]
}
Expand Down

0 comments on commit 937648c

Please sign in to comment.