tychedelia / kafka-protocol-rs

Rust Kafka protocol
Apache License 2.0
63 stars 23 forks source link

decode_request is a great candidate for being codegen'd #89

Closed aovestdipaperino closed 1 month ago

aovestdipaperino commented 2 months ago

I am currently implementing this method myself, but it would be wonderful if it's part of the codegen when the message_enum flag is enabled:

    fn decode_request(
        buf: &mut BytesMut,
        api_key: ApiKey,
        request_api_version: i16,
    ) -> Result<RequestKind, std::io::Error> {
        let req = match api_key {
            ApiKey::ApiVersionsKey => RequestKind::ApiVersions(
                ApiVersionsRequest::decode(buf, request_api_version).unwrap(),
            ),
            ApiKey::CreateTopicsKey => RequestKind::CreateTopics(
                CreateTopicsRequest::decode(buf, request_api_version).unwrap(),
            ),
            ApiKey::InitProducerIdKey => RequestKind::InitProducerId(
                InitProducerIdRequest::decode(buf, request_api_version).unwrap(),
            ),
            ApiKey::FindCoordinatorKey => RequestKind::FindCoordinator(
                FindCoordinatorRequest::decode(buf, request_api_version).unwrap(),
            ),
            ApiKey::MetadataKey => {
                RequestKind::Metadata(MetadataRequest::decode(buf, request_api_version).unwrap())
            }
            ApiKey::ProduceKey => {
                RequestKind::Produce(ProduceRequest::decode(buf, request_api_version).unwrap())
            }
            ApiKey::FetchKey => {
                RequestKind::Fetch(FetchRequest::decode(buf, request_api_version).unwrap())
            }
            ApiKey::ListOffsetsKey => {
                RequestKind::ListOffsets(ListOffsetsRequest::decode(buf, request_api_version).unwrap())
            }
            _ => Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid API key",
            ))?,
        };
        Ok(req)
    }
tychedelia commented 2 months ago

Yeah, have done something similar here before, would def accept a pr!

rukai commented 2 months ago

I believe that already exists as https://docs.rs/kafka-protocol/latest/kafka_protocol/messages/enum.RequestKind.html#method.decode ?

aovestdipaperino commented 1 month ago

Thanks, I was misled by the generated code example!