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

Client/Broker cargo features #71

Merged
merged 1 commit into from
Sep 3, 2024
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
7 changes: 5 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- run: cargo clippy --workspace -- -D warnings
- run: cargo clippy --workspace --all-features -- -D warnings
- name: Install cargo-hack
uses: taiki-e/install-action@v2
with:
tool: [email protected]
- run: cargo hack --feature-powerset clippy --all-targets --locked -- -D warnings
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ keywords = ["kafka"]
# adds the ResponseKind + RequestKind enums with variants for every message type.
# disabled by default since it doubles clean release build times due to lots of generated code.
messages_enum = []
# Enable this feature if you are implementing a kafka client.
# It will enable encoding of requests and decoding of responses.
client = []
# Enable this feature if you are implementing a kafka protocol compatible broker.
# It will enable encoding of responses and decoding of requests.
broker = []
default = ["client", "broker"]

[dependencies]
bytes = "1.0.1"
Expand Down
29 changes: 28 additions & 1 deletion protocol_codegen/src/generate_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,32 @@ pub fn run() -> Result<(), Error> {
writeln!(module_file)?;
writeln!(
module_file,
"use crate::protocol::{{NewType, Request, StrBytes, HeaderVersion}};"
"use crate::protocol::{{NewType, StrBytes, HeaderVersion}};"
)?;
writeln!(
module_file,
"#[cfg(all(feature = \"client\", feature = \"broker\"))]"
)?;
writeln!(module_file, "use crate::protocol::Request;")?;
writeln!(module_file, "use std::convert::TryFrom;")?;
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?;
writeln!(
module_file,
"#[cfg(any(feature = \"client\", feature = \"broker\"))]"
)?;
writeln!(module_file, "use crate::protocol::Encodable;")?;
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?;
writeln!(
module_file,
"#[cfg(any(feature = \"client\", feature = \"broker\"))]"
)?;
writeln!(module_file, "use crate::protocol::Decodable;")?;
writeln!(module_file, "use anyhow::Result;")?;
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?;
writeln!(
module_file,
"#[cfg(any(feature = \"client\", feature = \"broker\"))]"
)?;
writeln!(module_file, "use anyhow::Context;")?;
writeln!(module_file)?;

Expand Down Expand Up @@ -169,6 +186,10 @@ pub fn run() -> Result<(), Error> {
.find(|(k, _)| k == api_key)
.map(|(_, v)| v)
.expect("Every request type has a response type");
writeln!(
module_file,
"#[cfg(all(feature = \"client\", feature = \"broker\"))]"
)?;
writeln!(module_file, "impl Request for {} {{", request_type)?;
writeln!(module_file, " const KEY: i16 = {};", api_key)?;
writeln!(module_file, " type Response = {};", response_type)?;
Expand Down Expand Up @@ -278,6 +299,7 @@ pub fn run() -> Result<(), Error> {
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?;
writeln!(module_file, "impl RequestKind {{")?;
writeln!(module_file, "/// Encode the message into the target buffer")?;
writeln!(module_file, "#[cfg(feature = \"client\")]")?;
writeln!(
module_file,
"pub fn encode(&self, bytes: &mut bytes::BytesMut, version: i16) -> anyhow::Result<()> {{"
Expand All @@ -297,6 +319,7 @@ pub fn run() -> Result<(), Error> {
module_file,
"/// Decode the message from the provided buffer and version"
)?;
writeln!(module_file, "#[cfg(feature = \"broker\")]")?;
writeln!(
module_file,
"pub fn decode(api_key: ApiKey, bytes: &mut bytes::Bytes, version: i16) -> anyhow::Result<RequestKind> {{"
Expand Down Expand Up @@ -332,6 +355,7 @@ pub fn run() -> Result<(), Error> {
module_file,
r#"
#[cfg(feature = "messages_enum")]
#[cfg(any(feature = "client", feature = "broker"))]
fn decode<T: Decodable>(bytes: &mut bytes::Bytes, version: i16) -> Result<T> {{
T::decode(bytes, version).with_context(|| {{
format!(
Expand All @@ -343,6 +367,7 @@ fn decode<T: Decodable>(bytes: &mut bytes::Bytes, version: i16) -> Result<T> {{
}}

#[cfg(feature = "messages_enum")]
#[cfg(any(feature = "client", feature = "broker"))]
fn encode<T: Encodable>(encodable: &T, bytes: &mut bytes::BytesMut, version: i16) -> Result<()> {{
encodable.encode(bytes, version).with_context(|| {{
format!(
Expand Down Expand Up @@ -378,6 +403,7 @@ fn encode<T: Encodable>(encodable: &T, bytes: &mut bytes::BytesMut, version: i16
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?;
writeln!(module_file, "impl ResponseKind {{")?;
writeln!(module_file, "/// Encode the message into the target buffer")?;
writeln!(module_file, "#[cfg(feature = \"broker\")]")?;
writeln!(
module_file,
"pub fn encode(&self, bytes: &mut bytes::BytesMut, version: i16) -> anyhow::Result<()> {{"
Expand All @@ -397,6 +423,7 @@ fn encode<T: Encodable>(encodable: &T, bytes: &mut bytes::BytesMut, version: i16
module_file,
"/// Decode the message from the provided buffer and version"
)?;
writeln!(module_file, "#[cfg(feature = \"client\")]")?;
writeln!(
module_file,
"pub fn decode(api_key: ApiKey, bytes: &mut bytes::Bytes, version: i16) -> anyhow::Result<ResponseKind> {{"
Expand Down
20 changes: 20 additions & 0 deletions protocol_codegen/src/generate_messages/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::cmp::Ordering;

#[derive(Debug, Clone, PartialEq, Eq)]
struct PreparedStruct {
spec_type: SpecType,
name: String,
map_key: Option<Box<PreparedType>>,
prepared_fields: Vec<PreparedField>,
Expand Down Expand Up @@ -249,6 +250,7 @@ fn prepare_field_type<W: Write>(
deprecated_versions: VersionSpec,
prepared_structs: &BTreeMap<String, PreparedStruct>,
prepared_structs_output: &mut Vec<PreparedStruct>,
spec_type: SpecType,
) -> Result<PreparedType, Error> {
Ok(match type_ {
TypeSpec::Primitive(prim) => {
Expand Down Expand Up @@ -278,6 +280,7 @@ fn prepare_field_type<W: Write>(
valid_versions,
flexible_msg_versions,
deprecated_versions,
spec_type,
}
}
} else {
Expand All @@ -291,6 +294,7 @@ fn prepare_field_type<W: Write>(
deprecated_versions,
prepared_structs,
prepared_structs_output,
spec_type,
)?
};

Expand All @@ -307,6 +311,7 @@ fn prepare_field_type<W: Write>(
deprecated_versions,
prepared_structs,
prepared_structs_output,
spec_type,
)?;
match prepared_field {
PreparedType::Struct(PreparedStruct {
Expand Down Expand Up @@ -977,6 +982,7 @@ fn prepared_struct_def<W: Write>(
deprecated_versions: VersionSpec,
prepared_structs: &BTreeMap<String, PreparedStruct>,
prepared_structs_output: &mut Vec<PreparedStruct>,
spec_type: SpecType,
) -> Result<PreparedStruct, Error> {
let mut prepared_fields = Vec::new();
let mut map_key = None;
Expand All @@ -994,6 +1000,7 @@ fn prepared_struct_def<W: Write>(
deprecated_versions,
prepared_structs,
prepared_structs_output,
spec_type,
)?;

if field.map_key && num_map_keys == 1 {
Expand Down Expand Up @@ -1068,6 +1075,7 @@ fn prepared_struct_def<W: Write>(
}

let prepared_struct = PreparedStruct {
spec_type,
name: name.into(),
map_key,
prepared_fields,
Expand Down Expand Up @@ -1187,6 +1195,11 @@ impl PreparedStruct {
writeln!(w)?;
writeln!(w)?;

match self.spec_type {
SpecType::Request => writeln!(w, "#[cfg(feature = \"client\")]")?,
SpecType::Response => writeln!(w, "#[cfg(feature = \"broker\")]")?,
_ => {}
}
if self.map_key.is_some() {
write!(w, "impl MapEncodable for {} ", self.name)?;
} else {
Expand Down Expand Up @@ -1227,6 +1240,11 @@ impl PreparedStruct {
writeln!(w)?;
writeln!(w)?;

match self.spec_type {
SpecType::Request => writeln!(w, "#[cfg(feature = \"broker\")]")?,
SpecType::Response => writeln!(w, "#[cfg(feature = \"client\")]")?,
_ => {}
}
if self.map_key.is_some() {
write!(w, "impl MapDecodable for {} ", self.name)?;
} else {
Expand Down Expand Up @@ -1459,6 +1477,7 @@ pub fn generate(output_path: &str, spec: Spec) -> Result<Option<GenerationOutput
deprecated_versions,
&prepared_structs,
&mut prepared_structs_output,
spec.type_,
)?;
}

Expand All @@ -1472,6 +1491,7 @@ pub fn generate(output_path: &str, spec: Spec) -> Result<Option<GenerationOutput
deprecated_versions,
&prepared_structs,
&mut prepared_structs_output,
spec.type_,
)?;

for prepared_struct in prepared_structs_output {
Expand Down
Loading