Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: use declarative macro to implement formats #67

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

luizirber
Copy link
Owner

Trying out a declarative macro to reduce copy-and-paste between basic and send modules.

Copy link

codecov bot commented Jun 10, 2024

Codecov Report

Attention: Patch coverage is 89.47368% with 4 lines in your changes missing coverage. Please review.

Project coverage is 76.41%. Comparing base (6db5584) to head (fa4936c).

Files Patch % Lines
src/basic/compression.rs 77.77% 2 Missing ⚠️
src/send/compression.rs 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #67      +/-   ##
==========================================
- Coverage   81.57%   76.41%   -5.16%     
==========================================
  Files          12       12              
  Lines         266      229      -37     
==========================================
- Hits          217      175      -42     
- Misses         49       54       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

crate::send::compression::Format::Gzip,
flate2::write::GzEncoder::new,
flate2::read::MultiGzDecoder::new,
std::io::Read | Send,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This | is a bit weird, I wanted + but that is not available syntax for a path

More info: https://internals.rust-lang.org/t/allow-to-follow-path-fragments-in-declarative-macros/13676

Comment on lines +17 to +18
gz,
"gz",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are a bit repetitive, need to figure out how to make an ident into a literal in the macro

level: Level,
) -> Result<Box<dyn io::Write + Send + 'a>, Error> {
Ok(Box::new(
zstd::stream::write::Encoder::new(out, level.into())?.auto_finish(),
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't do zstd because it has the extra .auto_finish(), need to look more into it

Comment on lines -121 to -122
cfg_if! {
if #[cfg(feature = "zstd")] {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out we don't need cfg_if!, #[cfg(feature = "zstd")] and #[cfg(not(feature = "zstd"))] are enough for our use case

Comment on lines -55 to +52
compression::Format::Zstd => compression::new_zstd_encoder(out_stream, level),
compression::Format::Gzip => compression::gz::encoder(out_stream, level),
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now each format is a submodule instead of a function, and all submodules have an encoder and a decoder function. These are all private, so it doesn't break the public API

@natir
Copy link
Collaborator

natir commented Jun 10, 2024

Very good idea and work, if you need/want help just ask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants