-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add helper methods to RequestKind and ResponseKind #70
Merged
tychedelia
merged 3 commits into
tychedelia:main
from
rukai:add_methods_to_request_kind_and_response_kind
Aug 28, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,13 @@ pub fn run() -> Result<(), Error> { | |
"use crate::protocol::{{NewType, Request, StrBytes, HeaderVersion}};" | ||
)?; | ||
writeln!(module_file, "use std::convert::TryFrom;")?; | ||
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?; | ||
writeln!(module_file, "use crate::protocol::Encodable;")?; | ||
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?; | ||
writeln!(module_file, "use crate::protocol::Decodable;")?; | ||
writeln!(module_file, "use anyhow::Result;")?; | ||
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?; | ||
writeln!(module_file, "use anyhow::Context;")?; | ||
writeln!(module_file)?; | ||
|
||
for input_file_path in &input_file_paths { | ||
|
@@ -252,6 +259,7 @@ pub fn run() -> Result<(), Error> { | |
module_file, | ||
"/// Wrapping enum for all requests in the Kafka protocol." | ||
)?; | ||
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?; | ||
writeln!(module_file, "#[non_exhaustive]")?; | ||
writeln!(module_file, "#[derive(Debug, Clone, PartialEq)]")?; | ||
writeln!(module_file, "pub enum RequestKind {{")?; | ||
|
@@ -267,7 +275,47 @@ pub fn run() -> Result<(), Error> { | |
writeln!(module_file, "}}")?; | ||
writeln!(module_file)?; | ||
|
||
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, | ||
"pub fn encode(&self, bytes: &mut bytes::BytesMut, version: i16) -> anyhow::Result<()> {{" | ||
)?; | ||
writeln!(module_file, "match self {{")?; | ||
for (_, request_type) in request_types.iter() { | ||
let variant = request_type.trim_end_matches("Request"); | ||
writeln!( | ||
module_file, | ||
"RequestKind::{variant}(x) => encode(x, bytes, version)," | ||
)?; | ||
} | ||
writeln!(module_file, "}}")?; | ||
writeln!(module_file, "}}")?; | ||
|
||
writeln!( | ||
module_file, | ||
"/// Decode the message from the provided buffer and version" | ||
)?; | ||
writeln!( | ||
module_file, | ||
"pub fn decode(api_key: ApiKey, bytes: &mut bytes::Bytes, version: i16) -> anyhow::Result<RequestKind> {{" | ||
)?; | ||
writeln!(module_file, "match api_key {{")?; | ||
for (_, request_type) in request_types.iter() { | ||
let variant = request_type.trim_end_matches("Request"); | ||
writeln!( | ||
module_file, | ||
"ApiKey::{variant}Key => Ok(RequestKind::{variant}(decode(bytes, version)?))," | ||
)?; | ||
} | ||
writeln!(module_file, "}}")?; | ||
writeln!(module_file, "}}")?; | ||
|
||
writeln!(module_file, "}}")?; | ||
|
||
for (_, request_type) in request_types.iter() { | ||
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?; | ||
writeln!(module_file, "impl From<{request_type}> for RequestKind {{")?; | ||
writeln!( | ||
module_file, | ||
|
@@ -280,12 +328,40 @@ pub fn run() -> Result<(), Error> { | |
writeln!(module_file)?; | ||
} | ||
|
||
writeln!( | ||
module_file, | ||
r#" | ||
#[cfg(feature = "messages_enum")] | ||
fn decode<T: Decodable>(bytes: &mut bytes::Bytes, version: i16) -> Result<T> {{ | ||
T::decode(bytes, version).with_context(|| {{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: needs indentation |
||
format!( | ||
"Failed to decode {{}} v{{}} body", | ||
std::any::type_name::<T>(), | ||
version | ||
) | ||
}}) | ||
}} | ||
|
||
#[cfg(feature = "messages_enum")] | ||
fn encode<T: Encodable>(encodable: &T, bytes: &mut bytes::BytesMut, version: i16) -> Result<()> {{ | ||
encodable.encode(bytes, version).with_context(|| {{ | ||
format!( | ||
"Failed to encode {{}} v{{}} body", | ||
std::any::type_name::<T>(), | ||
version | ||
) | ||
}}) | ||
}} | ||
"# | ||
)?; | ||
|
||
writeln!( | ||
module_file, | ||
"/// Wrapping enum for all responses in the Kafka protocol." | ||
)?; | ||
writeln!(module_file, "#[non_exhaustive]")?; | ||
writeln!(module_file, "#[derive(Debug, Clone, PartialEq)]")?; | ||
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?; | ||
writeln!(module_file, "pub enum ResponseKind {{")?; | ||
for (_, response_type) in response_types.iter() { | ||
writeln!(module_file, " /// {},", response_type)?; | ||
|
@@ -299,7 +375,66 @@ pub fn run() -> Result<(), Error> { | |
writeln!(module_file, "}}")?; | ||
writeln!(module_file)?; | ||
|
||
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, | ||
"pub fn encode(&self, bytes: &mut bytes::BytesMut, version: i16) -> anyhow::Result<()> {{" | ||
)?; | ||
writeln!(module_file, "match self {{")?; | ||
for (_, response_type) in response_types.iter() { | ||
let variant = response_type.trim_end_matches("Response"); | ||
writeln!( | ||
module_file, | ||
"ResponseKind::{variant}(x) => encode(x, bytes, version)," | ||
)?; | ||
} | ||
writeln!(module_file, "}}")?; | ||
writeln!(module_file, "}}")?; | ||
|
||
writeln!( | ||
module_file, | ||
"/// Decode the message from the provided buffer and version" | ||
)?; | ||
writeln!( | ||
module_file, | ||
"pub fn decode(api_key: ApiKey, bytes: &mut bytes::Bytes, version: i16) -> anyhow::Result<ResponseKind> {{" | ||
)?; | ||
writeln!(module_file, "match api_key {{")?; | ||
for (_, response_type) in response_types.iter() { | ||
let variant = response_type.trim_end_matches("Response"); | ||
writeln!( | ||
module_file, | ||
"ApiKey::{variant}Key => Ok(ResponseKind::{variant}(decode(bytes, version)?))," | ||
)?; | ||
} | ||
writeln!(module_file, "}}")?; | ||
writeln!(module_file, "}}")?; | ||
|
||
writeln!( | ||
module_file, | ||
"/// Get the version of request header that needs to be prepended to this message" | ||
)?; | ||
writeln!( | ||
module_file, | ||
"pub fn header_version(&self, version: i16) -> i16 {{" | ||
)?; | ||
writeln!(module_file, "match self {{")?; | ||
for (_, response_type) in response_types.iter() { | ||
let variant = response_type.trim_end_matches("Response"); | ||
writeln!( | ||
module_file, | ||
"ResponseKind::{variant}(_) => {response_type}::header_version(version)," | ||
)?; | ||
} | ||
writeln!(module_file, "}}")?; | ||
writeln!(module_file, "}}")?; | ||
writeln!(module_file, "}}")?; | ||
writeln!(module_file)?; | ||
|
||
for (_, response_type) in response_types.iter() { | ||
writeln!(module_file, "#[cfg(feature = \"messages_enum\")]")?; | ||
writeln!( | ||
module_file, | ||
"impl From<{response_type}> for ResponseKind {{" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