Skip to content

Commit

Permalink
Use my new io-adapters crate
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX committed Dec 6, 2023
1 parent d8e0bd6 commit c65caaf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 132 deletions.
62 changes: 30 additions & 32 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ license = "Apache-2.0"
[dependencies]
bytesize = "1.3.0"
cfg-if = "1.0.0"
clap = { version = "4.4.10", features = ["derive", "wrap_help"] }
clap = { version = "4.4.11", features = ["derive", "wrap_help"] }
clap-num = "1.0.2"
clap-verbosity-flag = "2.1.0"
error-stack = "0.4.1"
io-adapters = "0.1.0"
itoa = "1.0.9"
log = { version = "0.4.20", features = ["release_max_level_info"] }
paste = "1.0.14"
Expand All @@ -41,7 +42,7 @@ expect-test = "1.4.1"
more-asserts = "0.3.1"
rand = "0.8.5"
rstest = { version = "0.18.2", default-features = false }
supercilex-tests = "0.4.1"
supercilex-tests = "0.4.2"
tempfile = "3.8.1"
trycmd = "0.14.19"

Expand Down
8 changes: 4 additions & 4 deletions api.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub ftzz::generator::Error::Io
pub ftzz::generator::Error::RuntimeCreation
pub ftzz::generator::Error::TaskJoin
impl core::error::Error for ftzz::generator::Error
impl core::fmt::Display for ftzz::generator::Error
pub fn ftzz::generator::Error::fmt(&self, __formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::fmt::Debug for ftzz::generator::Error
pub fn ftzz::generator::Error::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::fmt::Display for ftzz::generator::Error
pub fn ftzz::generator::Error::fmt(&self, __formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::marker::Send for ftzz::generator::Error
impl core::marker::Sync for ftzz::generator::Error
impl core::marker::Unpin for ftzz::generator::Error
Expand Down Expand Up @@ -46,10 +46,10 @@ pub ftzz::generator::NumFilesWithRatioError::InvalidRatio
pub ftzz::generator::NumFilesWithRatioError::InvalidRatio::file_to_dir_ratio: core::num::nonzero::NonZeroU64
pub ftzz::generator::NumFilesWithRatioError::InvalidRatio::num_files: core::num::nonzero::NonZeroU64
impl core::error::Error for ftzz::generator::NumFilesWithRatioError
impl core::fmt::Display for ftzz::generator::NumFilesWithRatioError
pub fn ftzz::generator::NumFilesWithRatioError::fmt(&self, __formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::fmt::Debug for ftzz::generator::NumFilesWithRatioError
pub fn ftzz::generator::NumFilesWithRatioError::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::fmt::Display for ftzz::generator::NumFilesWithRatioError
pub fn ftzz::generator::NumFilesWithRatioError::fmt(&self, __formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
impl core::marker::Send for ftzz::generator::NumFilesWithRatioError
impl core::marker::Sync for ftzz::generator::NumFilesWithRatioError
impl core::marker::Unpin for ftzz::generator::NumFilesWithRatioError
Expand Down
67 changes: 2 additions & 65 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use clap_num::si_number;
use clap_verbosity_flag::Verbosity;
use error_stack::ResultExt;
use ftzz::generator::{Generator, NumFilesWithRatio, NumFilesWithRatioError};
use io_adapters::WriteExtension;
use paste::paste;

/// A random file and directory generator
Expand Down Expand Up @@ -248,7 +249,7 @@ fn ftzz(
match cmd {
Cmd::Generate(options) => Generator::try_from(options)
.change_context(CliError::InvalidArgs)?
.generate(&mut fmt_adapter::FmtWriteAdapter::from(&mut stdout))
.generate(&mut stdout.write_adapter())
.change_context(CliError::Generator),
}
}
Expand Down Expand Up @@ -287,70 +288,6 @@ macro_rules! lenient_si_number {
lenient_si_number!(u64);
lenient_si_number!(u32);

// TODO https://github.com/rust-lang/rust/pull/104389
mod fmt_adapter {
use std::{
fmt,
fmt::Debug,
io::{Error, Write},
};

/// Adapter that enables writing through a [`fmt::Write`] to an underlying
/// [`io::Write`].
///
/// # Examples
///
/// ```rust
/// #![feature(impl_fmt_write_for_io_write)]
/// # use std::{fmt, io};
/// # use std::io::FmtWriteAdapter;
///
/// let mut output1 = String::new();
/// let mut output2 = io::stdout();
/// let mut output2 = FmtWriteAdapter::from(&mut output2);
///
/// my_common_writer(&mut output1).unwrap();
/// my_common_writer(&mut output2).unwrap();
///
/// fn my_common_writer(output: &mut impl fmt::Write) -> fmt::Result {
/// writeln!(output, "Hello World!")
/// }
/// ```
pub struct FmtWriteAdapter<'a, W: Write + ?Sized> {
inner: &'a mut W,
error: Option<Error>,
}

impl<'a, W: Write + ?Sized> From<&'a mut W> for FmtWriteAdapter<'a, W> {
fn from(inner: &'a mut W) -> Self {
Self { inner, error: None }
}
}

impl<W: Write + ?Sized> fmt::Write for FmtWriteAdapter<'_, W> {
fn write_str(&mut self, s: &str) -> fmt::Result {
match self.inner.write_all(s.as_bytes()) {
Ok(()) => {
self.error = None;
Ok(())
}
Err(e) => {
self.error = Some(e);
Err(fmt::Error)
}
}
}
}

impl<W: Write + ?Sized> Debug for FmtWriteAdapter<'_, W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut builder = f.debug_struct("FmtWriteAdapter");
builder.field("error", &self.error);
builder.finish()
}
}
}

