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

cargo fmt non-generated code in generated crate #30

Merged
merged 2 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
//! allows encoding and decoding records into a [`Record`](crate::records::Record).

use crate::protocol::buf::{ByteBuf, ByteBufMut};
use crate::protocol::{EncodeError, DecodeError};
use crate::protocol::{DecodeError, EncodeError};

mod gzip;
mod none;
mod snappy;
mod gzip;

pub use gzip::Gzip;
pub use none::None;
pub use snappy::Snappy;
pub use gzip::Gzip;

/// A trait for record compression algorithms.
pub trait Compressor<B: ByteBufMut> {
Expand Down
13 changes: 7 additions & 6 deletions src/compression/gzip.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::io::Write;

use bytes::{Bytes, BytesMut};
use bytes::buf::BufMut;
use bytes::{Bytes, BytesMut};
use flate2::write::{GzDecoder, GzEncoder};
use flate2::Compression;
use flate2::write::{GzEncoder, GzDecoder};
use log::error;

use crate::protocol::buf::{ByteBuf, ByteBufMut};
use crate::protocol::{EncodeError, DecodeError};
use crate::protocol::{DecodeError, EncodeError};

use super::{Compressor, Decompressor};

Expand All @@ -29,7 +29,7 @@ impl<B: ByteBufMut> Compressor<B> for Gzip {
type BufMut = BytesMut;
fn compress<R, F>(buf: &mut B, f: F) -> Result<R, EncodeError>
where
F: FnOnce(&mut Self::BufMut) -> Result<R, EncodeError>
F: FnOnce(&mut Self::BufMut) -> Result<R, EncodeError>,
{
// Write uncompressed bytes into a temporary buffer
let mut tmp = BytesMut::new();
Expand All @@ -48,13 +48,14 @@ impl<B: ByteBuf> Decompressor<B> for Gzip {
type Buf = Bytes;
fn decompress<R, F>(buf: &mut B, f: F) -> Result<R, DecodeError>
where
F: FnOnce(&mut Self::Buf) -> Result<R, DecodeError>
F: FnOnce(&mut Self::Buf) -> Result<R, DecodeError>,
{
let mut tmp = BytesMut::new();

// Decompress directly from the input buffer
let mut d = GzDecoder::new((&mut tmp).writer());
d.write_all(&buf.copy_to_bytes(buf.remaining())).map_err(decompression_err)?;
d.write_all(&buf.copy_to_bytes(buf.remaining()))
.map_err(decompression_err)?;
d.finish().map_err(decompression_err)?;

f(&mut tmp.into())
Expand Down
6 changes: 3 additions & 3 deletions src/compression/none.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::protocol::buf::{ByteBuf, ByteBufMut};
use crate::protocol::{EncodeError, DecodeError};
use crate::protocol::{DecodeError, EncodeError};

use super::{Compressor, Decompressor};

Expand All @@ -10,7 +10,7 @@ impl<B: ByteBufMut> Compressor<B> for None {
type BufMut = B;
fn compress<R, F>(buf: &mut B, f: F) -> Result<R, EncodeError>
where
F: FnOnce(&mut Self::BufMut) -> Result<R, EncodeError>
F: FnOnce(&mut Self::BufMut) -> Result<R, EncodeError>,
{
f(buf)
}
Expand All @@ -20,7 +20,7 @@ impl<B: ByteBuf> Decompressor<B> for None {
type Buf = B;
fn decompress<R, F>(buf: &mut B, f: F) -> Result<R, DecodeError>
where
F: FnOnce(&mut Self::Buf) -> Result<R, DecodeError>
F: FnOnce(&mut Self::Buf) -> Result<R, DecodeError>,
{
f(buf)
}
Expand Down
18 changes: 10 additions & 8 deletions src/compression/snappy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bytes::{Bytes, BytesMut};
use snap::raw::*;
use log::error;
use snap::raw::*;

use crate::protocol::buf::{ByteBuf, ByteBufMut};
use crate::protocol::{EncodeError, DecodeError};
use crate::protocol::{DecodeError, EncodeError};

use super::{Compressor, Decompressor};

Expand All @@ -15,7 +15,7 @@ impl<B: ByteBufMut> Compressor<B> for Snappy {
type BufMut = BytesMut;
fn compress<R, F>(buf: &mut B, f: F) -> Result<R, EncodeError>
where
F: FnOnce(&mut Self::BufMut) -> Result<R, EncodeError>
F: FnOnce(&mut Self::BufMut) -> Result<R, EncodeError>,
{
// Write uncompressed bytes into a temporary buffer
let mut tmp = BytesMut::new();
Expand All @@ -24,10 +24,12 @@ impl<B: ByteBufMut> Compressor<B> for Snappy {
// Compress directly into the target buffer
let start_pos = buf.offset();
let compress_gap = buf.put_gap(max_compress_len(tmp.len()));
let actual_len = Encoder::new().compress(&tmp, buf.gap_buf(compress_gap)).map_err(|e| {
error!("Failed to compress buffer: {}", e);
EncodeError
})?;
let actual_len = Encoder::new()
.compress(&tmp, buf.gap_buf(compress_gap))
.map_err(|e| {
error!("Failed to compress buffer: {}", e);
EncodeError
})?;
buf.seek(start_pos + actual_len);

Ok(res)
Expand All @@ -38,7 +40,7 @@ impl<B: ByteBuf> Decompressor<B> for Snappy {
type Buf = Bytes;
fn decompress<R, F>(buf: &mut B, f: F) -> Result<R, DecodeError>
where
F: FnOnce(&mut Self::Buf) -> Result<R, DecodeError>
F: FnOnce(&mut Self::Buf) -> Result<R, DecodeError>,
{
// Allocate a temporary buffer to hold the uncompressed bytes
let buf = buf.copy_to_bytes(buf.remaining());
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
//! # api_versions_req.client_software_version = StrBytes::from_str("1.0");
//! # api_versions_req.client_software_name = StrBytes::from_str("example-client");
//! # api_versions_req.encode(&mut buf, 3);
//!
//!
//! let api_key = buf.peek_bytes(0..2).get_i16();
//! let api_version = buf.peek_bytes(2..4).get_i16();
//! let header_version = ApiKey::try_from(api_key).unwrap().request_header_version(api_version);
Expand All @@ -107,11 +107,11 @@
#[macro_use]
extern crate log;

pub mod compression;
pub mod error;
#[allow(clippy::all)]
pub mod messages;
pub mod records;
pub mod compression;
pub mod protocol;
pub mod error;
pub mod records;

pub use error::ResponseError;
5 changes: 4 additions & 1 deletion src/protocol/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ pub trait ByteBufMut: BufMut {

/// Put a gap of `len` at the current buffer offset.
fn put_gap(&mut self, len: usize) -> Gap {
let res = Gap { offset: self.offset(), len };
let res = Gap {
offset: self.offset(),
len,
};
self.seek(res.offset + len);
res
}
Expand Down
20 changes: 12 additions & 8 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//! Most types are used internally in encoding/decoding, and are not required by typical use cases
//! for interacting with the protocol. However, types can be used for decoding partial messages,
//! or rewriting parts of an encoded message.
use std::string::FromUtf8Error;
use std::{error::Error, str::Utf8Error};
use std::borrow::Borrow;
use std::ops::RangeBounds;
use std::collections::BTreeMap;
use std::cmp;
use std::collections::BTreeMap;
use std::ops::RangeBounds;
use std::string::FromUtf8Error;
use std::{error::Error, str::Utf8Error};

use buf::{ByteBuf, ByteBufMut};

use self::buf::NotEnoughBytesError;

pub mod types;
pub mod buf;
pub mod types;

/// A string type backed by [`bytes::Bytes`].
pub type StrBytes = string::String<bytes::Bytes>;
Expand Down Expand Up @@ -54,7 +54,6 @@ impl From<FromUtf8Error> for DecodeError {
#[derive(Debug)]
pub struct EncodeError;


impl std::fmt::Display for EncodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "EncodeError")
Expand Down Expand Up @@ -131,7 +130,12 @@ pub trait Decodable: Sized {

pub(crate) trait MapEncodable: Sized {
type Key;
fn encode<B: ByteBufMut>(&self, key: &Self::Key, buf: &mut B, version: i16) -> Result<(), EncodeError>;
fn encode<B: ByteBufMut>(
&self,
key: &Self::Key,
buf: &mut B,
version: i16,
) -> Result<(), EncodeError>;
fn compute_size(&self, key: &Self::Key, version: i16) -> Result<usize, EncodeError>;
}

Expand Down Expand Up @@ -199,4 +203,4 @@ pub trait Builder {

/// Retrieve the builder for this protocol item.
fn builder() -> Self::Builder;
}
}
Loading
Loading