#[cfg(test)]
mod cli_tests {
use clap::CommandFactory;
Expand Down
32 changes: 3 additions & 29 deletions tests/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::{

use expect_test::expect_file;
use ftzz::generator::{Generator, NumFilesWithRatio};
use io_adapters::WriteExtension;
use more_asserts::assert_le;
use rand::Rng;
use rstest::rstest;
Expand Down Expand Up @@ -309,12 +310,7 @@ fn fuzz_test() {
.bytes_exact(bytes_exact)
.build();
println!("Params: {g:?}");
{
// TODO https://github.com/rust-lang/rust/pull/104389
let mut output = String::new();
g.generate(&mut output).unwrap();
io::Write::write_all(&mut stdout(), output.as_bytes()).unwrap();
}
g.generate(&mut stdout().write_adapter()).unwrap();

assert_le!(find_max_depth(&dir.path), max_depth);
if files_exact {
Expand All @@ -327,28 +323,6 @@ fn fuzz_test() {

/// Recursively hashes the file and directory names in dir
fn print_and_hash_dir(dir: &Path, output: &mut impl Write) {
// TODO https://github.com/rust-lang/libs-team/issues/309
struct HashWriteAdapter<'a, H> {
hasher: &'a mut H,
}

impl<'a, H> HashWriteAdapter<'a, H> {
pub fn new(hasher: &'a mut H) -> Self {
Self { hasher }
}
}

impl<'a, H: Hasher> io::Write for HashWriteAdapter<'a, H> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.hasher.write(buf);
Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

writeln!(output).unwrap();

let mut hasher = DefaultHasher::new();
Expand All @@ -367,7 +341,7 @@ fn print_and_hash_dir(dir: &Path, output: &mut impl Write) {
} else if entry.metadata().unwrap().len() > 0 {
io::copy(
&mut File::open(entry.path()).unwrap(),
&mut HashWriteAdapter::new(&mut hasher),
&mut hasher.write_adapter(),
)
.unwrap();
}
Expand Down

0 comments on commit c65caaf

Please sign in to comment.